工厂模式
工厂方法模式分为三种:
一、普通工厂模式,建立一个工厂类,对实现了同一接口的一些类进行实例的创建。
1、创建公共接口
public interface Fruit { public void showPrice(); }
2、创建产品类,分别为AppleFactory.java和PearFactory.java
public class AppleFactory implements Fruit{ public void showPrice() { System.out.println("$1.1"); } } public class PearFactory implements Fruit{ public void showPrice() { System.out.println("pear price is $0.5"); } }
3、创建工厂类
public class SimpleFactoryMethod { public Fruit produce(String type) { if ("Apple".equals(type)) { return new AppleFactory(); } else if ("Pear".equals(type)) { return new PearFactory(); } else { System.out.println("输入正确的类型 "); return null; } } }
4、最后测试类
public class FactoryTest { public static void main(String[] args) { SimpleFactoryMethod factory = new SimpleFactoryMethod(); Fruit fruit = factory.produce("Apple"); fruit.showPrice(); } }
5、输出结果:$1.1
二、多工厂方法
多工厂方法就是用多个生产产品的方法代替type的分支
public class MultiFactoryMethod { public Fruit produceApple() { return new AppleFactory(); } public Fruit producePear() { return new PearFactory(); } }
三、静态工厂方法模式
将上面的多个工厂方法模式里面的方法设置为静态的,不需要创建工厂实例,直接调用即可。
public class MultiFactoryMethod { public static Fruit produceApple() { return new AppleFactory(); } public static Fruit producePear() { return new PearFactory(); } }
工厂模式适合有大量产品需要创建,并且具有共同接口时,可以通过工厂方法模式创建。以上三种模式中,第一种如果传入的type字符串有误,不能正确的创建对象,第三种相对于第二种就不需要实例化工厂类,故大多数情况下都是用第三种静态工厂方法模式。
四、工厂方法模式与抽象工厂模式(Abstract Factory)区别
工厂方法模式和抽象工厂模式不好分清楚,他们的区别如下:
工厂方法模式:
一个抽象产品类,可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类只能创建一个具体产品类的实例。
抽象工厂模式:
多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类可以创建多个具体产品类的实例,也就是创建的是一个产品线下的多个产品。
区别:
工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。
工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个。
工厂方法创建 "一种" 产品,他的着重点在于"怎么创建",也就是说如果你开发,你的大量代码很可能围绕着这种产品的构造,初始化这些细节上面。也因为如此,类似的产品之间有很多可以复用的特征,所以会和模版方法相随。
抽象工厂需要创建一些列产品,着重点在于"创建哪些"产品上,也就是说,如果你开发,你的主要任务是划分不同差异的产品线,并且尽量保持每条产品线接口一致,从而可以从同一个抽象工厂继承。
先看看工厂方法模式:
产品类:AppleProduct.java和 PearProduct.java
public interface Fruit { public void showName(); }
public class AppleProduct implements Fruit{ public void showName() { System.out.println("i am apple"); } }
public class PearProduct implements Fruit{ public void showName() { System.out.println("i am pear"); } }
工厂类:AppleFactory.java 、PearFactory.java、抽象接口Provider.java
public class AppleFactory implements Provider{ public Fruit produce() { return new AppleProduct(); } }
public class PearFactory implements Provider{ public Fruit produce() { return new PearProduct(); } }
public interface Provider { public Fruit produce(); }
我们看到一个工厂类只生产一个产品类,
测试代码如下:
public class Test { public static void main(String[] args) { Provider provider = new AppleFactory(); Fruit fruit = provider.produce(); fruit.showName(); } }
测试结果:
i am apple
ok,这样理解是不是很easy啦。
抽象工厂模式:
在农场系统的实现 //两种抽象产品:水果、蔬菜 public interface Fruit { } public interface Veggie { } //四种具体产品:北方水果,热带水果,北方蔬菜,热带蔬菜 //Northern Fruit public class NorthernFruit implements Fruit { private String name; public NorthernFruit(String name) { } public String getName() { return name; } public void setName(String name) { this. name = name; } } //TropicalFruit public class TropicalFruit implements Fruit { private String name; public TropicalFruit(String name) { } public String getName() { return name; } public void setName(String name) { this. name = name; } } //NorthernVeggie public class NorthernVeggie implements Veggie { private String name; public NorthernVeggie(String name) { } public String getName() { return name; } public void setName(String name) { this. name = name; } } //TropicalVeggie public class TropicalVeggie implements Veggie { private String name; public TropicalVeggie(String name) { } public String getName() { return name; } public void setName(String name) { this. name = name; } } //抽象工厂角色 public interface Gardener { public Fruit createFruit(String name); public Veggie createVeggie(String name); } //具体工厂角色:北方工厂,热带角色 public class NorthernGardener implements Gardener { public Fruit createFruit(String name) { return new NorthernFruit(name); } public Veggie createVeggie(String name) { return new NorthernVeggie(name); } } public class TropicalGardener implements Gardener { public Fruit createFruit(String name) { return new TropicalFruit(name); } public Veggie createVeggie(String name) { return new TropicalVeggie(name); } } 这样客户端只需要创建具体工厂的实例,然后调用工厂对象的工厂方法就可以得到所需要的产品对象。
浙公网安备 33010602011771号