Design Patterns Java™ Workbook

(Michael S) #1
Chapter 14. Introducing Construction

Summary......................................................................................................................................................


Ordinarily, you will furnish classes that you develop with constructors to provide a means for
instantiating the class. These constructors may form a collaborating suite, and every call to
a constructor will ultimately also invoke a superclass constructor.


Beyond Ordinary Construction.................................................................................................................


Java's constructor features provide many alternatives when you design a new class. However,
constructors are effective only if the user of your class knows which class to instantiate and
knows the required fields for instantiating an object. For example, the choice of which user
interface component to compose may depend on whether the program is running on
a handheld device or on a larger display. It can also happen that a developer knows which
class to instantiate but does not have all the necessary initial values or has them in the wrong
format. For example, the developer may need to create an object from a dormant or a textual
version of an object. In such circumstances, you need to go beyond the use of ordinary Java
constructors and apply a design pattern.


The following principles describe the intent of patterns that facilitate construction.


If you intend to Apply the pattern



  • Gather the information for an object gradually before
    requesting its construction


Builder (Chapter 15)


  • Defer the decision of which class to instantiate Factory Method
    (Chapter 16)

  • Construct a family of objects that share a trait Abstract Factory
    (Chapter 17)

  • Specify an object to create by giving an example Prototype (Chapter 18)

  • Reconstruct an object from a dormant version that contains
    just the object's internal state


Memento (Chapter 19)

The intent of each design pattern is to solve a problem in a context. Construction-oriented
patterns are designs that let a client construct a new object through a means other than calling
a class constructor. For example, when you find the initial values for an object gradually, you
may want to follow the BUILDER pattern.

Free download pdf