Java创建对象的方法•所有面向对象的语言都有固定的创立对象的办法。java的办法就是使用new操作符。比如StringBuffers=newStringBuffer(1000);•使用new操作符的短处是事先必须明确知道要实例化的类是什么•实例化的责任往往与使用实例的责任不加区分。创立性模式•面向对象的设计的目的之一,就是把责任进行划分,以分派给不同的对象。•创立性模式把对象的创立过程封装起来,使得创立实例的责任与使用实例的责任分割开•由专门的模块分管实例的创立,而系统在宏观上不再依赖于对象创立过程的细节创立性模式•创立性模式将类实例化,不必事先知道每次是要实例化哪一个类•把实例化的责任与使用实例的责任分割开来工厂模式•工厂模式就是专门负责将大量有共同接口的类实例化,而且不必事先知道每次是要实例化哪一个类的模式。•工厂模式有以下几种形态:简单工厂(SimpleFactory)模式工厂方法(FactoryMethod)模式抽象工厂(AbstractFactory)模式简单工厂(SimpleFactory)模式•比如说,你有一个描述你的后花园的系统,在你的后花园里有各种的花,但还没有水果。你现在要往你的系统里引进一些新的类,用来描述下列的水果:葡萄Grapes草莓Strawberry萍果Apple简单工厂(SimpleFactory)模式源代码源代码源代码源代码简单工厂(SimpleFactory)模式•作为小花果园的主人兼园丁,也是系统的一部分,自然要由一个合适的类来代表,这个类就是FruitGardener类。源代码园丁的工作(简单工厂)•FruitGardener类会根据要求,创立出不同的水果类,比如萍果Apple,葡萄Grape或草莓Strawberry的实例。•这里的园丁就如同一个可以创建水果产品的工厂一样•如果接到不合法的要求,FruitGardener类会给出例外BadFruitException。源代码丰收的果园(客户端)•在使用时,只须呼叫FruitGardener的factory()方法即可•try{FruitGardenergardener=newFruitGardener();FruitIFgrape=gardener.factory(grape);FruitIFapple=gardener.factory(apple);FruitIFstrawberry=gardener.factory(strawberry);...}catch(BadFruitExceptione){...}小结:简单工厂模式的定义•总而言之,简单工厂模式就是由一个工厂类根据参数来决定创立出那一种产品类的实例。小结:简单工厂模式框架的源代码publicclassCreator{publicProductfactory(){returnnewConcreteProduct();}}publicinterfaceProduct{}publicclassConcreteProductimplementsProduct{publicConcreteProduct(){}}源代码:FruitIF接口•接口FruitIF的源代码。这个接口确定了水果类必备的方法:种植plant(),生长grow(),以及收获harvest()。•packagecom.javapatterns.simplefactory;publicinterfaceFruitIF{voidgrow();voidharvest();voidplant();Stringcolor=null;Stringname=null;}返回源代码:Apple类•萍果是多年生木本植物,因此具备树龄treeAge性质•packagecom.javapatterns.simplefactory;publicclassAppleimplementsFruitIF{publicvoidgrow(){log(Appleisgrowing...);}publicvoidharvest(){log(Applehasbeenharvested.);}publicvoidplant(){log(Applehasbeenplanted.);}publicstaticvoidlog(Stringmsg){System.out.println(msg);}publicintgetTreeAge(){returntreeAge;}publicvoidsetTreeAge(inttreeAge){this.treeAge=treeAge;}privateinttreeAge;}返回源代码:Grape类•葡萄分为有籽与无籽两种,因此具有seedful性质•packagecom.javapatterns.simplefactory;publicclassGrapeimplementsFruitIF{publicvoidgrow(){log(Grapeisgrowing...);}publicvoidharvest(){log(Grapehasbeenharvested.);}publicvoidplant(){log(Grapehasbeenplanted.);}publicstaticvoidlog(Stringmsg){System.out.println(msg);}publicbooleangetSeedful(){returnseedful;}publicvoidsetSeedful(booleanseedful){this.seedful=seedful;}privatebooleanseedful;}返回源代码:Strawberry类•packagecom.javapatterns.simplefactory;publicclassStrawberryimplementsFruitIF{publicvoidgrow(){log(Strawberryisgrowing...);}publicvoidharvest(){log(Strawberryhasbeenharvested.);}publicvoidplant(){log(Strawberryhasbeenplanted.);}publicstaticvoidlog(Stringmsg){System.out.println(msg);}}返回源代码:FruitGardener类•packagecom.javapatterns.simplefactory;publicclassFruitGardener{publicFruitIFfactory(Stringwhich)throwsBadFruitException{if(which.equalsIgnoreCase(apple)){returnnewApple();}elseif(which.equalsIgnoreCase(strawberry)){returnnewStrawberry();}elseif(which.equalsIgnoreCase(grape)){returnnewGrape();}else{thrownewBadFruitException(Badfruitrequest);}}}返回源代码:BadFruitException类•packagecom.javapatterns.simplefactory;publicclassBadFruitExceptionextendsException{publicBadFruitException(Stringmsg{super(msg);}}返回