Tuesday, 6 January 2009

Design Patterns Java™ Workbook - Part7

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 (13-14).

1) The flyweight pattern addresses sharing, relying on an object's ability to be responsible to more than a single client. An ordinary object doesn't have to worry much about shared responsibility. Often, only one client will hold a reference to an object at any one time. When the object's state changes, it's because the client changed it, and the object does not have any responsibility to inform any other clients. Sometimes, though, you will want to arrange for multiple clients to share access to an object.

One incentive for sharing an object among multiple clients occurs when you must manage thousands or tens of thousands of small objects, such as the characters in an online version of a book. In such a case, you may have a performance incentive to share these fine-grained objects among many clients. A book needs only one A object, although it needs a way to model where different As appear.

In any application having a large number of small objects, you may need to provide a way for clients to safely share the common elements of these objects. The intent of the FLYWEIGHT pattern is to use sharing to support large numbers of fine-grained objects efficiently.

2) Immutability

When a client changes the state of an object, the state changes for every client that has access to the object. This is no problem when there is only a single client, which is the most ordinary case. When multiple clients will share access to an object, the easiest and most common way to keep clients from affecting one another is to restrict clients from introducing any state changes in the shared object. You can achieve this by making an object immutable so that once created, the object cannot change. The most common immutable objects in Java are instances of the String class. Once you create a string, neither you nor any client with access to the string can change its characters.

3) When you have large numbers of similar objects, you may want to arrange for shared access to these objects, but they may not be immutable. In this case, a preliminary step in applying the FLYWEIGHT pattern is to extract the immutable part of an object so that this part can be shared.

4) The FLYWEIGHT pattern lets you share access to objects, such as characters, chemicals, and borders, that may appear in large quantities. The flyweight objects must be immutable,a feature you can establish by extracting the immutable part of the class that you want to share. To ensure that your flyweight objects are shared, you have to provide a factory in which clients can find flyweights, and you have to enforce the use of this factory. Visibility modifiers give you some control over how other developers access your code. Inner classes take this further, letting you guarantee that a class is accessible by only its containing class.By ensuring that clients use your flyweight factory properly, you can provide safe, shared access to what would otherwise be a multitude of fine-grained objects.

5) When you create a Java class, you normally provide for the creation of objects of your class by supplying class constructors. In many regards, constructors are like any other methods, but Java has numerous rules that specifically govern the use of constructors. In particular,constructors have special abilities to collaborate with one another.

Glossary

TITLE CASE

A String Like This Whose Characters Are Uppercase If They Follow Whitespace.

TREE

An object model that contains no cycles.

UNIFIED MODELING LANGUAGE

A notation for illustrating design ideas.

UNIFORM RESOURCE LOCATOR

A pointer to a resource on the World Wide Web.

UNIT

A standard measure; a standard magnitude or amount of a particular physical dimension.

UTILITY

A class that has all static methods.

WORKBOOK

A student's book of problems and exercises.

No comments: