设计模式学习—抽象工厂模式

  1. 动机:产品有多个等级结构(需要一个工厂可以提供多个产品对象,而不是单一的产品对象。)
  2. 角色:
    1. 抽象工厂(一)
    2. 具体工厂
    3. 抽象产品(多)
    4. 具体产品
  3. 结构(其中A和B为一个产品族)
               
   4.适用:
  1. 一个系统不需知道产品创建的细节
  2. 系统产品有多于一个的产品族(A与B一族),系统只消费其中某一族的产品
  3. 同属一个产品族的产品是在一起使用的
  4. 系统提供一个产品类的库,所有的产品以同样的接口出现,是客户端不依赖于实现
5.优缺点:
  1. 优点:
    1. 只需改变具体工厂的实例,就可以在某种程度上改变整个软件系统的行为
    2. 能够保证客户端始终只使用同一个产品族中的对象

  2. 缺点:
    1. 难以扩展抽象工厂以便生产新种类的产品(增加新的产品族时需要修改整个工场和角色以及其子类)
6.代码:
     
/**
 * 抽象工厂
 * @author  ilmlife(ilmlife @163.com)
 *  <pre> 2013  - 12 -  15下午09:19:52 </pre>
 */
public  interface  ICreator {
        public  IProductA factorA();
        public  IProductB factorB();
}

/**
 * 具体工厂1
 * @author  ilmlife(ilmlife @163.com)
 *  <pre> 2013  - 12 -  15下午09:35:25 </pre>
 */
public  class  ConcreteCreator1  implements  ICreator {

        @Override
        public  IProductA factorA() {
              return  new  ProductA1();
      }

        @Override
        public  IProductB factorB() {
              return  new  ProductB1();
      }

}

/**
 * 具体工厂2
 * @author  ilmlife(ilmlife @163.com)
 *  <pre> 2013  - 12 -  15下午09:35:25 </pre>
 */
public  class  ConcreteCreator2  implements  ICreator {

        @Override
        public  IProductA factorA() {
              return  new  ProductA2();
      }

        @Override
        public  IProductB factorB() {
              return  new  ProductB2 ();
      }

}

/**
 * 抽象产品A
 * @author  ilmlife(ilmlife @163.com)
 *  <pre> 2013  - 12 -  15下午09:20:33 </pre>
 */
public  interface  IProductA {

}

/**
 * 抽象产品B
 * @author  ilmlife(ilmlife @163.com)
 *  <pre> 2013  - 12 -  15下午09:20:41 </pre>
 */
public  interface  IProductB {

}

/**
 * 具体产品A1
 * @author  ilmlife(ilmlife @163.com)
 *  <pre> 2013  - 12 -  15下午09:30:33 </pre>
 */
public  class  ProductA1  implements  IProductA {

}

/**
 * 具体产品A2
 * @author  ilmlife(ilmlife @163.com)
 *  <pre> 2013  - 12 -  15下午09:30:33 </pre>
 */
public  class  ProductA2  implements  IProductA {

}

/**
 * 具体产品B1
 * @author  ilmlife(ilmlife @163.com)
 *  <pre> 2013  - 12 -  15下午09:30:57 </pre>
 */
public  class  ProductB1  implements  IProductB {

}

/**
 * 具体产品B2
 * @author  ilmlife(ilmlife @163.com)
 *  <pre> 2013  - 12 -  15下午09:30:57 </pre>
 */
public  class  ProductB2  implements  IProductB {

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值