今天在看"基于UML的面向对象建模技术"时候看到了设计模式部分看了工厂模式,在这之前我看过基本将设计模式的书不过觉得讲的不怎么清晰看了这本书候觉得清晰了很多,关键是上面的例子让我觉得很清晰.<o:p></o:p>
Tel30工厂
Tel88产品
添加新业务
假日套餐工厂
新业务添加成功!
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元套餐.对于不同的产品往往采用不同的的计费方式,为了使系统具有更好的通用性,便于新通信产品的加入在系统设计的时候可以采用工厂模式进行设计.
抽象工厂
- package com.lht.factory;
- public interface Creator {
- //建立工厂用于生产产品,返回的类型就是要声场的产品这样材可以生产出产品阿!
- public Product factory();
- }
抽象产品
- package com.lht.factory;
- public interface Product {
- double price();//产品的单价
- int limit();//通过通话时间来限制优惠
- boolean islocal();//判断打的使不是短途,或者使长途
- }
Tel30工厂
- package com.lht.factory;
- public class Tel30Creator implements Creator {
- public Product factory() {
- // TODO Auto-generated method stub
- return new Tel30();
- }
- }
Tel30产品
- package com.lht.factory;
- public class Tel30 implements Product {
- double price;
- int limit_time;
- boolean local;
- public Tel30(){
- price=0.15;
- limit_time=100;
- local=true;
- }
- public boolean islocal() {
- // TODO Auto-generated method stub
- return local;
- }
- public int limit() {
- // TODO Auto-generated method stub
- return limit_time;
- }
- public double price() {
- // TODO Auto-generated method stub
- return price;
- }
- }
Tel88工厂
- package com.lht.factory;
- public class Tel88Creator implements Creator {
- public Product factory() {
- // TODO Auto-generated method stub
- return new Tel88();
- }
- }
Tel88产品
- package com.lht.factory;
- public class Tel88 implements Product {
- double price;//电话的每分钟价格
- int limit_time;//限制时间
- boolean local;//是不是短途
- //建立构造方法
- Tel88(){
- price=0.4;
- limit_time=300;
- local=true;
- }
- public boolean islocal() {
- // TODO Auto-generated method stub
- return local;
- }
- public int limit() {
- // TODO Auto-generated method stub
- return limit_time;
- }
- public double price() {
- // TODO Auto-generated method stub
- return price;
- }
- }
显示业务的详细信息
- package com.lht.factory;
- public class ListInfo {
- //public ;
- public static String msg;
- public static String listDetali(Product product){
- double price=product.price();
- int limit_time=product.limit();
- boolean local=product.islocal();
- String localString;
- if(local){
- localString="短途";
- }
- else
- {
- localString="长途";
- }
- msg=" 价格是"+price+"角/分 "+"时间限制是"+limit_time+"分 "+"地区限制是"+localString;
- return msg;
- }
- }
客户端程序用于测试
- package com.lht.factory;
- public class Client {
- /**
- * @param args
- */
- //如果我们添加新的产品可以在用新的产品实现Product接口
- //同时建立newTelCreator创建新产品
- private static Creator creator1,creator2;
- private static Product product1,product2;
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- creator1=new Tel88Creator();//建立Tel88建造器
- creator2=new Tel30Creator();//建立Tel30建造器
- creator3=new TeleHolidayCreator();//建立假日建造器
- product1=creator1.factory();//建立88产品
- product2=creator2.factory();//建立30产品
- product3=creator3.factory();//建立新产品假日套餐
- //获得Tel88的基本信息情况
- String Tel88msg=ListInfo.listDetali(product1);
- System.out.println("Tel88套餐的详细信息"+Tel88msg);
- String Tel30msg=ListInfo.listDetali(product2);
- System.out.println("Tel30套餐的详细信息"+Tel30msg);
- }
- }
结果
- Tel88套餐的详细信息 价格是0.4角/分 时间限制是300分 地区限制是短途
- Tel30套餐的详细信息 价格是0.15角/分 时间限制是100分 地区限制是短途
添加新业务
假日套餐工厂
- package com.lht.factory;
- public class TeleHolidayCreator implements Creator {
- public Product factory() {
- // TODO Auto-generated method stub
- return new TeleHoliday();
- }
- }
假日套餐产品
- package com.lht.factory;
- public class TeleHoliday implements Product {
- double price;
- int limit_time;
- boolean local;
- public TeleHoliday(){
- price=0.3;
- limit_time=100;
- local=false;
- }
- public boolean islocal() {
- // TODO Auto-generated method stub
- return local;
- }
- public int limit() {
- // TODO Auto-generated method stub
- return limit_time;
- }
- public double price() {
- // TODO Auto-generated method stub
- return price;
- }
- }
新业务添加成功!