北京尚学堂-cctv央视网广告合作伙伴,专业IT培训机构,口碑最好的java培训、,iOS培训,android培训,大数据培训北京尚学堂提供生产模式1.工厂方法模式(FactoryMethod)将程序中创建对象的操作,单独出来处理,创建一个产品的工厂接口,把实际的工作转移到具体的子类。大大提高了系统扩展的柔性,接口的抽象化处理给相互依赖的对象创建提供了最好的抽象模式。[java]viewplaincopy1.publicclassTestFactoryMethod{2.publicstaticvoidmain(String[]args){3.AnimalFactoryaf=newDogFactory();4.5.Animal1a=af.getAnimal();6.}7.}8.abstractclassAnimal1{}9.10.classDog1extendsAnimal1{}11.12.classCat1extendsAnimal1{}13.14.15.16.abstractclassAnimalFactory{17.18.publicabstractAnimal1getAnimal();19.}20.classDogFactoryextendsAnimalFactory{21.publicAnimal1getAnimal(){22.System.out.println(Dog);23.returnnewDog1();24.}25.}26.classCatFactoryextendsAnimalFactory{27.publicAnimal1getAnimal(){28.System.out.println(Cat);29.returnnewCat1();30.}31.}北京尚学堂-cctv央视网广告合作伙伴,专业IT培训机构,口碑最好的java培训、,iOS培训,android培训,大数据培训2.抽象工厂模式(AbstractFactory)针对多个产品等级的情况,而工厂方法模式针对单一产品等级的情况。[java]viewplaincopy1.importjava.awt.*;2.importjavax.swing.*;3.importjava.awt.event.*;4.publicclassTestAbstractFactory{5.publicstaticvoidmain(String[]args){6.GUIFactoryfact=newSwingFactory();7.Framef=fact.getFrame();8.Componentc1=fact.getButton();9.Componentc2=fact.getTextField();10.f.setSize(500,300);11.f.setLayout(newFlowLayout());12.f.add(c1);13.f.add(c2);14.f.setVisible(true);15.f.addWindowListener(newWindowAdapter(){16.publicvoidwindowClosing(WindowEvente){17.System.exit(0);18.}19.});20.abstractclassGUIFactory{21.publicabstractComponentgetButton();22.publicabstractComponentgetTextField();23.publicabstractFramegetFrame();24.}25.classAWTFactoryextendsGUIFactory{26.publicComponentgetButton(){27.returnnewButton(AWTButton);28.}publicFramegetFrame(){returnnewFrame(AWTFrame);}publicComponentgetTextField(){29.returnnewTextField(20);30.classSwingFactoryextendsGUIFactory{31.publicComponentgetButton(){32.returnnewJButton(SwingButton);33.}34.publicFramegetFrame(){35.returnnewJFrame(SwingFrame);}36.publicComponentgetTextField(){37.returnnewJTextField(20);北京尚学堂-cctv央视网广告合作伙伴,专业IT培训机构,口碑最好的java培训、,iOS培训,android培训,大数据培训38.}39.}3.单例模式(Singleton)改善全局变量和命名空间的冲突,可以说是一种改良了的全局变量。这种一个类只有一个实例,且提供一个访问全局点的方式,更加灵活的保证了实例的创建和访问约束。系统中只有一个实例,因此构造方法应该为私有饿汉式:类加载时直接创建静态实例懒汉式:第一次需要时才创建一个实例,那么newInstance方法要加同步饿汉式比懒汉式要好,尽管资源利用率要差。但是不用同步。[java]viewplaincopy1.publicclassTestSingleton{2.3.publicstaticvoidmain(String[]args){4.}5.}6.classClassA{//饿汉式7.8.privatestaticClassAi=newClassA();9.10.publicstaticClassAnewInstance(){11.12.returni;13.}14.privateClassA(){}15.}16.classClassB{//懒汉式17.privatestaticClassBi=null;18.publicstaticsynchronizedClassBnewInstance(){19.if(i==null)i=newClassB();20.returni;21.}22.privateClassB(){}23.}4.建造模式(Builder)将一个对象的内部表象和建造过程分割,一个建造过程可以造出不同表象的对象。可简化为模版方法模式.[java]viewplaincopy1.publicclassTestBuilder{2.3.publicstaticvoidmain(String[]args){北京尚学堂-cctv央视网广告合作伙伴,专业IT培训机构,口碑最好的java培训、,iOS培训,android培训,大数据培训4.5.Builderb=newBuilderImpl1();6.7.Directord=newDirector(b);8.9.Productp=d.createProduct();10.11.}12.}13.14.interfaceBuilder{15.voidbuildPart1();16.voidbuildPart2();17.voidbuildPart3();18.ProductgetProduct();19.20.}21.classBuilderImpl1implementsBuilder{22.publicvoidbuildPart1(){23.System.out.println(createpart1);24.}25.publicvoidbuildPart2(){26.System.out.println(createpart2);27.}28.publicvoidbuildPart3(){29.System.out.println(createpart3);30.}31.32.publicProductgetProduct(){33.returnnewProduct();34.}35.}36.37.classDirector{38.Builderb;39.publicDirector(Builderb){40.this.b=b;41.}42.publicProductcreateProduct(){43.44.b.buildPart1();b.buildPart2();45.46.b.buildPart3();北京尚学堂-cctv央视网广告合作伙伴,专业IT培训机构,口碑最好的java培训、,iOS培训,android培训,大数据培训47.48.returnb.getProduct();49.}50.}51.classProduct{}5.原型模式(ProtoType)深拷贝:拷贝本对象引用的对象,有可能会出现循环引用的情况。可以用串行化解决深拷贝。写到流里再读出来,这时会是一个对象的深拷贝结果。浅拷贝:只拷贝简单属性的值和对象属性的地址通过一个原型对象来创建一个新对象(克隆)。Java中要给出Clonable接口的实现,具体类要实现这个接口,并给出clone()方法的实现细节,这就是简单原型模式的应用。[java]viewplaincopy1.importjava.io.*;2.publicclassTestClonealbe{3.4.publicstaticvoidmain(String[]args)throwsException{5.6.Fatherf=newFather();7.Useru1=newUser(123456,f);8.9.Useru2=(User)u1.clone();10.11.System.out.println(u1==u2);12.13.System.out.println(u1.f==u2.f);14.}15.}16.classUserimplementsCloneable,Serializable{17.Stringpassword;18.Fatherf;19.publicUser(Stringpassword,Fatherf){20.this.password=password;21.this.f=f;22.}23.publicObjectclone()throwsCloneNotSupportedException{24.//returnsuper.clone();25.ObjectOutputStreamout=null;26.ObjectInputStreamin=null;27.try{28.ByteArrayOutputStreambo=newByteArrayOutputStream();29.out=newObjectOutputStream(bo);北京尚学堂-cctv央视网广告合作伙伴,专业IT培训机构,口碑最好的java培训、,iOS培训,android培训,大数据培训30.out.writeObject(this);31.out.flush();32.byte[]bs=bo.toByteArray();33.ByteArrayInputStreambi=newByteArrayInputStream(bs);34.in=newObjectInputStream(bi);35.Objecto=in.readObject();36.returno;37.38.}catch(IOExceptione){39.e.printStackTrace();40.returnnull;41.}catch(ClassNotFoundExceptione){42.e.printStackTrace();43.returnnull;44.}45.finally{46.try{47.out.close();48.in.close();49.}catch(IOExceptione){50.e.printStackTrace();51.}52.}53.}54.}55.classFatherimplementsSerializable{}结构模式如何把简单的类根据某种结构组装为大的系统6