设计模式总结.docx

上传人:b****8 文档编号:9767978 上传时间:2023-05-21 格式:DOCX 页数:54 大小:27.33KB
下载 相关 举报
设计模式总结.docx_第1页
第1页 / 共54页
设计模式总结.docx_第2页
第2页 / 共54页
设计模式总结.docx_第3页
第3页 / 共54页
设计模式总结.docx_第4页
第4页 / 共54页
设计模式总结.docx_第5页
第5页 / 共54页
设计模式总结.docx_第6页
第6页 / 共54页
设计模式总结.docx_第7页
第7页 / 共54页
设计模式总结.docx_第8页
第8页 / 共54页
设计模式总结.docx_第9页
第9页 / 共54页
设计模式总结.docx_第10页
第10页 / 共54页
设计模式总结.docx_第11页
第11页 / 共54页
设计模式总结.docx_第12页
第12页 / 共54页
设计模式总结.docx_第13页
第13页 / 共54页
设计模式总结.docx_第14页
第14页 / 共54页
设计模式总结.docx_第15页
第15页 / 共54页
设计模式总结.docx_第16页
第16页 / 共54页
设计模式总结.docx_第17页
第17页 / 共54页
设计模式总结.docx_第18页
第18页 / 共54页
设计模式总结.docx_第19页
第19页 / 共54页
设计模式总结.docx_第20页
第20页 / 共54页
亲,该文档总共54页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

设计模式总结.docx

《设计模式总结.docx》由会员分享,可在线阅读,更多相关《设计模式总结.docx(54页珍藏版)》请在冰点文库上搜索。

设计模式总结.docx

设计模式总结

 

1,简单工厂,工厂方法和抽象工厂模式

作者:

罗鹏Email:

luopeng@

对于简单工厂来说,它的工厂只能是这个样子的

publicclassSimplyFactory{

/**

*静态工厂方法

*/

publicstaticProuctfactory(Stringwhich)throwNoSuchProductExcption

{

if(which.equalIgnoreCase("product1"))

{

returnnewProduct1();

}

elseif(which.equalsIgnoreCase("product2"))

{

returnnewProduct2();

}

elseif(which.equalsIgnoreCase("product3"))

{

returnnewProduct3();

}

elsethrowNoSuchProductExcption("NoSuchProduct");

}

}

}

而对产品Product1,Product2,Product3,可以执行接口Product,也可以不执行接口Product(当然这样不好),这个Product接口只是用来抽象具体product用的

publicinterfaceProduct

{

voidproductMethod1();//这些只是

voidproductMethod2();

voidproductMethod3();

}

对工厂来说,只要有这么一种产品,一般来说就要在工厂里有它的生产的方法,否则抛出异常,而要工厂生产的话,也必须下达生产什么产品的命令,至少要向工厂发出信号,让工厂足以区分是要生产什么产品,否则工厂是不知道生产哪一种产品,

对于简单工厂来说,就是需要在工厂中枚举所有的产品,所以说简单工厂还是非常笨的。

 

if(which.equalIgnoreCase("product1"))只是用来区分生产什么产品的标记值,(也可以根据产品其它属性来判断,比如产品类型,产品大小,总之能够区分是什么产品的属性,或者是与产品属性相关的变量)或者说标记值是A,生产A产品,或者工厂里定义不是这样的,我偏偏要生产B产品,或者再特殊一些,我偏偏要生产A产品+B产品,那么就要returnnewProductA()+newProductB()了。

这样,我们就可以看出一个问题来,如果要能够被简单工厂生产出来,就必须在简单工厂中有能够生产出的它的方法定义,当然还需要有这个具体产品类的定义,就是有class对应,这样确保在简单工厂中new它的时候不会抛出NoSuchProduct的Exception.

 

对于工厂方法来说

实质上它是让工厂实现了抽象的工厂接口,它把具体怎么生产一种东西,放在具体的工厂去实现了,所谓”延迟到子类中实现“

publicinterfaceCreator

{

publicProuctfactory();

}

publicSubCreator1implentCreator

{

publicProuctfactory()

{

returnnewConcreteProduct1();

}

}

publicSubCreator2implentCreator

{

publicProuctfactory()

{

returnnewConcreteProduct2();

}

}

请注意:

返回类型是Product型的!

这样客户端调用是直接new一个具体工厂的实例,然后命令它去生产,而对于具体工厂的父类(既工厂接口,接口完全可以改成子类继承父类来实现,只是这样不好,不符合OO的原则),它完全不知道什么产品被生产了,甚至它连那个具体工厂被实例化它都不知道

抽象工厂模式

抽象工厂模式主要是用来解决具体产品是有几类产品簇的问题

publicinterfaceCreator

{

publicProuctAfactoryA();

publicProuctBfactoryB();

}

publicinterfaceProductA//ProductA接口

{

}

publicinterfaceProductB//ProductB接口

{

}

 

publicclassConCreator1implementsCreator

{

publicProuctAfactoryA()

{

returnnewConcreteProductA1();

}

publicProuctBfactoryB()

{

returnnewConcreteProductB1();

}

}

publicclassConCreator2implementsCreator

{

publicProuctAfactoryA()

{

returnnewProductA2();

}

publicProuctBfactoryB()

{

returnnewProductB2();

}

}

 

publicclassProductA1implementsProductA

{

publicProductA1()

{

}

}

publicclassProductA2implementsProductA

{

publicProductA2()

{

}

}

publicclassProductB1implementsProductB

{

publicProductB1()

{

}

}

publicclassProductB2implementsProductB

{

publicProductB2()

{

}

}

实际上是这样的

1,两个工厂类ConCreator1,ConCreator2都实现了Creator接口

2,ProuctA1,ProductA2都实现了ProductA接口

3,ProuctB1,ProductB2都实现了ProductB接口

4,ConCreator1负责生产1类型的产品(包括ProductA1,ProductB1)

5,ConCreator2负责生产2类型的产品(包括ProductA2,ProductB2)

6,工厂方法也有这样的特征,也就是说Creator不知道什么被生产出来,甚至不知道ConCreator1还是ConCreator2被实例化了,因为client高兴调那一个工厂,就调那一个工厂,就是说工厂能生产什么,对客户端是可见的。

甚至还有一种情况,客户端高兴起来就生产了ProductA1,我就不生产ProductA2,因为上面的例子中它们还都是松散的,没有绑定在一起

 

于是提出另外一个例子,也是老提起的电脑类型的例子

1,电脑生产商是接口,

2,CUP是接口,

3,硬盘是接口,

4,IBM工厂是制造IBM品牌的电脑的工厂

5,DELL工厂是制造DEll品牌的电脑的工厂

为讨论方便,就认为电脑=CUP+硬盘;

6,所以呀CUP有IBM的CPU和DELL的CPU

7,同样硬盘也是有IBM的硬盘和DELL的硬盘

8,IBM工厂生产IBM的CPU和IBM的硬盘,绝对不生产DELL的CPU,也不生产DELL的硬盘

9,同样DELL工厂也是一样

 

publicinterface电脑生产商

{

publicCPU制造CPU();

public硬盘制造硬盘();

public电脑制造电脑();

}

publicinterfaceCPU

{

}

publicinterface硬盘

{

}

publicclassIBM的CPUimplementsCPU

{

publicIBM的CPU();

}

publicclassIBM的硬盘implements硬盘

{

publicIBM的硬盘();

}

publicclassDELL的CPUimplementsCPU

{

publicDELL的CPU();

}

publicclassDELL的硬盘implements硬盘

{

publicDELL的硬盘();

}

//下面是IBM工厂

publicclassIBM工厂implements电脑生产商

{

privateCPUIBM的CPU私有变量=null;

private硬盘IBM的硬盘私有变量=null;

privateCPU制造CPU()

{

returnnewIBM的CPU();

}

private硬盘制造硬盘()

{

returnnewIBM的CPU();

}

public电脑制造电脑()

{

try{

IBM的CPU私有变量=this.制造CPU();

IBM的硬盘私有变量=this.制造硬盘();

if(IBM的CPU私有变量!

=null&&IBM的硬盘私有变量!

=null)

retrun(IBM的CPU私有变量+IBM的硬盘私有变量);

//组装成IBM电脑

}

catch(Exceptione)

{

System.out.println("制造IBM电脑失败!

");

}

}

}

}

这样,客户端无法通过命令单生产出一个CPU来,这样抽象才真正成为一个完整产品的工厂,只要向工厂发出生产的命令,一台完整的电脑就生产出来了,而工厂怎么生产的,生产了哪些部件,外界就看不见了,外界就知道这个工厂是生产IBM电脑整机的工厂!

DELL电脑工厂一样

/****下面来改错,请指出下面片段的错误*****/

publicabstractclassFactory{

  publicabstractSamplecreator();

 

  publicabstractSample2creator();

}

publicclassSimpleFactoryextendsFactory{

  publicSamplecreator(){

    ......

  }

  publicSample2creator(){

    ......

  }

 

}

publicclassBombFactoryextendsFactory{

  publicSamplecreator(){

    ......

  }

  publicSample2creator(){

    ......

  }

}

思考:

上面的代码错在哪?

/************改错结束***************/

2,builder模式的理解

作者:

罗鹏Email:

luopeng@

简单地说,就好象我要一座房子住,可是我不知道怎么盖(简单的砌墙,层次较低),也不知道怎么样设计(建几个房间,几个门好看,层次较高),于是我需要找一帮民工,他们会砌墙,还得找个设计师,他知道怎么设计,我还要确保民工听设计师的领导,而设计师本身也不干脏活,重活,光是下命令,这里砌一堵墙,这里砌一扇门,这样民工开始建设,最后,我可以向民工要房子了。

在这个过程中,设计师是什么也没有,除了他在脑子里的设计和命令,所以要房子也是跟民工要,记住了!

就象国内好多企业上erp一样,上erp,首先得找软件公司呀,找到软件公司后,软件公司说,我只知道怎么写软件,就知道怎么实现,不清楚整个erp的流程。

好,那我们还得找一个咨询公司,好,找到德勤了,德勤说好,我要软件怎么做,软件公司怎么做,我就能保证软件能为你们提供erp系统了,哈哈!

 

packagebuilder;

publicinterfaceBuilder

{

publicvoidmakeWindow();

publicvoidmakeFloor();

publicRoomgetRoom();

}

 

/*************************************************************/

packagebuilder;

publicclassDesigner{

publicDesigner(){

}

publicvoidorder(Builderbuilder)//这些下等人没有知识,没有文化,哈哈,得听我的

{

builder.makeWindow();

builder.makeFloor();

}

publicstaticvoidmain(String[]args){

Designerdesigner1=newDesigner();

}

}

/*************************************************************/

packagebuilder;

publicclassMingongimplementsBuilder{

privateStringwindow="";

privateStringfloor="";

publicMingong(){

}

publicstaticvoidmain(String[]args){

Mingongmingong1=newMingong();

}

publicvoidmakeWindow(){

window=newString("window");

}

publicvoidmakeFloor(){

floor=newString("floor");

}

publicRoomgetRoom()

{

if((!

window.equals(""))&&(!

floor.equals("")))

{

System.out.println("roomready!

");

returnnewRoom();

}

elsereturnnull;

}

}

/*************************************************************/

packagebuilder;

publicclassRoom{

privateStringwindow="";

privateStringfloor="";

publicRoom(){

}

publicstaticvoidmain(String[]args){

Roomroom1=newRoom();

}

}

/*************************************************************/

packagebuilder;

publicclassClient{

publicClient(){

}

publicstaticvoidmain(String[]args){

Buildermingong=newMingong();

Designerdesigner=newDesigner();

designer.order(mingong);

mingong.getRoom();

}

}

3,适配器模式的理解

作者:

罗鹏email:

luopeng@

publicclasswindow的软件

{

publicvoidrun(){

System.out.print("我运行在window上");

}

publicvoidrun1(){

System.out.print("我运行在window上的run1方法");

}

}

publicclasslinux的软件

{

publicvoidrun(){

System.out.print("我运行在linux上");

}

}

publicclass适配器extendswindow的软件

{

privatelinux的软件软件1;//实际上是聚合的使用

public适配器(linux的软件软件变量)

{

this.软件1=软件变量;

}

publicvoidrun()

{

软件1.run();

}

publicvoidrun1()

{

System.out.print(“已经被adpter改写了”);

}

}

客户段

publicclasstest{

publicstaticvoidmain(Stringargs[])

{

linux的软件test1=newlinux的软件();

适配器test2=new适配器(test1);

test2.run();

test2.run1();

}

 

4,Memento模式理解

作者:

罗鹏email:

luopeng@

Memento模式说的明白一点,就是事情太多,你一个人记不住,找一个人专门帮你记住,用的时候去问一下。

就象董事长和秘书一样

publicclassManage{

publicStringnext_week;//下个星期的安排

publicStringn_next_week;///下个星期的安排

publicManage(){

}

publicAssistantgetMemento()

{

returnnewAssistant(this);

}

publicvoidsetMemento(Assistantassistant)

{

next_week=assistant.next_week;

n_next_week=assistant.n_next_week;

}

}

publicclassAssistantimplementsjava.io.Serializable{

publicStringnext_week;

publicStringn_next_week;

publicAssistant(Managemanage)

{

next_week=manage.next_week;

n_next_week=manage.n_next_week;

}

}

 

publicclassTest{

publicTest(){

}

publicstaticvoidmain(String[]args){

Managemanage=newManage();

manage.next_week="见客户";

manage.n_next_week="见大客户";

System.out.println(manage.next_week);

System.out.println(manage.n_next_week);

Assistantassistant=manage.getMemento();

manage.next_week="见1111客户";

manage.n_next_week="见111111大客户";

System.out.println(manage.next_week);

System.out.println(manage.n_next_week);

manage.setMemento(assistant);

System.out.println(manage.next_week);

System.out.println(manage.n_next_week);

}

}

 

5,state模式理解

作者:

罗鹏email:

luopeng@

主要是用于状态的变化,就象红绿灯一样

如何使用state模式

1,要一个状态管理类

2,状态接口

3,各种子状态实现状态接口

State模式的效果

1)它将与特定状态的行为局部化,并且将不同的行为分割开来

2)它使得状态转化显式化

3)State对象可以被共享

state模式中谁定义状态转化?

state模式中并没有指定哪一个参与者定义状态转换准则。

换句话说,它们可以在Context中定义,也可以在state子类自身指定她们的后继状态以及何时进行转换,后者通常更灵活。

publicinterfaceState{

publicabstractvoidhandleGreen(Statestate);

publicabstractvoidhandleRed(Statestate);

publicabstractStategetColor();

}

publicclassManage{

privateStatestate=null;

publicManage(Statestate)

{

this.state=state;

}

publicvoidchangeToGreen()

{

this.state=newGreenLight();

state.handleGreen(state);

}

publicvoidchangeToRed()

{

this.state=newRedLight();

state.handleRed(state);

}

}

publicclassGreenLightimplementsState{

publicStatestate=null;

publicStategetColor()

{

returnstate;

}

publicvoidhandleGreen(Statestate)

{

System.out.println("绿灯,前进");

}

publicvoidhandleRed(Statestate)

{

System.out.println(",");

}

}

publicclassRedLightimplementsState{

publicStatestate=null;

publicStategetColor()

{

returnstate;

}

publicvoidhandleGreen(States)

{

System.out.println(",cc");

}

publicvoidhandleRed(States)

{

System.ou

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2