码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-11-15 21:12:24      阅读:678      评论:0      收藏:0      [点我收藏+]

标签:配置文件   method   interface   imp   void   简单   XML   实现类   inter   

简单工厂

技术分享图片

 

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创建了  。

工厂方法模式是  定义一个顶级工厂,这个工厂是干啥的?  是创建单个水果工厂的,然后利用单个水果工厂创建水果实例。 例:使用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         AppleFactory appleFactory = 
            (AppleFactory)Class.forName("com.dhl.factory_method_mode.AppleFactory").newInstance(); 7 BananaFactory bananaFactory =
            (BananaFactory) Class.forName("com.dhl.factory_method_mode.BananaFactory").newInstance(); 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

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

标签:配置文件   method   interface   imp   void   简单   XML   实现类   inter   

原文地址:https://www.cnblogs.com/mutumango/p/9965909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!