Monday, 5 January 2009

Design Patterns Java™ Workbook - Part6

Yesterday 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 (11-12).

1) An ordinary object does its own work in support of the public interface that it advertises. It can happen, though, that a legitimate object cannot live up to this ordinary responsibility. This occurs most frequently when an object takes a long time to load or when the object is running on another computer. In these cases, a proxy object can take the responsibility that a client expects and forward the request appropriately to an underlying target object. The intent of the PROXY pattern is to provide a surrogate, or placeholder, for another object to control access to it.

2) RMI makes it about as easy as possible for a client to obtain a proxy object that forwards calls to a desired object that is active on another computer. It is well worth learning about RMI, as RMI is part of the underpinning of the Enterprise JavaBeans (EJB) specification, an important emerging industry standard. Regardless of how industry standards evolve, the role of PROXY in distributed computing will continue into the foreseeable future, and RMI provides a good example of this pattern in action.

3) Implementations of the PROXY pattern establish a placeholder object that manages access to a target object. A proxy object can isolate clients from shifts in state of a desired object, as when loading an image endures a discernible duration. The problem with PROXY is that by its nature, it relies on a tight coupling between the placeholder and the proxied object.

Nowhere is this coupling more justified than in remote computing. Here, the PROXY pattern represents the ideal form of intercomputer communication. Rather than relying on another protocol, distributed computing schemes, such as RMI, establish communication with remote objects as normal-looking method calls. This lets a client communicate with a remote object through a proxy as if the remote object were local. The role of PROXY in distributed computing appears to be a permanent advance in object-oriented computing.

4) Object-oriented developers strive to keep objects loosely coupled, keeping the responsibility between objects specific and minimal. This lets you introduce change more easily and with less risk of introducing defects. To a degree, decoupling occurs naturally in Java. Clients see only an object's visible interface and remain isolated from the details of the object's implementation. This arrangement, however, leaves in place the fundamental coupling that the client knows which object has the method the client needs to call.

5) An opportunity to loosen the restriction that a client must know which object to use occurs when you can arrange the objects in chains. In such a case, you can provide these objects with an operation that each object either performs or passes along the chain. The intent of the CHAIN OF RESPONSIBILITY pattern is to avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. To apply this pattern,chain the receiving objects and pass the request along the chain until an object handles it.

6) Varieties of Lookup

In CHAIN OF RESPONSIBILITY, an object model takes on the job of finding which object can satisfy a client's request. This approach goes beyond the two lookup mechanisms that are built into Java: exception handling and method lookup.

7) When an exception is thrown, the Java interpreter looks back up the call stack to find a method called from a block enclosed in a try/catch statement. If this try/catch statement does not catch an exception of the type thrown, the Java interpreter keeps looking. This can propagate up to the main() method. If the exception is not handled there, the Java interpreter prints out an error message and stack trace and then exits.

8) The more common case of lookup in Java is method lookup—the algorithm for deciding which definition of a method to use when a client calls an object's method. For example, if you call an object's toString() method, Java will use the method's implementation that appears lowest in the object's class hierarchy. When the compiler cannot make this determination in advance, the Java interpreter looks up the right method to invoke, at runtime.

9) Chain of Responsibility without Composite

CHAIN OF RESPONSIBILITY requires a strategy for ordering the search for an object that can handle a request. Usually, the order to follow will depend on an underlying aspect in the modeled domain.

When you apply the CHAIN OF RESPONSIBILITY pattern, you relieve a client from having to know which object in a collection supports a given behavior. By setting up the search for responsibility to occur across objects, you decouple the client from any specific object in the chain.

The CHAIN OF RESPONSIBILITY pattern occurs occasionally when an arbitrary chain of objects can apply a series of different strategies to tackling a problem, such as parsing a user's input. More frequently this pattern occurs in aggregations, in which a containment hierarchy provides a natural ordering for a chain of objects.CHAIN OF RESPONSIBILITY leads to simpler code in both the hierarchy and the client, which is the main advantage of this pattern.


Glossary

ROOT

In a tree, a distinguished node or object that has no parent.

SEQUENCE DIAGRAM

A drawing that shows a flow of messages between objects.

SESSION

The event of a user running a program, conducting transactions within the program, and exiting.

SHALLOW COPY

As opposed to deep copy, a shallow copy limits the depth to which it copies an object's attributes, letting the new object share subordinate objects with the original.

SIGNATURE

A combination of a method's name and the number and types of its formal parameters.

STATE

A combination of the current values of an object's attributes.

STATIC METHOD

A method that is bound to a class and that can be invoked against the class or against an object whose declared type is the class.

STRATEGY

A plan, or approach, for achieving an aim given certain input conditions.

STREAM

A serial collection of bytes or characters, such as those that appear in a document.

STRUCTURED QUERY LANGUAGE

A computer language for querying relational databases.

STUB

A method that does nothing; a class that implements an interface with methods that do nothing.

TIER

A layer that executes on a computer.

No comments: