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()} ;
}
抽象工厂模式和工厂方法模式的区别就在于需要创建对象的复杂程度上。而且抽象工厂模式是三个里面最为抽象,最具一般性的。
抽象工厂模式的用意为:给客户端提供一个接口或抽象类,可以创建多个产品族中的产品对象而且使用抽象工厂模式还要满足以下条件:
应用场景:宝马分商务商务与跑车 ,奔驰有商务奔驰与奔驰跑车 用于只使用 商务车(跑车)为一类 创建工厂
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()} ;
}
相关文章推荐
- 23种设计模式(3):抽象工厂模式
- 23种经典设计模式的java实现_1_抽象工厂模式
- 23种设计模式(3):抽象工厂模式
- 23种设计模式(3):抽象工厂模式
- 23种设计模式(3):抽象工厂模式
- 23种设计模式(3):抽象工厂模式
- 23种设计模式(3):抽象工厂模式
- GoF23种设计模式之创建型模式之抽象工厂模式
- Java开发中的23种设计模式之二:抽象工厂模式(Abstract Factory)
- 23种经典设计模式的java实现_1_抽象工厂模式
- 23种设计模式(3):抽象工厂模式
- 23种设计模式之抽象工厂模式2
- 23种设计模式(3):抽象工厂模式
- 23种设计模式-2.抽象工厂模式(Abstract Factory)
- 23种设计模式(3):抽象工厂模式
- java_23种设计模式之抽象工厂模式
- 23种设计模式之抽象工厂模式
- 23种设计模式(3):抽象工厂模式
- 23种设计模式(3):抽象工厂模式
- 23种设计模式(3):抽象工厂模式