Couple of Days back i started reading Design Patterns Java™ Workbook By Steven John Metsker.
Nice book written by Steven John Metsker.
I wanted to share few quotations found the book from the next 2 chapters (15-16).
1) The builder pattern moves the construction logic for an object outside the class to instantiate.Making this move might be useful for several reasons. You might simply want to reduce the size of a class that has many methods. You may also want to allow step-by-step construction of a target object. This occurs when you acquire the parameters for a constructor gradually, as happens with parsers and may happen with a user interface.
2) The BUILDER pattern separates the construction of a complex object from its representation.This has the immediate effect of making a complex target class simpler. It lets a builder class focus on the proper construction of an object, leaving the target class to focus on the operation
of a valid instance. This is especially useful when you want to ensure the validity of an object before instantiating it and don't want the associated logic to appear in the target class's constructors. A builder also accommodates step-by-step construction, which occurs when you
create an object by parsing text and may occur when you gather an object's parameters from a user interface.
3) As a class developer, you will ordinarily provide class constructors to let users of your class instantiate it. Sometimes, however, a client that needs an object does not or should not know which of several possible classes to instantiate. The FACTORY METHOD pattern lets a class
developer define the interface for creating an object while retaining control of which class to instantiate.
4) You might feel that a method like Arrays.asList(), which instantiates an object and has an interface return type, is an example of FACTORY METHOD. After all, such methods do isolate a client from knowing which class to instantiate. But the spirit of FACTORY METHOD is that the object creator makes a choice about which of several possible classes to instantiate for the client. The Arrays.asList() method instantiates the same type of object for every client, so this method does not fulfill the intent of FACTORY METHOD.
5) Usually, you will find that several classes in a hierarchy implement the FACTORY METHOD operation. Then the decision of which class to instantiate depends on the class of the object that receives the call to create. In other words, FACTORY METHOD lets subclasses decide
which class to instantiate.
6) The intent of the FACTORY METHOD pattern is to define the interface for creating a new object so that a service provider decides which class to instantiate instead of clients. This pattern occurs in the Java class libraries and is common in application code You can also apply FACTORY METHOD when you want to introduce a parallel class hierarchy. This can help keep a set of classes from becoming bloated with many different aspects of behavior. FACTORY METHOD lets you connect parallel hierarchies by letting subclasses in one hierarchy determine which class to instantiate in the corresponding hierarchy.
Wednesday, 7 January 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment