I am trying to understand the translation of uml composition into code. I have question about three example of code where Dog has Memory. Can these three examples be considered a composition (compositions in the meaning of uml)?
Example 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);
}
}
Example 2
class Memory {
// CODE
}
class Dog {
private Memory variable;
Dog(Memory variable) {
this.variable = variable;
}
}
class Factory {
Dog createDog() {
return new Dog(new Memory());
}
}
Example 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);
}
}
A little different example:
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
}
}
This is further composition?