builder-pattern

Passing Properties to Factory method

99封情书 提交于 2019-11-28 13:50:47
I have a factory method which returns implementation of an interface. The thing is - implementations have different constructor parameters. My question is - how to pass parameters through factory method to different implementations of the interface? I have an idea, but I'm not sure if it makes sense - pass Properties object to factory method? This way each of interface implementations can get the properties that needs for its constructor, while factory interface will be unified. Does this make sense, or there is a better solution? I decided to add-up an example, so I could clarify the problem

How to improve the builder pattern?

荒凉一梦 提交于 2019-11-27 11:13:17
Motivation Recently I searched for a way to initialize a complex object without passing a lot of parameter to the constructor. I tried it with the builder pattern, but I don't like the fact, that I'm not able to check at compile time if I really set all needed values. Traditional builder pattern When I use the builder pattern to create my Complex object, the creation is more "typesafe", because it's easier to see what an argument is used for: new ComplexBuilder() .setFirst( "first" ) .setSecond( "second" ) .setThird( "third" ) ... .build(); But now I have the problem, that I can easily miss an

Passing Properties to Factory method

一笑奈何 提交于 2019-11-27 08:01:59
问题 I have a factory method which returns implementation of an interface. The thing is - implementations have different constructor parameters. My question is - how to pass parameters through factory method to different implementations of the interface? I have an idea, but I'm not sure if it makes sense - pass Properties object to factory method? This way each of interface implementations can get the properties that needs for its constructor, while factory interface will be unified. Does this

How to improve the builder pattern?

一曲冷凌霜 提交于 2019-11-26 17:59:34
问题 Motivation Recently I searched for a way to initialize a complex object without passing a lot of parameter to the constructor. I tried it with the builder pattern, but I don't like the fact, that I'm not able to check at compile time if I really set all needed values. Traditional builder pattern When I use the builder pattern to create my Complex object, the creation is more "typesafe", because it's easier to see what an argument is used for: new ComplexBuilder() .setFirst( "first" )

What is the difference between Builder Design pattern and Factory Design pattern?

别来无恙 提交于 2019-11-26 16:45:57
What is the difference between the Builder design pattern and the Factory design pattern? Which one is more advantageous and why ? How do I represent my findings as a graph if I want to test and compare/contrast these patterns ? With design patterns, there usually is no "more advantageous" solution that works for all cases. It depends on what you need to implement. From Wikipedia: Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the

Builder Pattern in Effective Java

风格不统一 提交于 2019-11-26 11:38:32
I have recently started to read Effective Java by Joshua Bloch. I found the idea of the Builder pattern [Item 2 in the book] really interesting. I tried to implement it in my project but there were compilation errors. Following is in essence what I was trying to do: The class with multiple attributes and its builder class: public class NutritionalFacts { private int sodium; private int fat; private int carbo; public class Builder { private int sodium; private int fat; private int carbo; public Builder(int s) { this.sodium = s; } public Builder fat(int f) { this.fat = f; return this; } public

What is the difference between Builder Design pattern and Factory Design pattern?

混江龙づ霸主 提交于 2019-11-26 04:57:01
问题 What is the difference between the Builder design pattern and the Factory design pattern? Which one is more advantageous and why ? How do I represent my findings as a graph if I want to test and compare/contrast these patterns ? 回答1: With design patterns, there usually is no "more advantageous" solution that works for all cases. It depends on what you need to implement. From Wikipedia: Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of

Builder Pattern in Effective Java

坚强是说给别人听的谎言 提交于 2019-11-26 02:28:53
问题 I have recently started to read Effective Java by Joshua Bloch. I found the idea of the Builder pattern [Item 2 in the book] really interesting. I tried to implement it in my project but there were compilation errors. Following is in essence what I was trying to do: The class with multiple attributes and its builder class: public class NutritionalFacts { private int sodium; private int fat; private int carbo; public class Builder { private int sodium; private int fat; private int carbo;