工厂模式

/* * 工厂模式:工厂模式是一种创建型模式,目的是封装对象的创建过程,利用一个工厂类来“生产”对象,具体流程有工厂来负责。 * 工厂模式有三种:Simple Factory简单工厂模式,Factory Methods 工厂方法模式,Abstract Factory抽象工厂模式 *1)Simple Factory Pattern(简单工厂模式) *简单地把产生对象的代码封装成一个工厂类,具体生产出来的对象由参数决定,这个模式在创建对象时,利用的是switch或if语句等 *来做判断所生产的对象类型,由此使得该工厂类会涉及到了大量变动,当需要增加或减少一个类时,需要修改代码,随之带来将是很丑 *的代码。 *2)Factory Methods(工厂方法模式)就利用继承而不是利用判断语句实现,这种情况对工厂类修改不大,只需提供接口,具体类继承实现 *各自创建办法,而工厂类不需知道各类的创建。 起到隔离工厂类与实现类的作用。 *3)Abstract Factory(抽象工厂模式)提供了一个抽象工厂类,其他具体工厂类继承该抽象工厂类,实现多种产品的生产,而不是工厂方法提供 *的一种产品生产,即在生产产品的层次上加了一个抽象层。 * *在这章中提到了一个Dependency Inversion Principle(依赖倒置原则DIP) *:depend upon abstractions ,do not depend upon concrete classes *有点类似于Program to interface,not to implemention *提到的是依赖于抽象,不要依赖于具体类 *下面是DIP原则的重点: *A. HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND UPON ABSTRACTIONS. *B. ABSTRACTIONS SHOULD NOT DEPEND UPON DETAILS. DETAILS SHOULD DEPEND UPON ABSTRACTIONS. *当设计依赖于低层时,构造一个抽象层,使设计依赖于此抽象层,这样可以提高代码的独立性,提高复用性。 *另外对这个原则,有三点指导性意见(guidelines) *1.no variable should hold a reference to an concrete class; *2.no class should derived from concrete class; *3.no method should overide an implemented method of any of its base class; * * *还有一个就是:如果对(”生产产品“接口)creator(下面的creteBread())使用static ,确实可以保证用类来产生产品 *但是static不可以继承,这时缺点。 * *下面是练习的两个例子,一个简单工厂模式,一个是工厂方法模式,抽象工厂模式(暂缺) */ //1--------------------------Simple Factory Pattern---------------------------------- abstract class Bread{ private String _name; public Bread(String name) { _name = name; } public void prepare() { System.out.println("prepare for " + _name); } public void bake() { System.out.println("baking.... "); } public void cut() { System.out.println("cut 4 piece"); } public void pack() { System.out.println("packing......."); } } class BlackBread extends Bread{ public BlackBread(){ super("BlackBread"); } } class WhiteBread extends Bread{ public WhiteBread(){ super("WhiteBread"); } } class CheezeBread extends Bread{ public CheezeBread(){ super("CheezeBread"); } } class BreadFactory{ private Bread br; public static Bread createBread(String name){ //注意比较这个函数,在Factory Methods 与 Simple Factory的不同 if(name == "BlackBread") return new BlackBread(); else if(name == "WhiteBread") return new WhiteBread(); else return new CheezeBread(); } } public class BreadStore { /** * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 Bread br1 = BreadFactory.createBread("BlackBread"); br1.prepare(); br1.bake(); br1.cut(); br1.pack(); System.out.println("......................."); Bread br2 = BreadFactory.createBread("WhiteBread"); br2.prepare(); br2.bake(); br2.cut(); br2.pack(); } } //2-------------------------- Factory Methods Pattern---------------------------------- abstract class Bread{ String type; public Bread(String type){ this.type = type; } public void prepare(){ System.out.println("running prepare()" + type); } public void bake(){ System.out.println("running bake()" + type); } public void cut(){ System.out.println("running cut()" + type); } public void pack(){ System.out.println("running pack()" + type); } } class BJBlackBread extends Bread{ public BJBlackBread(){ super("BJBlackBread"); } } class BJWhiteBread extends Bread{ public BJWhiteBread(){ super("BJWhiteBread"); } } class GZBlackBread extends Bread{ public GZBlackBread(){ super("GZBlackBread"); } } class GZWhiteBread extends Bread{ public GZWhiteBread(){ super("GZWhiteBread"); } } abstract class AbstractFactory{ public abstract Bread createBread(String type); public void orderBread(String type){ Bread bread = createBread(type); bread.prepare(); bread.bake(); bread.cut(); bread.pack(); } } class BJFactory extends AbstractFactory{ public Bread createBread(String type){ Bread bread; if(type == "BJBlackBread") { bread = new BJBlackBread(); return bread; } else { bread = new BJWhiteBread(); return bread; } } } class GZFactory extends AbstractFactory{ public Bread createBread(String type){ Bread bread; if(type == "GZBlackBread") { bread = new GZBlackBread(); return bread; } else { bread = new GZWhiteBread(); return bread; } } } public class Factory_Test{ public static void main(String args[]){ AbstractFactory BJfactory = new BJFactory(); BJfactory.orderBread("BJBlackBread"); System.out.println("---------------------------------"); AbstractFactory GZfactory = new GZFactory(); GZfactory.orderBread("GZBlackBread"); } }

你可能感兴趣的:(String,Class,interface,reference,产品,methods)