`
VerRan
  • 浏览: 453401 次
  • 性别: Icon_minigender_1
  • 来自: 陕西.西安
社区版块
存档分类
最新评论

工厂模式学习

阅读更多
  今天在看"基于UML的面向对象建模技术"时候看到了设计模式部分看了工厂模式,在这之前我看过基本将设计模式的书不过觉得讲的不怎么清晰看了这本书候觉得清晰了很多,关键是上面的例子让我觉得很清晰.<o:p></o:p>

1. 创建模式 <o:p></o:p>

  创建模式是提供创建对象和管理对象生命周期的方法.<o:p></o:p>

 1.1工厂模式<o:p></o:p>

  不同的工厂生产不同的产品,而在一般的方法中则是用一个控制类来直接实例化我们所要生产的产品,而工厂模式则是利用建立一个工厂的思想,工厂来创建对象,主要应用在一个类需要其子类进行创建时还有在编码的时候不能预见需要创建哪类的对象实例,开发人员不希望把创建哪个类的对象实例和讲如何创建实例的信息暴露给外来的程序.
类图和一般的都一样.<o:p></o:p>

实例<o:p></o:p>

 电信行业的计费系统中.电信供应商会根据市场情况提供不同的通信产品.比如88元套餐和30元套餐.对于不同的产品往往采用不同的的计费方式,为了使系统具有更好的通用性,便于新通信产品的加入在系统设计的时候可以采用工厂模式进行设计.

抽象工厂
  1. package com.lht.factory;   
  2.   
  3. public interface Creator {   
  4.     //建立工厂用于生产产品,返回的类型就是要声场的产品这样材可以生产出产品阿!   
  5. public Product factory();   
  6. }   
抽象产品
  1. package com.lht.factory;   
  2.   
  3. public interface Product {   
  4.        double price();//产品的单价   
  5.        int limit();//通过通话时间来限制优惠   
  6.        boolean islocal();//判断打的使不是短途,或者使长途   
  7. }   

Tel30工厂
  1. package com.lht.factory;   
  2.   
  3. public class Tel30Creator implements Creator {   
  4.   
  5.     public Product factory() {   
  6.         // TODO Auto-generated method stub   
  7.         return new Tel30();   
  8.     }   
  9.   
  10. }   
Tel30产品
  1. package com.lht.factory;   
  2.   
  3. public class Tel30 implements Product {   
  4.     double price;   
  5.     int limit_time;   
  6.     boolean local;   
  7.     public Tel30(){   
  8.         price=0.15;   
  9.         limit_time=100;   
  10.         local=true;   
  11.     }   
  12.   
  13.     public boolean islocal() {   
  14.         // TODO Auto-generated method stub   
  15.         return local;   
  16.     }   
  17.   
  18.     public int limit() {   
  19.         // TODO Auto-generated method stub   
  20.         return limit_time;   
  21.     }   
  22.   
  23.     public double price() {   
  24.         // TODO Auto-generated method stub   
  25.         return price;   
  26.     }   
  27.   
  28. }   
Tel88工厂
  1. package com.lht.factory;   
  2.   
  3. public class Tel88Creator implements Creator {   
  4.   
  5.     public Product factory() {   
  6.         // TODO Auto-generated method stub   
  7.         return new Tel88();   
  8.     }   
  9.   
  10. }   

Tel88产品
  1. package com.lht.factory;   
  2.   
  3. public class Tel88 implements Product {   
  4.     double price;//电话的每分钟价格   
  5.     int limit_time;//限制时间   
  6.     boolean local;//是不是短途   
  7.     //建立构造方法   
  8.     Tel88(){   
  9.         price=0.4;   
  10.         limit_time=300;   
  11.         local=true;   
  12.     }   
  13.     public boolean islocal() {   
  14.         // TODO Auto-generated method stub   
  15.         return local;   
  16.     }   
  17.   
  18.     public int limit() {   
  19.         // TODO Auto-generated method stub   
  20.         return limit_time;   
  21.     }   
  22.   
  23.     public double price() {   
  24.         // TODO Auto-generated method stub   
  25.         return price;   
  26.     }   
  27.   
  28. }  
显示业务的详细信息
  1. package com.lht.factory;   
  2.   
  3. public class ListInfo {   
  4.       //public ;   
  5.      public static String msg;   
  6.       public  static String  listDetali(Product product){   
  7.           double price=product.price();   
  8.           int limit_time=product.limit();   
  9.           boolean local=product.islocal();   
  10.           String localString;   
  11.           if(local){   
  12.               localString="短途";   
  13.           }   
  14.           else  
  15.           {   
  16.               localString="长途";   
  17.           }   
  18.           msg="  价格是"+price+"角/分  "+"时间限制是"+limit_time+"分  "+"地区限制是"+localString;   
  19.           return msg;   
  20.       }   
  21. }   
客户端程序用于测试
  1. package com.lht.factory;   
  2.   
  3. public class Client {   
  4.     /**  
  5.      * @param args  
  6.      */  
  7.     //如果我们添加新的产品可以在用新的产品实现Product接口   
  8.     //同时建立newTelCreator创建新产品   
  9.     private  static Creator creator1,creator2;   
  10.     private  static Product product1,product2;   
  11.     public static void main(String[] args) {   
  12.         // TODO Auto-generated method stub   
  13. creator1=new Tel88Creator();//建立Tel88建造器   
  14. creator2=new Tel30Creator();//建立Tel30建造器   
  15. creator3=new TeleHolidayCreator();//建立假日建造器   
  16. product1=creator1.factory();//建立88产品   
  17. product2=creator2.factory();//建立30产品   
  18. product3=creator3.factory();//建立新产品假日套餐   
  19. //获得Tel88的基本信息情况   
  20. String Tel88msg=ListInfo.listDetali(product1);   
  21. System.out.println("Tel88套餐的详细信息"+Tel88msg);   
  22. String Tel30msg=ListInfo.listDetali(product2);   
  23. System.out.println("Tel30套餐的详细信息"+Tel30msg);   
  24.     }   
  25.   
  26.        
  27. }   
结果
  1. Tel88套餐的详细信息  价格是0.4角/分  时间限制是300分  地区限制是短途   
  2. Tel30套餐的详细信息  价格是0.15角/分  时间限制是100分  地区限制是短途  
  
  添加新业务
     假日套餐工厂
  1. package com.lht.factory;   
  2.   
  3. public class TeleHolidayCreator implements Creator {   
  4.   
  5.     public Product factory() {   
  6.         // TODO Auto-generated method stub   
  7.         return new TeleHoliday();   
  8.     }   
  9.   
  10. }   
假日套餐产品
  1. package com.lht.factory;   
  2.   
  3. public class TeleHoliday implements Product {   
  4.     double price;   
  5.     int limit_time;   
  6.     boolean local;   
  7.     public TeleHoliday(){   
  8.         price=0.3;   
  9.         limit_time=100;   
  10.         local=false;   
  11.     }   
  12.     public boolean islocal() {   
  13.         // TODO Auto-generated method stub   
  14.         return local;   
  15.     }   
  16.   
  17.     public int limit() {   
  18.         // TODO Auto-generated method stub   
  19.         return limit_time;   
  20.     }   
  21.   
  22.     public double price() {   
  23.         // TODO Auto-generated method stub   
  24.         return price;   
  25.     }   
  26.   
  27. }   

新业务添加成功!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics