软件体系结构与设计模式期末复习(8)

2019-01-07 19:18

publicclass BigCup implements TeaSize {

publicdouble price() {

return 4; }}

SmallCup类

publicclass SmallCup implements TeaSize {

publicdouble price() {

return 2; }}

Client主类

publicclass Client {

publicstaticvoid main(String[] args) {

Teakind tk=null; TeaSize ts=null; tk=new BlackTea(); ts=new BigCup(); tk.setTeaSize(ts);

double price=tk.getprice();

System.out.println(\中杯红茶的价格是\+price); tk=new GreenTea(); ts=new BigCup(); tk.setTeaSize(ts);

double price1=tk.getprice();

System.out.println(\大杯绿茶的价格是\+price1); }}

访问者模式:作用于某对象结构中的各元素的操作,它使我们可以在不变各元素的类的前提下定义,这些元素的新操作

Child类

publicabstractclass Child {

publicabstractvoidAccept(Visitor visitor); }

Usachild类

publicclass USAchild extends Child{

publicvoid Accept(Visitor visitor) {

visitor.visitUSA(this); }}

Chinachild类

publicclassChinachildextends Child{

publicvoid Accept(Visitor visitor) {

visitor.visitChina(this);

}}

Vistor类

publicabstractclassVisitor {

publicabstractvoid visitChina(Chinachild c); publicabstractvoid visitUSA(USAchild u);}

Weightvistor类

publicclassWeightVisitorextends Visitor{

publicvoid visitChina(Chinachild c) {

System.out.println(\中国孩子体重标准是60kg\); publicvoid visitUSA(USAchild u) {

System.out.println(\美国孩子体重标准是70kg\);

} }}

Heightvistor类

publicclass HeightVisitor extends Visitor{

publicvoid visitChina(Chinachild c) {

System.out.println(\中国孩子身高标准是175cm\); } publicvoid visitUSA(USAchild u) {

System.out.println(\美国孩子身高标准是180cm\); }}

ObjectStructure类

publicclassObjectStructure {

private ArrayListlist=new ArrayList(); publicvoid add(Child c){

list.add(c);} list.remove(c);} for(Child c:list){

c.Accept(v); }}} publicvoid remove(Child c){ publicvoid accept(Visitor v){

主类

publicclass Client {

publicstaticvoid main(String[] args) {

ObjectStructure o=new ObjectStructure(); o.add(new Chinachild());

o.add(new USAchild());

HeightVisitor h=new HeightVisitor(); WeightVisitor w=new WeightVisitor(); o.accept(h); o.accept(w);

}}

观察者模式:对象间的一种1对n的依赖关系,当一个对象的状态发生变化时,所有依赖于它的随想得到通知被更新。

Observer类(观察者)

publicinterfaceObserver {

publicvoid update(Subject obj);}

Subject类(被观察者)

publicabstractclass Subject {

private ArrayListlist=new ArrayList(); publicvoid attach(Observer o){

list.add(o);

System.out.println(\添加新的观察者\);} publicvoidremove(Observer o){

list.remove(o); }

for(Observer observer:list){

observer.update(this); }}} publicvoid notifyObserver(){

ConcreteObserver类

publicclass Concretorobersver implements Observer { String info;

publicvoid update(Subject obj) {

ConcretorSubject cs=new ConcretorSubject(); info=cs.getinfo();

System.out.println(\状态为:\+info); }}

ConcreteSubject类

publicclass ConcretorSubject extends Subject { String info;

public String getinfo(){

returninfo;} info=state;

System.out.println(\改变状态为:\+info); this.notifyObserver();}} publicvoidchange(String state){

主类

publicclass Client {

publicstaticvoid main(String[] args) { ConcretorSubject con=new ConcretorSubject(); Observer c=new Concretorobersver(); con.attach(c); con.change(\);

}}

代理模式:

IGamePlayer接口

publicinterface IGamePlayer {

publicvoid login(String user,String password); publicvoid killBoss(); publicvoid upgrade();}

GamePlayProxy类

publicclass GamePlayerProxy implements IGamePlayer {

private IGamePlayer gamePlayer=null;

public GamePlayerProxy(String name){ try{ } }

this.gamePlayer.killBoss(); }

this.gamePlayer.login(\,\); } this.gamePlayer.upgrade(); }}

gamePlayer =new GamePlayer(gamePlayer, name);

}catch (Exception e){

publicvoid login(String user, String password) { publicvoid killBoss() { publicvoid upgrade() {

GamePlayer类

publicclass GamePlayer implements IGamePlayer {

private String name=\;

public GamePlayer(IGamePlayer _gamePlayer,String _name)throws

if(_gamePlayer==null){

thrownew Exception(\不能创建真实角色!\); }else{

this.name=name;

} }

Exception{

publicvoid login(String user, String password) {

System.out.println(\登录名为\+user+\的用户\+this.name+\登陆成功!”); } publicvoid killBoss() {

System.out.println(this.name+\在打怪!\);

}

publicvoid upgrade() { System.out.println(this.name+\又升了一级!”);}}

主类

publicclass client {

publicstaticvoid main(String[] args) {

IGamePlayer proxy=new GamePlayerProxy(\张三\); System.out.println(\开始时间是:2010-10-10 10:10\); proxy.login(\, \); proxy.killBoss(); proxy.upgrade();

System.out.println(\结束时间是:2010-10-19 15:10\);

}}

组织模式:将对象组合成树形成以表现“部分整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。


软件体系结构与设计模式期末复习(8).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:新教材苏教版二年级下册第六单元两三位数的加法和减法

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: