Practice with Composite and Interpreter Patterns - PowerPoint PPT Presentation

About This Presentation
Title:

Practice with Composite and Interpreter Patterns

Description:

Practice with Composite and Interpreter Patterns Another Composite Example Computing with Lists An IntList is either: Empty(), the empty list, or NonEmpty(first,rest ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 8
Provided by: Tria622
Learn more at: https://www.cs.rice.edu
Category:

less

Transcript and Presenter's Notes

Title: Practice with Composite and Interpreter Patterns


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 includeEmpty() 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()"

4
Remarks on IntList
  • The operations getFirst() and getRest() are
    included in the abstract class IntList because
    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 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.

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()"

Static member holding the unique instance
Private constructor
Write a Comment
User Comments (0)
About PowerShow.com