Traditionally, programmers have used the
telescoping constructor pattern.
In short, the telescoping constructor pattern works, but it is hard to write client code when there are many parameters, and harder still to read it.
A second alternative when you are faced with many constructor parameters is
the
JavaBeans pattern
1st.a may be in an inconsistent state partway through its construction.
2nd.precludes the possibility of making a class immutable
disadvantages :
1st. In order to create an object, you must first create its builder. While the cost of creating the builder is unlikely to be noticeable in practice, it could be a problem in some performancecritical situations.
2nd. the Builder pattern is more verbose than the telescoping
constructor pattern, so it should be used only if there are enough parameters, say,four or more. But keep in mind that you may want to add parameters in the future.If you start out with constructors or static factories, and add a builder when the class evolves to the point where the number of parameters starts to get out of hand,
the obsolete constructors or static factories will stick out like a sore thumb. Therefore,it’s often better to start with a builder in the first place.
In summary, the Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters, especially if most of those parameters are optional.
分享到:
相关推荐
Item 2: Consider a builder when faced with many constructor parameters Item 3: Enforce the singleton property with a private constructor or an enum type Item 4: Enforce noninstantiability with a ...
Common case: Suppose you’re faced with N equally probable choices, and you receive a message that narrows it down to M choices. The probability that message would be sent is M*(1/N) so the amount ...
"When faced with tight deadlines, I prioritize tasks based on urgency and importance, allocate resources efficiently, and communicate clearly with team members to ensure everyone is aware of the ...
Master the art of implementing scalable and reactive microservices in your production environment with Java 11 Microservices are key to designing scalable, easy-to-maintain applications. This latest ...
Mastering Microservices with Java, 3rd Edition: Master the art of implementing scalable and reactive microservices in your production environment with Java 11 Microservices are key to designing ...
When faced with solving such non-smooth problems, methods like the genetic algorithm or the more recently developed pattern search methods, both found in the Genetic Algorithm and Direct Search ...
The title itself suggests a candid and critical examination of the Java ecosystem, emphasizing the challenges and difficulties faced by developers in real-world scenarios. #### Key Points from the ...
Exploring real life applications, we will explore the Zend Framework 2 components, as well as throwing some light on best practices and design concerns faced when building complex MVC applications. ...
2. 勇气与选择:\"Some when faced with a bloody battle simply give in, but for some surrender is unacceptable, even though they know it would be a fight to the death.\" 这里阐述了面对困难时的两种态度:...
Achieving efficient code through performance tuning is one of the key challenges faced by many programmers. This book looks at Qt programming from a performance perspective. You’ll explore the ...