简单工厂和工厂方法模式 -- 小案例

简单工厂

 

1 public interface Fruit {
2     public void pro(); //生产水果
3     public void eat(); //吃水果
4 }

1 public class Apple implements Fruit {
2     public void pro() {
3         System.out.println("生产苹果");
4     }
5 
6     public void eat() {
7         System.out.println("吃苹果");
8     }
9 }


1 public class Banana implements Fruit {
2     public void pro() {
3         System.out.println("生产香蕉");
4     }
5 
6     public void eat() {
7         System.out.println("吃香蕉");
8     }
9 }

 

 1 public class SimpleFactory_1 {
 2 
 3     public static Fruit getFruit(String message){
 4         if("apple".equalsIgnoreCase(message)){
 5             return new Apple();
 6         }else if("banana".equalsIgnoreCase(message)){
 7             return new Banana();
 8         }else{
 9             return null;
10         }
11     }
12 
13     public static void main(String[] args) {
14         Fruit apple = getFruit("banana");
15         apple.pro();
16         apple.eat();
17     }
18 }

这种方式不利于扩展 多加一种水果就得多写一个if else  , 使用反射优化。

 

新增一个存储路径的接口  也可以是配置文件 xml properties等   作用就是新增水果类的时候 直接配置路径。

 

1 public interface FruitPath {
2     String applePath = "com.dhl.simple_factory_mode.Apple";
3     String bananaPath = "com.dhl.simple_factory_mode.Banana";
4 }

 

 1     public static Fruit getFruit_2(String message) throws Exception {
 2         Class<?> clazz = Class.forName(message);
 3         Fruit fruit = (Fruit) clazz.newInstance();
 4         return fruit;
 5     }
 6 
 7     public static void main(String[] args) throws Exception {
 8         Fruit apple = getFruit_2(FruitPath.applePath);
 9         apple.pro();
10         apple.eat();
11     }

 

 

工厂方法模式:

工厂方法模式就是为了贴合开闭设计原则,扩展的时候不修改,可新增。

在简单工厂模式中 我们定义一个水果接口  定义几个实现类 就基于反射 或者 ife else创建了  。那如果新增一个水果类的话,那就得新增if else 或者说在path接口中新增一个路径,需要修改代码。

工厂方法模式是  定义一个顶级工厂,这个工厂是干啥的?  是创建单个水果工厂的,然后利用单个水果工厂创建水果实例。 例:使用Factory创建一个AppleFactory苹果工厂,再利用苹果工厂创建苹果实例,那要以后新增水果,就实现Factory来一个对应的水果工厂。

Fruit Apple Banana 和上面一样。

public interface Factory {
    public  Fruit createFruitFactory();
}
1 public class AppleFactory implements Factory {
2     public Fruit createFruitFactory() {
3         return new Apple();
4     }
5 }
1 public class BananaFactory implements Factory {
2     public Fruit createFruitFactory() {
3         return new Banana();
4     }
5 }

 

新增水果只需要 新增实现类,不涉及修改原代码。

 1 public class Client {
 2 
 3     public static void main(String[] args) throws Exception {
 4 
 5     //创建对应的水果工厂
 6        Factory appleFactory = new AppleFactory();
 7        Factory bananaFactory = new BananaFactory();
 8 
 9         //苹果工厂创建苹果
10         Fruit apple = appleFactory.createFruitFactory();
11 
12         //香蕉工厂创建香蕉
13         Fruit banana = bananaFactory.createFruitFactory();
14 
15 
16         apple.pro();
17         apple.eat();
18 
19         banana.pro();
20         banana.eat();
21 
22     }
23 }

 

一些相关的博客

http://blog.51cto.com/zero01/2067822

posted on 2018-11-15 20:30  木土mango  阅读(122)  评论(0)    收藏  举报

导航