Thursday, 15 January 2009

Design Patterns Java™ Workbook - Part13

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 (25-26).

1) The interpreter pattern, like the STATE and STRATEGY patterns, distributes an operation across a collection of classes. In such patterns, the effect of calling the operation depends on the class of the object that receives the call. In both STATE and STRATEGY, the receiver of an operation call is a single object. INTERPRETER takes the same idea and applies it to a composition—in particular, a rich composite, or one with various ways of forming groups.

2) The way a class composes other components defines how an INTERPRETER class will implement, or interpret, a distributed operation. Each composite class in an instance of INTERPRETER models a rule for how composition may occur. The intent of the INTERPRETER pattern is to let you compose executable objects according to a set of composition rules that you define. An interpreter object conducts the execution, or evaluation of a collection of rules, letting you build expression evaluators and command languages.

3) The INTERPRETER pattern addresses how interpreters work but does not specify how you should compose new interpreters at runtime. In this chapter, you have built new interpreters "manually," by writing lines of Java code. But by far the most common way to create a new interpreter is with a parser. A parser can read textual commands from a file or from a user prompt and can use the text to create an interpreter. Powerful! Another reason to learn about building parsers is that if you want to understand the INTERPRETER chapter in Design Patterns (Gamma et al. 1995), you really need to understand the interrelation of interpreters,parsers, and computer languages.

4) A parser is an object that can recognize text and decompose its structure according to a set of rules into a form suitable for further processing.

5) When you write a parser, the set of strings it recognizes is a language. When you create a new little computer language, you will most often create a context-free language, a pattern of text that follows a grammar—a collection of rules that determine how you can compose an element of the language. A parser decomposes a string according to the rules of composition.

6) Interpreters let you compose new commands, using the classes in a hierarchy that you create.Each class in the hierarchy defines a way of composing commands—a grammar rule. This composition rule determines how the class implements, or interprets, an operation that you distribute throughout the hierarchy.

The operation in an interpreter is often named execute() or evaluate(). The operation name is necessarily vague, becoming meaningful only when combined with the name of the class that implements it. Like STATE, STRATEGY, and COMMAND, the INTERPRETER pattern fixes the name of an operation and uses various classes to implement the operation.INTERPRETER applies this idea to let you compose executable objects according to a set of composition rules that you define.

7) An extension is the addition of a class, an interface, or a method to an existing code base.The most common way to extend code is to write a new class, although you can also extend a class through delegation. In particular, when you'd like to subclass from two parents, you can subclass from one parent and use delegation to "inherit" the behavior of the other class.

8) Object-oriented development in Java does not begin from scratch. Rather, it's fair to say that object-oriented software development is extension. To develop in Java, learn what is available in an existing code base, including a company's code and the Java class libraries. After seeing where your changes fit in, add the code you need for your application. In Java, application development is extension, and extension begins where reuse leaves off.

9) New classes should be logical, consistent extensions of their superclasses, but what does it mean to be logical and consistent? A Java compiler will ensure a certain level of consistency,but many principles of consistency will elude a compiler. One principle you should consider in your designs is the Liskov Substitution Principle (LSP). This principle, documented in Liskov (1987), can be paraphrased: An instance of a class should function as an instance of its superclass.

10) Extension through delegation occurs when a class has methods that forward calls to identical operations supplied by another object. This technique makes available the behavior of the forwarded-to class on the class you are extending, but this is usually not the best way to achieve extension. If you want to inherit the exact methods of an existing class, the most straightforward approach is to subclass it. However, this is not always possible. The class whose behavior you need may be declared final—as String is, for example—or your class may already be subclassing something else. In these cases, you can extend your class through delegation.

11) Talented Java developers spend more time reading than writing, to understand how their code will fit with and benefit from the voluminous class libraries. One class that you should be especially sure to understand is Object, the superclass from which all your classes will derive.

12) The ordinary way to extend a software system is to find a suitable superclass—usually Object—and to subclass it. You can also "inherit" behavior from more than one class by copying some of the operations of a second class and delegating calls to an instance of that class. Both of these extension techniques, however, require that you know at compile time what behaviors you want to add. If you need to add behavior to an object without changing its class, you may be able to apply the Template Method, Command,Decorator, or Visitor design patterns.

No comments: