Practice with Composite and Interpreter Patterns Another Composite

  • Slides: 7
Download presentation
Practice with Composite and Interpreter Patterns

Practice with Composite and Interpreter Patterns

Another Composite Example Computing with Lists • An Int. List is either: – Empty(),

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

Int. List import java. util. No. Such. Element. Exception; This class is located in

Int. List import java. util. No. Such. Element. Exception; This class is located in a library so it must be imported abstract class Int. List { abstract int get. First(); abstract Int. List get. Rest(); } class Empty extends Int. List { int get. First() { throw new No. Such. Element. Exception("get. First applied to Empty()"); } int get. Rest() { throw new No. Such. Element. Exception("get. Rest applied to Empty()"); } public String to. String() { return "Empty()"; } } class Non. Empty extends Int. List { int first; Int. List rest; int get. First() { return first; ); Int. List get. Rest() { return rest; } public String to. String() { return "Non. Empty(" + first + ", " + rest + ")"; } }

Remarks on Int. List • The operations get. First() and get. Rest() are included

Remarks on Int. List • The operations get. First() and get. Rest() are included in the abstract class Int. List because they are useful operations for clients of Int. List. • Invoking get. First() or get. Rest() 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.

Finger Exercise Open the class Int. List. java in Dr. Java, compile it, and

Finger Exercise Open the class Int. List. java in Dr. Java, compile it, and try evaluating new Empty(). get. First() il 1 = new Non. Empty(17, new Empty()) il 1 Il 1. get. Rest(). get. First()

The Singleton Pattern • Each execution of the expression new Empty() creates a new

The Singleton Pattern • Each execution of the expression new 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 singleton pattern 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.

Singleton Int. List import java. util. No. Such. Element. Exception; abstract class Int. List

Singleton Int. List import java. util. No. Such. Element. Exception; abstract class Int. List { abstract int get. First(); abstract Int. List get. Rest(); } class Empty extends Int. List { Static static final Empty ONLY = new Empty(); private Empty() {} Private constructor int get. First() { throw new No. Such. Element. Exception("get. First applied to Empty()"); } int get. Rest() { throw new No. Such. Element. Exception("get. Rest applied to Empty()"); } public String to. String() { return "Empty()"; } } member holding the unique instance class Non. Empty extends Int. List { int first; Int. List rest; int get. First() { return first; ); Int. List get. Rest() { return rest; } public String to. String() { return "Non. Empty(" + first + ", " + rest + ")"; } }