我想了解uml合成到代碼的轉換。 我有三個代碼狗有內存的例子的問題。這三個例子可以被認爲是一個組成(uml含義的組成)?試圖將uml合成轉換成代碼[uml]
實施例1
class Memory {
// CODE
}
class Dog {
private Memory variable;
Dog(Memory variable) {
this.variable = variable;
}
}
class Factory {
Dog createDog() {
Memory memory = new Memory() // memory contains reference to object Memory only moment and after create dog don't use it
return new Dog(memory);
}
}
實施例2
class Memory {
// CODE
}
class Dog {
private Memory variable;
Dog(Memory variable) {
this.variable = variable;
}
}
class Factory {
Dog createDog() {
return new Dog(new Memory());
}
}
實施例3
class Memory {
// CODE
}
class MemoryFactory {
Memory createMemory() {
return new Memory();
}
}
class Dog {
private Memory variable;
Dog(MemoryFactory memoryFactory) {
this.variable = memoryFactory.createMemory();
}
}
class Factory {
Dog createDog() {
MemoryFactory factory = new MemoryFactory()
return new Dog(factory);
}
}
有點不同的示例:
class Memory {
// CODE
}
class Dog {
private Memory variable;
Dog() {
this.variable = new Memory();
Other other = new Other();
other.method(variable);
}
}
class Other {
void method(Memory memory) {
// code which don't save reference to memory
}
}
這是進一步組合物?
除了您還有一個關聯之外,您的最後一個示例基本上沒有區別。請先嚐試理解答案。 –