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 ...
My first encounter with the Java programming language was during a one-week Java training session in 1997. I did not get a chance to use Java in a project until 1999. I read two Java books and took 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 ...
My first encounter with the Java programming language was during a one-week Java training session in 1997. I did not get a chance to use Java in a project until 1999. I read two Java books and took a...
My first encounter with the Java programming language was during a one-week Java training session in 1997. I did not get a chance to use Java in a project until 1999. I read two Java books and took a ...
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 ...
then faced with the question, “Should I shorten the content of the book or include all the details that I think a Java developer needs?” I opted for including all the details in the book, rather ...
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 ...
Many Python developers are curious about what machine learning is and how it can be concretely applied to solve issues faced in businesses handling medium to large amount of data. Machine Learning ...
Get a solid understanding of Java fundamentals through series of practical steps to the actual real programming Key Features Take your first steps into the world of programming Understand what a ...
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. ...
One of the first questions they are immediately faced with is providing a common security model for their heterogeneous services. Many enterprises with heavy investments into Java technologies have ...
Originally devised to enhance web pages in Netscape 2.0, JavaScript is now faced with being a single-threaded language in a multimedia, multitasking, multicore world. Yet JavaScript has not only ...
It will expose your innards and then build a report of their indicators of compromise along with detection rule sets that will enable you to help contain the outbreak when faced with such a situation...
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 ...
Using Spring Security, you will secure microservices and test them effectively using REST Java clients and other tools such as RxJava 2.0. We'll show you the best patterns, practices, and common ...
This means that developers now are faced with the challenge of building build applications that are native to the cloud. For this, they need to be aware of the environment, tools, and resources they'...