1 / 7

Practice with Composite and Interpreter Patterns

This tutorial explores the implementation of the Composite and Interpreter patterns through a practical example of computing with lists in Java. It covers the IntList class, which represents a list of integers, and demonstrates the usage of the Empty and NonEmpty subclasses. The tutorial also introduces the concept of the Singleton pattern and provides an example implementation using the IntList class.

sryan
Download Presentation

Practice with Composite and Interpreter Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Practice with Composite and Interpreter Patterns

  2. Another Composite Example Computing with Lists • An IntList is either: • Empty(), the empty list, or • NonEmpty(first,rest), a non-empty list, where first is an int and rest is an IntList. • Some examples include:Empty()NonEmpty(7,Empty()) NonEmpty(12, NonEmpty(17, Empty()))

  3. IntList This class is located in a library so it must be imported import java.util.NoSuchElementException; abstract class IntList { abstract int getFirst(); abstract IntList getRest(); } class Empty extends IntList { int getFirst() { throw new NoSuchElementException("getFirst applied to Empty()"); } int getRest() { throw new NoSuchElementException("getRest applied to Empty()"); } public String toString() { return "Empty()"; } } class NonEmpty extends IntList { int first; IntList rest; int getFirst() { return first; ); IntList getRest() { return rest; } public String toString() { return "NonEmpty(" + first + ", " + rest + ")"; } }

  4. Remarks onIntList • The operations getFirst() and getRest()are included in the abstract class IntListbecause they are useful operations for clients of IntList. • Invoking getFirst() or getRest()on the Empty list is a run-time error, which we implement by throwing an exception. The Java throw construct takes an object of type Exception (which is a built-in class). In the absence of a catch handler attached to a method on the call stack, throwing an exception aborts the computation and prints the String message embedded in the exception.

  5. Finger Exercise Open the class IntList.java in DrJava, compile it, and try evaluating new Empty().getFirst() il1 = new NonEmpty(17, new Empty()) il1 Il1.getRest().getFirst()

  6. The Singleton Pattern • Each execution of the expressionnew Empty()creates a new object. In principle, there is only one empty list, just like there is only one number 0. Hence, we would like to represent the empty list by a single library. • The singletonpattern is the mechanism that we use to create a unique instance of a class. This pattern consists of two chunks of code: • a static final field in the class that holds the single instance of the class • a private attribute on the class constructor, so no client can create another instance of the class.

  7. Singleton IntList import java.util.NoSuchElementException; abstract class IntList { abstract int getFirst(); abstract IntList getRest(); } class Empty extends IntList { static final Empty ONLY = new Empty(); private Empty() {} int getFirst() { throw new NoSuchElementException("getFirst applied to Empty()"); } int getRest() { throw new NoSuchElementException("getRest applied to Empty()"); } public String toString() { return "Empty()"; } } class NonEmpty extends IntList { int first; IntList rest; int getFirst() { return first; ); IntList getRest() { return rest; } public String toString() { return "NonEmpty(" + first + ", " + rest + ")"; } } Static member holding the unique instance Private constructor

More Related