Say that creating/calculating all the properties of fruit needs a lot of logic, and you just want to have a very simple immutable POCO object in the end.
So i.e. it doesn’t have any methods, making it really easy to reason about, and also easily and clearly serializable.
Right now I have something names like this:
class BananaCalulator { public Banana Calculate() }
class Banana {}
class AppleCalulator { public Apple Calculate() }
class Apple {}
Is this a common pattern? Is there a more common terminology instead of Calculator?
7
The Gang of Four knows two patterns which describe objects which do nothing but create other objects.
One is the Factory.
The other is the Builder.
The difference between the two is that a Factory receives all arguments with a single method-call and usually can be reused to create more objects of the same type, a Builder has a number of methods which define the properties of the created object until a build-method is called. The Builder is often (but not necessarily) not reusable afterwards.
BananaFactory factory = new BananaFactory();
Banana b1 = factory.createBanana(100, 200);
Banana b2 = factory.createBanana(110, 210);
BananaBuilder builder = new BananaBuilder();
builder.setLength(100);
builder.setWeight(200),
Banana b3 = builder.build();
1