本文共 4796 字,大约阅读时间需要 15 分钟。
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
顾名思义,这个模式本身很简单,而且使用在业务较简单的情况下。
它由三种角色组成(关系见下面的类图):
创建一个产品接口:
package com.siyi.simplefactory;public interface Shape { void draw();}
创建产品类:
package com.siyi.simplefactory;public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); }}
package com.siyi.simplefactory;public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); }}
package com.siyi.simplefactory;public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); }}
创建一个工厂类:
package com.siyi.simplefactory;public class ShapeFactory { //使用 getShape 方法获取形状类型的对象 public static Shape getShape(String shapeType) { if (shapeType == null) { return null; } if (shapeType.equalsIgnoreCase("CIRCLE")) { return new Circle(); } else if (shapeType.equalsIgnoreCase("RECTANGLE")) { return new Rectangle(); } else if (shapeType.equalsIgnoreCase("SQUARE")) { return new Square(); } return null; }}
Main方法执行查看效果:
package com.siyi.simplefactory;public class Main { public static void main(String[] args) { //获取 Circle 的对象,并调用它的 draw 方法 Shape shape1 = ShapeFactory.getShape("CIRCLE"); //调用 Circle 的 draw 方法 shape1.draw(); //获取 Rectangle 的对象,并调用它的 draw 方法 Shape shape2 = ShapeFactory.getShape("RECTANGLE"); //调用 Rectangle 的 draw 方法 shape2.draw(); //获取 Square 的对象,并调用它的 draw 方法 Shape shape3 = ShapeFactory.getShape("SQUARE"); //调用 Square 的 draw 方法 shape3.draw(); }}
运行结果:
Inside Circle::draw() method.Inside Rectangle::draw() method.Inside Square::draw() method.
优点:
缺点:
工厂方法模式去掉了简单工厂模式中工厂方法的静态属性,使得它可以被子类继承。这样在简单工厂模式里集中在工厂方法上的压力可以由工厂方法模式里不同的工厂子类来分担。
工厂方法模式组成:
工厂方法模式使用继承自抽象工厂角色的多个子类来代替简单工厂模式中的“上帝类”。正如上面所说,这样便分担了对象承受的压力;而且这样使得结构变得灵活 起来——当有新的产品产生时,只要按照抽象产品角色、抽象工厂角色提供的合同来生成,那么就可以被客户使用,而不必去修改任何已有 的代码。可以看出工厂角色的结构也是符合开闭原则的!
生产不同产品工厂的抽象类:
package com.siyi.factorymethod;public interface PhoneFactory { Phone makePhone();}
生产手机的工厂(ConcreteFactory):
package com.siyi.factorymethod;public class ApplePhoneFactory implements PhoneFactory { @Override public Phone makePhone() { return new IPhone(); }}--------------------------------------------------------------package com.siyi.factorymethod;public class XiaoMiPhoneFactory implements PhoneFactory { @Override public Phone makePhone() { return new XiaoMi(); }}
具体手机对应的类:
package com.siyi.factorymethod;public class XiaoMi implements Phone { public XiaoMi() { } @Override public void show() { // TODO Auto-generated method stub System.out.println("make xiaomi phone!"); }}-------------------------------------------------------package com.siyi.factorymethod;public class IPhone implements Phone { public IPhone() { } @Override public void show() { // TODO Auto-generated method stub System.out.println("make iphone!"); }}
Main:
package com.siyi.factorymethod;public class Main { public static void main(String[] arg) { Phone xiaoMiPhone = new XiaoMiPhoneFactory().makePhone(); Phone applePhone = new ApplePhoneFactory().makePhone(); xiaoMiPhone.show(); applePhone.show(); }}
运行结果:
make xiaomi phone!make iphone!
优点:
缺点:
转载地址:http://iryof.baihongyu.com/