Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

Source code

 * Declares an abstract Interpret operation that is common to all nodes in the
 * abstract syntax tree.
 * @role __InterpreterExpression
public abstract class AbstractExpression {
        public abstract void interpret(Context context);}

 * Builds an abstract syntax tree representing a particular sentence in the
 * language that the grammar defines. Invokes the Interpret operation.
public class Client {
        public AbstractExpression getExpression() {
                // put your code to create expression here
                return null;        }

        public void sampleOperation() {
                Context context = new Context();                
} } /** * Contains information that is global to interpreter. * * @role __InterpreterContext */ public class Context { } /** * One such class is required for every rule in the grammar. */ public class NonterminalExpression extends AbstractExpression { public void interpret(Context context) { // put your code here } } /** * Implements an Interpret operation associated with terminal symbols in the * grammar. */ public class TerminalExpression extends AbstractExpression { public void interpret(Context context) { // put your code here } }

You might like also

Template method
Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses refine certain steps of an algorithm without changing the algorithm's structure.Source Code/** * This class defines abstract primitive operations that concrete * subclasses define to implement steps of an algorithm. Implements a template * method defining the skeleton of an algorithm. * * @role __TemplateContext */ public abstract class Context } /** * Implements the primitive operations to carry out subclass-specific steps …
5476 Days ago
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.Source Code/** * Defines an interface for encapsulating the behavior associated with a * particular state of the Context. * * @role __State */ public interface State /** * Defines an interface of interest to clients. Maintains an instance of a * ConcreteState subclass that defines the current state. */ public class Context public void someOperation() } /** * Implements a …
5476 Days ago
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.Source code/** * Creates a memento containing a snapshot of its current internal state. * Uses the memento to restore its internal state. * @role __Originator */ public class Originator public void setMemento(Memento memento) } } /** * Represents narrow interface of the memento visible to Caretaker * @role __Memento */ public interface Memento /** * Stores internal state of …
5483 Days ago
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.Source code/** * Each colleague knows its Mediator object. Communicates with its mediator * whenever it would have otherwise communicated with another colleague. * * @role __Colleague */ public abstract class Colleague /** @return mediator this colleague knows about */ public Mediator getMediator() } /** Concrete Colleague */ public …
5483 Days ago
Chain of responsability
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.Source code/** * Handles request it is responsible to. */ public class ConcreteHandler extends Handler else } } /** * Defines interface for request handling * @role __Handler */ public class Handler } public Handler getSuccessor() public void setSuccessor(Handler successor) } …
5483 Days ago
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Source code/** * Knows its observers. Any number of Observer objects * may observe a subject. Provides an interface for attaching * and detaching Observer objects. * @role __Subject */ public class Subject public void detach(Observer observer) } protected void notifyObservers() } } /** * Defines an updating interface for objects that * should be notified of changes in …
5483 Days ago
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Builds an undo/redo manager and a set of command classes.Source Code/** * Declares an interface for executing an operation * @role __Command */ public interface Command /** * Defines a binding betweeen a Receiver object and an action. * Implements Command by invoking the corresponding operation(s) on Receiver */ public class CommandA implements Command /** @see patterns.gof.command.Command#executeCommand()*/ public …
5483 Days ago
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.Source Code/** * Knows which subsystem classes are responsible for a request. * Delegates client requests to appropiate susystem objects. * @role __Facade */ public class Facade } /** * Implements subsystem functionality. Handles work assigned by the Facade object. * Has no knowledge of the facade. * @role __System */ public class SubsystemA } /** * …
5483 Days ago