Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

The interfaces Contact and Location, with their corresponding implementation classes ContactImpl and LocationImpl, provide additional business objects used by the Appointment class.

Example A.46 Contact.java

1.import java.io.Serializable;

2.public interface Contact extends Serializable{

3.public static final String SPACE = " ";

4.public String getFirstName();

5.public String getLastName();

6.public String getTitle();

7.public String getOrganization();

8.

9.public void setFirstName(String newFirstName);

10.public void setLastName(String newLastName);

11.public void setTitle(String newTitle);

12.public void setOrganization(String newOrganization);

13.}

Example A.47 ContactImpl.java

1.public class ContactImpl implements Contact{

2.private String firstName;

3.private String lastName;

4.private String title;

5.private String organization;

6.public static final String EOL_STRING =

7.System.getProperty("line.separator");

9.public ContactImpl(){ }

10.public ContactImpl(String newFirstName, String newLastName,

11.String newTitle, String newOrganization){

12. firstName = newFirstName;

13. lastName = newLastName;

14. title = newTitle;

15. organization = newOrganization;

16. }

17.

18.public String getFirstName(){ return firstName; }

19.public String getLastName(){ return lastName; }

20.public String getTitle(){ return title; }

21.public String getOrganization(){ return organization; }

23.public void setFirstName(String newFirstName){ firstName = newFirstName; }

24.public void setLastName(String newLastName){ lastName = newLastName; }

25.public void setTitle(String newTitle){ title = newTitle; }

26.public void setOrganization(String newOrganization){ organization = newOrganization; }

28.public String toString(){

29.return firstName + " " + lastName;

30.}

31.}

Example A.48 Location.java

1.import java.io.Serializable;

2.public interface Location extends Serializable{

3.public String getLocation();

4.public void setLocation(String newLocation);

5.}

Example A.49 LocationImpl.java

1.public class LocationImpl implements Location{

2.private String location;

3.

4.public LocationImpl(){ }

5.public LocationImpl(String newLocation){

6.location = newLocation;

7.}

8.

9. public String getLocation(){ return location; }

10.

11. public void setLocation(String newLocation){ location = newLocation; }

12.

13.public String toString(){ return location; }

14.}

246

RunPattern loads the data for a sample Appointment and creates an instance of CommandGui. The GUI enables you to make changes to the location of the Appointment, with update, undo and redo behavior.

Example A.50 RunPattern.java

1.import java.util.Calendar;

2.import java.util.Date;

3.

4.public class RunPattern{

5.private static Calendar dateCreator = Calendar.getInstance();

7.public static void main(String [] arguments){

8.System.out.println("Example for the Command pattern");

9.System.out.println();

10.System.out.println("This sample will use a command class called");

11.System.out.println(" ChangeLocationCommand to update the location");

12.System.out.println(" of an Appointment object.");

13.System.out.println("The ChangeLocationCommand has the additional");

14.System.out.println(" ability to undo and redo commands, so it can");

15.System.out.println(" set the locaition back to its original value,");

16.System.out.println(" if desired.");

17.System.out.println();

18.

19.System.out.println("Creating an Appointment for use in the demo");

20.Contact [] people = { new ContactImpl(), new ContactImpl() };

21.Appointment appointment = new Appointment("Java Twister Semi-Finals",

22. people, new LocationImpl(""), createDate(2001, 10, 31, 14, 30),

23. createDate(2001, 10, 31, 14, 31));

24.

25.System.out.println("Creating the ChangeLocationCommand");

26.ChangeLocationCommand cmd = new ChangeLocationCommand();

27.cmd.setAppointment(appointment);

28.

29.System.out.println("Creating the GUI");

30.CommandGui application = new CommandGui(cmd);

31.application.setAppointment(appointment);

32.cmd.setLocationEditor(application);

33.application.createGui();

34.

35.}

36.public static Date createDate(int year, int month, int day, int hour, int minute){

37.dateCreator.set(year, month, day, hour, minute);

38.return dateCreator.getTime();

39.}

40.}

247

Interpreter

The Expression hierarchy is at the heart of the Interpreter pattern. It defines the grammar that can be used to create and evaluate expressions. The Expression interface is the foundation for all expressions, and defines the interpret method that performs an evaluation.

Table A-1 lists the interface and corresponding information.

Table A-1. Purpose of the Expression interface and its implementers

Expression

Common interface for all expressions

ConstantExpression

Represents a constant value

VariableExpression

Represents a variable value, obtained by calling a method on some class

CompoundExpression

A pair of comparison expressions that evaluate to a boolean result

AndExpression

The logical “and” of two expressions

OrExpression

The logical “or” of two expressions

ComparisonExpression

A pair of expressions that evaluate to a boolean result

EqualsExpression

Performs an equals method comparison between the two expressions

ContainsExpression

Checks to see if the first String expression contains the second one

Example A.51 Expression.java

1.public interface Expression{

2.void interpret(Context c);

3.}

Example A.52 ConstantExpression.java

1.import java.lang.reflect.Method;

2.import java.lang.reflect.InvocationTargetException;

3.public class ConstantExpression implements Expression{

4.private Object value;

5.

6.public ConstantExpression(Object newValue){

7.value = newValue;

8.}

9.

10.public void interpret(Context c){

11.c.addVariable(this, value);

12.}

13.}

Example A.53 VariableExpression.java

1.import java.lang.reflect.Method;

2.import java.lang.reflect.InvocationTargetException;

3.public class VariableExpression implements Expression{

4.private Object lookup;

5.private String methodName;

6.

7.public VariableExpression(Object newLookup, String newMethodName){

8.lookup = newLookup;

9.methodName = newMethodName;

10.}

11.

12.public void interpret(Context c){

13.try{

14. Object source = c.get(lookup);

15. if (source != null){

16. Method method = source.getClass().getMethod(methodName, null);

17. Object result = method.invoke(source, null);

18. c.addVariable(this, result);

19. }

20.}

21.catch (NoSuchMethodException exc){ }

22.catch (IllegalAccessException exc){ }

23.catch (InvocationTargetException exc){ }

24.}

25.}

248

Example A.54 CompoundExpression.java

1.public abstract class CompoundExpression implements Expression{

2.protected ComparisonExpression expressionA;

3.protected ComparisonExpression expressionB;

4.

5.public CompoundExpression(ComparisonExpression expressionA, ComparisonExpression

expressionB){

6.this.expressionA = expressionA;

7.this.expressionB = expressionB;

8.}

9.}

Example A.55 AndExpression.java

1.public class AndExpression extends CompoundExpression{

2.public AndExpression(ComparisonExpression expressionA, ComparisonExpression expressionB){

3.super(expressionA, expressionB);

4.}

5.

6.public void interpret(Context c){

7.expressionA.interpret(c);

8.expressionB.interpret(c);

9.Boolean result = new Boolean(((Boolean)c.get(expressionA)).booleanValue()

&&((Boolean)c.get(expressionB)).booleanValue());

10.c.addVariable(this, result);

11.}

12.}

Example A.56 OrExpression.java

1.public class OrExpression extends CompoundExpression{

2.public OrExpression(ComparisonExpression expressionA, ComparisonExpression expressionB){

3.super(expressionA, expressionB);

4.}

5.

6.public void interpret(Context c){

7.expressionA.interpret(c);

8.expressionB.interpret(c);

9.Boolean result = new Boolean(((Boolean)c.get(expressionA)).booleanValue() ||

((Boolean)c.get(expressionB)).booleanValue());

10.c.addVariable(this, result);

11.}

12.}

Example A.57 ComparisonExpression.java

1.public abstract class ComparisonExpression implements Expression{

2.protected Expression expressionA;

3.protected Expression expressionB;

4.

5.public ComparisonExpression(Expression expressionA, Expression expressionB){

6.this.expressionA = expressionA;

7.this.expressionB = expressionB;

8.}

9.}

Example A.58 EqualsExpression.java

1.public class EqualsExpression extends ComparisonExpression{

2.public EqualsExpression(Expression expressionA, Expression expressionB){

3.super(expressionA, expressionB);

4.}

5.

6.public void interpret(Context c){

7.expressionA.interpret(c);

8.expressionB.interpret(c);

9.Boolean result = new Boolean(c.get(expressionA).equals(c.get( expressionB)));

10.c.addVariable(this, result);

11.}

12.}

Example A.59 ContainsExpression.java

1.public class ContainsExpression extends ComparisonExpression{

2.public ContainsExpression(Expression expressionA, Expression expressionB){

3.super(expressionA, expressionB);

4.}

5.

6. public void interpret(Context c){

249