您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > java设计模式示例
java设计模式示例创建模式1.工厂方法模式(FactoryMethod)将程序中创建对象的操作,单独出来处理,创建一个产品的工厂接口,把实际的工作转移到具体的子类。大大提高了系统扩展的柔性,接口的抽象化处理给相互依赖的对象创建提供了最好的抽象模式。[java]viewplaincopy1.publicclassTestFactoryMethod{2.3.publicstaticvoidmain(String[]args){4.5.AnimalFactoryaf=newDogFactory();6.7.Animal1a=af.getAnimal();8.9.}10.11.}12.13.abstractclassAnimal1{}14.15.classDog1extendsAnimal1{}16.17.classCat1extendsAnimal1{}18.19.20.21.abstractclassAnimalFactory{22.23.publicabstractAnimal1getAnimal();24.25.}26.27.classDogFactoryextendsAnimalFactory{28.29.publicAnimal1getAnimal(){30.31.System.out.println(Dog);32.33.returnnewDog1();34.35.}36.37.}38.39.classCatFactoryextendsAnimalFactory{40.41.publicAnimal1getAnimal(){42.43.System.out.println(Cat);44.45.returnnewCat1();46.47.}48.49.}2.抽象工厂模式(AbstractFactory)针对多个产品等级的情况,而工厂方法模式针对单一产品等级的情况。[java]viewplaincopy1.importjava.awt.*;2.3.importjavax.swing.*;4.5.importjava.awt.event.*;6.7.publicclassTestAbstractFactory{8.9.publicstaticvoidmain(String[]args){10.11.GUIFactoryfact=newSwingFactory();12.13.Framef=fact.getFrame();14.15.Componentc1=fact.getButton();16.17.Componentc2=fact.getTextField();18.19.20.21.f.setSize(500,300);22.23.f.setLayout(newFlowLayout());24.25.f.add(c1);26.27.f.add(c2);28.29.f.setVisible(true);30.31.32.33.f.addWindowListener(newWindowAdapter(){34.35.publicvoidwindowClosing(WindowEvente){36.37.System.exit(0);38.39.}40.41.});42.43.}44.45.}46.47.abstractclassGUIFactory{48.49.publicabstractComponentgetButton();50.51.publicabstractComponentgetTextField();52.53.publicabstractFramegetFrame();54.55.}56.57.classAWTFactoryextendsGUIFactory{58.59.publicComponentgetButton(){60.61.returnnewButton(AWTButton);62.63.}64.65.publicFramegetFrame(){66.67.returnnewFrame(AWTFrame);68.69.}70.71.publicComponentgetTextField(){72.73.returnnewTextField(20);74.75.}76.77.78.79.}80.81.classSwingFactoryextendsGUIFactory{82.83.publicComponentgetButton(){84.85.returnnewJButton(SwingButton);86.87.}88.89.publicFramegetFrame(){90.91.returnnewJFrame(SwingFrame);92.93.}94.95.publicComponentgetTextField(){96.97.returnnewJTextField(20);98.99.}100.101.}3.单例模式(Singleton)改善全局变量和命名空间的冲突,可以说是一种改良了的全局变量。这种一个类只有一个实例,且提供一个访问全局点的方式,更加灵活的保证了实例的创建和访问约束。系统中只有一个实例,因此构造方法应该为私有饿汉式:类加载时直接创建静态实例懒汉式:第一次需要时才创建一个实例,那么newInstance方法要加同步饿汉式比懒汉式要好,尽管资源利用率要差。但是不用同步。[java]viewplaincopy1.publicclassTestSingleton{2.3.publicstaticvoidmain(String[]args){4.5.6.7.}8.9.}10.11.classClassA{//饿汉式12.13.privatestaticClassAi=newClassA();14.15.publicstaticClassAnewInstance(){16.17.returni;18.19.}20.21.privateClassA(){}22.23.}24.25.classClassB{//懒汉式26.27.privatestaticClassBi=null;28.29.publicstaticsynchronizedClassBnewInstance(){30.31.if(i==null)i=newClassB();32.33.returni;34.35.}36.37.privateClassB(){}38.39.}4.建造模式(Builder)将一个对象的内部表象和建造过程分割,一个建造过程可以造出不同表象的对象。可简化为模版方法模式.[java]viewplaincopy1.publicclassTestBuilder{2.3.publicstaticvoidmain(String[]args){4.5.Builderb=newBuilderImpl1();6.7.Directord=newDirector(b);8.9.Productp=d.createProduct();10.11.}12.13.14.15.}16.17.interfaceBuilder{18.19.voidbuildPart1();20.21.voidbuildPart2();22.23.voidbuildPart3();24.25.ProductgetProduct();26.27.}28.29.classBuilderImpl1implementsBuilder{30.31.32.33.publicvoidbuildPart1(){34.35.System.out.println(createpart1);36.37.}38.39.40.41.publicvoidbuildPart2(){42.43.System.out.println(createpart2);44.45.}46.47.48.49.publicvoidbuildPart3(){50.51.System.out.println(createpart3);52.53.}54.55.56.57.publicProductgetProduct(){58.59.returnnewProduct();60.61.}62.63.64.65.}66.67.68.69.classDirector{70.71.Builderb;72.73.publicDirector(Builderb){74.75.this.b=b;76.77.}78.79.publicProductcreateProduct(){80.81.b.buildPart1();b.buildPart2();82.83.b.buildPart3();84.85.returnb.getProduct();86.87.}88.89.}90.91.classProduct{}5.原型模式(ProtoType)通过一个原型对象来创建一个新对象(克隆)。Java中要给出Clonable接口的实现,具体类要实现这个接口,并给出clone()方法的实现细节,这就是简单原型模式的应用。浅拷贝:只拷贝简单属性的值和对象属性的地址深拷贝:拷贝本对象引用的对象,有可能会出现循环引用的情况。可以用串行化解决深拷贝。写到流里再读出来,这时会是一个对象的深拷贝结果。[java]viewplaincopy1.importjava.io.*;2.3.publicclassTestClonealbe{4.5.publicstaticvoidmain(String[]args)throwsException{6.7.Fatherf=newFather();8.9.10.11.Useru1=newUser(123456,f);12.13.Useru2=(User)u1.clone();14.15.System.out.println(u1==u2);16.17.System.out.println(u1.f==u2.f);18.19.}20.21.}22.23.classUserimplementsCloneable,Serializable{24.25.Stringpassword;26.27.Fatherf;28.29.publicUser(Stringpassword,Fatherf){30.31.this.password=password;32.33.this.f=f;34.35.}36.37.publicObjectclone()throwsCloneNotSupportedException{38.39.//returnsuper.clone();40.41.ObjectOutputStreamout=null;42.43.ObjectInputStreamin=null;44.45.try{46.47.ByteArrayOutputStreambo=newByteArrayOutputStream();48.49.out=newObjectOutputStream(bo);50.51.out.writeObject(this);52.53.out.flush();54.55.byte[]bs=bo.toByteArray();56.57.58.59.ByteArrayInputStreambi=newByteArrayInputStream(bs);60.61.in=newObjectInputStream(bi);62.63.Objecto=in.readObject();64.65.66.67.returno;68.69.}catch(IOExceptione){70.71.e.printStackTrace();72.73.returnnull;74.75.}catch(ClassNotFoundExceptione){76.77.e.printStackTrace();78.79.returnnull;80.81.}82.83.finally{84.85.try
本文标题:java设计模式示例
链接地址:https://www.777doc.com/doc-6499781 .html