Design Patterns Java™ Workbook

(Michael S) #1
Appendix B. Solutions

SOLUTION 10.4....................................................................................................................................


The contents of Tub.java should look something like:


package com.oozinoz.chemical;
import com.oozinoz.machine.*;
public class Tub
{
protected String id;
protected Mediator mediator;


public Tub(String id, Mediator mediator)
{
this.id = id;
}


public Machine getMachine()
{
return mediator.getMachine(this);
}


public void setMachine(Machine machine)
{
mediator.set(this, machine);
}


public String toString()
{
return id;
}
}


SOLUTION 10.5....................................................................................................................................



  • The FACADE pattern may help to refactor a large application.

  • The BRIDGE pattern moves abstract operations to an interface.

  • The OBSERVER pattern may appear as you refactor code to support an MVC
    architecture.

  • The FLYWEIGHT pattern extricates the immutable part of an object so that this part can
    be shared.

  • The BUILDER pattern moves the construction logic for an object outside the class to
    instantiate.

  • The FACTORY METHOD pattern lets you reduce the amount of responsibility in a class
    hierarchy by moving an aspect of behavior to a parallel hierarchy.

  • The STATE and STRATEGY patterns let you move state-specific and strategy-specific
    behavior into separate classes.

Free download pdf