您的位置:首页 > 其它

23种设计模式--抽象工厂模式

2016-12-16 17:20 113 查看
抽象工厂模式:

抽象工厂模式和工厂方法模式的区别就在于需要创建对象的复杂程度上。而且抽象工厂模式是三个里面最为抽象,最具一般性的。

抽象工厂模式的用意为:给客户端提供一个接口或抽象类,可以创建多个产品族中的产品对象而且使用抽象工厂模式还要满足以下条件:

应用场景:宝马分商务商务与跑车  ,奔驰有商务奔驰与奔驰跑车  用于只使用 商务车(跑车)为一类 创建工厂

1 系统中有多个产品族。

2系统一次只可能消费其中一族产品及其使用。

Public interface ProductA{}

Public interface ProductB{}

public class ProductA1 implements ProductA{}

public class ProductA2 implements ProductA{}

public class ProductB1 implements ProductB{}

public class ProductB2 implements ProductB{}

注:ProductA1和ProductB1为一个产品族

        ProductB1和ProductB2为一个产品族

抽象工厂接口:

 public interface Creator{

      public ProductA createProductA();

      public ProductB createProductB();

}

工厂实现类

 public class Factory1{

      public ProductA createProductA(){ return new ProductA1()} ;

      public ProductB createProductB(){ return new ProductB1()} ;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息