/** * Created by Aaron on 15/9/16. */ publicabstractclassAbstractShape{ publicvoidrender(){ this.drawBackground(); this.drawGraphics(); this.drawSignature(); } abstractvoiddrawSignature(); abstractvoiddrawBackground(); abstractvoiddrawGraphics();
}
package design.pattern.temlatemothod;
/** * Created by Aaron on 15/9/16. */ publicclassCircleShapeextendsAbstractShape{ @Override publicvoiddrawSignature(){ System.out.println("draw circle signature!"); }
@Override publicvoiddrawBackground(){ System.out.println("draw circle background! "); }
@Override publicvoiddrawGraphics(){ System.out.println("draw circle graphics!"); } }
package design.pattern.temlatemothod;
/** * Created by Aaron on 15/9/16. */ publicclassRectShapeextendsAbstractShape{ @Override publicvoiddrawSignature(){ System.out.println("draw rect signature!"); }
@Override publicvoiddrawBackground(){ System.out.println("draw rect background! "); }
@Override publicvoiddrawGraphics(){ System.out.println("draw rect graphics!"); } }
package design.pattern.temlatemothod;
/** * Created by Aaron on 15/9/16. */ publicclassUser{ publicstaticvoidmain(String args[]){ new CircleShape().render(); System.out.println("-----"); new RectShape().render(); } }
输出结果:
draw circle background!
draw circle graphics!
draw circle signature!
-----
draw circle background!
draw circle graphics!
draw circle signature!
/** * Created by Aaron on 15/9/16. */ publicabstractclassSubject{ private ArrayList<Observer> observers=new ArrayList<Observer>(); publicvoidaddObserver(Observer observer){ this.observers.add(observer); } publicvoidremoveObserver(Observer observer){ this.observers.remove(observer); } publicvoidnotifyObserver(){ for(Observer observer:observers){ observer.update(this); } } }
package design.pattern.observer;
import java.awt.*;
/** * Created by Aaron on 15/9/16. */ publicclassGPSSubjectextendsSubject{ private Point point; publicvoidmove(Point point){ this.point=point; this.notifyObserver(); } }
package design.pattern.observer;
/** * Created by Aaron on 15/9/16. */ publicabstractclassObserver{ publicObserver(){
} publicabstractvoidupdate(Subject subject);
}
package design.pattern.observer;
/** * Created by Aaron on 15/9/16. */ publicclassMapObserverextendsObserver{ @Override publicvoidupdate(Subject subject){ System.out.println(this.getClass().getSimpleName()+"_"+this.hashCode()+" observer:"+subject.getClass().getSimpleName()+" position changed;"); } }
package design.pattern.observer;
import java.awt.*;
/** * Created by Aaron on 15/9/16. */ publicclassUser{ publicstaticvoidmain(String[] args){ GPSSubject subject=new GPSSubject(); subject.addObserver(new MapObserver()); Observer observer1=null; subject.addObserver(observer1=new MapObserver()); subject.move(new Point(200, 400)); System.out.println("remove one observer from subject's observer list!"); subject.removeObserver(observer1); subject.move(new Point(200,400)); } }
状态模式(State);
GoF这样定义: 允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它 所属的类。
以下是示例代码:
package design.pattern.state;
/** * Created by Aaron on 15/9/20. */ publicclassContextextendsAbstractLifeState{ publicstatic OpeningState openingState = new OpeningState(); publicstatic ClosingState closingState = new ClosingState(); publicstatic RunningState runningState = new RunningState(); publicstatic StoppingState stoppingState = new StoppingState(); private AbstractLifeState lifeState;
publicContext(){ }
public AbstractLifeState getLifeState(){ return lifeState; }
/** * Created by Aaron on 15/9/29. */ publicclassLoggerHandlerextendsHandler{ @Override publicvoidprocess(){ if(getSuccessor()!=null){ System.out.println(getClass().getSimpleName()+",处理请求,并调用下一个处理者"); getSuccessor().process(); }else{ System.out.println(getClass().getSimpleName()+",仅处理,无下一处理者"); } } }
package design.pattern.responsibilitychain;
/** * Created by Aaron on 15/9/29. */ publicclassValidateHandlerextendsHandler{ @Override publicvoidprocess(){ if(getSuccessor()!=null){ System.out.println(getClass().getSimpleName()+",处理请求,并调用下一个处理者"); getSuccessor().process(); }else{ System.out.println(getClass().getSimpleName()+",仅处理,无下一处理者"); } } }
package design.pattern.responsibilitychain;
/** * Created by Aaron on 15/9/29. */ publicclassUser{ publicstaticvoidmain(String[] args){ Handler validate = new ValidateHandler(); Handler handler = new LoggerHandler(); validate.setSuccessor(handler); validate.process(); } }
/** * Created by Aaron on 15/9/29. */ publicclassUser{ publicstaticvoidmain(String[] args){ AudioPlay audioPlay = new AudioPlay(); PlayCommand playCommand = new PlayCommand(audioPlay); RewindCommand rewindCommand = new RewindCommand(audioPlay); StopCommand stopCommand = new StopCommand(audioPlay);