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 (27-28).
1) Ordinarily, an object inherits behaviors from its superclasses. As earlier chapters have shown,you can apply the STATE and the STRATEGY patterns to alter the behavior of an object dynamically. Like STATE and STRATEGY, the DECORATOR pattern relies on polymorphism,but DECORATOR combines polymorphism with delegation to let you extend an object's behavior. The intent of DECORATOR is to let you compose an object's behavior dynamically.
2) The Java class libraries provide a classic example of the DECORATOR pattern in the overall design of Java input and output streams. A stream is a serial collection of bytes or characters,such as those that appear in a document. In Java, each stream object usually contains another stream object to which it forwards its operations. This structure is typical of DECORATOR.
3) The DECORATOR pattern arranges for each decorator object to contain another decorator object. In this regard, a decorator is like a slim composite whose elements each have a single child. Unlike the COMPOSITE pattern, whose purpose is to compose aggregate objects,the purpose of DECORATOR is to compose behavior.
4) When you have an object type that needs a variety of behaviors that you might compose in various ways, the DECORATOR pattern offers a flexible alternative to creating a class for every possible behavioral combination. The Java class libraries provide a classic example of DECORATOR in the implementation of input and output streams. In application code, you can apply DECORATOR to set up function hierarchies. This lets you create a large family of function objects from a fixed set of function classes.
5) It is easy to mistake examples of "decoration" in a design as instances of DECORATOR.A design may allow runtime customization of an object's attributes and its behavior without using DECORATOR. The intent of DECORATOR is to let you compose an object's behavior from a collection of cooperating classes.
6) In ordinary development, you add behavior by extending classes with new methods.Occasionally, though, you need to add behavior to a collection of instances of the classes you create. The Java class libraries provide many features for working with collections, including behaviors for sorting, shuffling, reversing, and iterating over a collection. You may find,however, that you need iteration behaviors that go beyond those built into Java. In particular,if you need to add type safety to iteration, or if you need to create a new type of collection,you will probably need to develop your own iterator code to walk through the collection. The intent of the ITERATOR pattern is to provide a way to access the elements of collection sequentially.
7) The intent of the ITERATOR pattern is to let a client access the elements of a collection sequentially. The collection classes in the Java class libraries offer rich support for operating on collections, including support for iteration. These iterators usually require a client to cast the returned objects to the appropriate type. You can strengthen your contract with clients by creating type-specific collections with type-specific iterators.
8) If you create a new type of collection, you will often want to create an iterator to go with it.Domain-specific composites are a common example of a new collection type. You can design a fairly generic iterator for composites that you may be able to apply against a variety of composite hierarchies.
9) When you instantiate an iterator, you should consider whether the collection can change while you are iterating over it. There is usually not much chance of this in a single-threaded application. In a multithreaded application, you first have to ensure that access to a collection is synchronized. The Java class libraries provide good support for this, but that support does not guarantee safe iteration. To safely iterate in a multithreaded application, you can synchronize on the monitor of a mutex object. You can either block out all access while iterating or block access briefly while you make a clone of the collection. With a proper design, you can provide thread safety and type safety to the clients of your iterator code.
Monday, 19 January 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment