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 (17-18).
1) Sometimes, you want to provide for object creation while retaining control of which class to instantiate. In such circumstances, you can apply the FACTORY METHOD pattern with a method that uses an outside factor to determine which class to instantiate. The outside factor can be anything.Sometimes, the factor that controls which object to instantiate can be thematic,running across several classes. The ABSTRACT FACTORY pattern addresses this situation.The intent of this pattern is to provide for the creation of a family of related, or dependent,
objects.
2) Design Patterns (Gamma et al. 1995) says that the intent for ABSTRACT FACTORY is that clients can create families of objects "without specifying their concrete classes." A literal reading of this might exclude the hierarchy of user interface component factories because clients will in fact know the exact class for each type of object. However, the point of the ABSTRACT FACTORY pattern is that the factory classes isolate clients from knowing how to create objects.
3) The ABSTRACT FACTORY pattern lets you arrange for a client to create objects that are part of a family of related, or dependent, objects. The theme, or common trait of the family, such as which country the classes pertain to, may run across many classes. In this case, you may maintain parallel packages, perhaps placing each family in its own package. For GUI factories, you may not need more than one package if different "classes" of GUI components differ only in such attributes as text size and cursor type. However you organize your code,ABSTRACT FACTORY lets you provide a client with a factory that produces objects that are related by a common theme.
4) As a class developer, you ordinarily furnish your class with constructors to let client applications instantiate it. The construction-oriented patterns covered so far—BUILDER,FACTORY METHOD, and ABSTRACT FACTORY—isolate the user of an object from this
constructor call. These patterns establish methods that instantiate an appropriate class on a client's behalf. The PROTOTYPE pattern also conceals object creation from clients but uses a different approach. Instead of bringing forth new, uninitialized instances of a class, the intent of the PROTOTYPE pattern is to provide new objects by copying an example.
5) The PROTOTYPE pattern lets a client create new objects by copying an example. A major difference between calling a constructor and copying an object is that a copy typically includes some of the state of the original object. You can use this to your advantage,particularly when different "classes" of objects differ only in their attributes, not in their behaviors. In such a case, you can create new classes at runtime by crafting prototypical objects for a client to copy.
When you need to create a copy, Object.clone() can help, but you must remember that this method creates a new object with the same fields. This object may not be a suitable clone,but any difficulties related to deep copying are your responsibility. If creating a deep copy is too complex or if a prototypical object has too many fields, you can create a copy by instantiating a new object and setting its fields to match just the aspects of original object that you need to copy.
Thursday, 8 January 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment