抽象工厂模式(别名:配套)
提供一个创建一系列或相互依赖对象的接口,而无需指定它们具体的类。
一 、 概述
当系统准备为用户提供一系列相关的对象,又不想让用户代码和创建这些对象的类形成耦合时,就可以使用抽象工厂方法模式来设计系统。抽象工厂模式的关键是在一个抽象类或接口中定义若干个抽象方法,这些抽象方法分别返回某个类的实例,该抽象类或接口让其子类或实现该接口的类重写这些抽象方法为用户提供一系列相关的对象。
二、抽象工厂模式的结构与使用
模式的结构中包括四种角色:
1.抽象产品(Product):
UpperClothes.java
public abstract class UpperClothes{
public abstract intgetChestSize();
public abstract intgetHeight();
public abstract String getName();
}
Trousers.java
public abstract class Trousers{
public abstract intgetWaistSize();
public abstract intgetHeight();
public abstract String getName();
}
2.具体产品(ConcreteProduct)_1:WesternUpperClothes.java
public class WesternUpperClothes extendsUpperClothes{
private intchestSize;
private int height;
private String name;
WesternUpperClothes(Stringname,intchestSize,int height){
this.name=name;
this.chestSize=chestSize;
this.height=height;
}
public intgetChestSize(){
return chestSize;
}
public intgetHeight(){
return height;
}
public String getName(){
return name;
}
}
2.具体产品(ConcreteProduct)_2:CowboyUpperClothes.java
public class CowboyUpperClothes extendsUpperClothes{
private intchestSize;
private int height;
private String name;
CowboyUpperClothes(Stringname,intchestSize,int height){
this.name=name;
this.chestSize=chestSize;
this.height=height;
}
public intgetChestSize(){
return chestSize;
}
public intgetHeight(){
return height;
}
public String getName(){
return name;
}
}
2.具体产品(ConcreteProduct)_3:WesternTrousers.java
public class WesternTrousers extends Trousers{
private intwaistSize;
private int height;
private String name;
WesternTrousers(Stringname,intwaistSize,int height){
this.name=name;
this.waistSize=waistSize;
this.height=height;
}
public intgetWaistSize(){
return waistSize;
}
public intgetHeight(){
return height;
}
public String getName(){
return name;
}
}
2.具体产品(ConcreteProduct)_4:CowboyTrousers.java
public class CowboyTrousers extends Trousers{
private intwaistSize;
private int height;
private String name;
CowboyTrousers(Stringname,intwaistSize,int height){
this.name=name;
this.waistSize=waistSize;
this.height=height;
}
public intgetWaistSize(){
return waistSize;
}
public intgetHeight(){
return height;
}
public String getName(){
return name;
}
}
3.抽象工厂(AbstractFactory):ClothesFactory.java
public abstractclass ClothesFactory{
public abstract UpperClothescreateUpperClothes(intchestSize,int height);
public abstract Trousers createTrousers(intwaistSize,int height);
}
4.具体工厂(ConcreteFactory):
BeijingClothesFactory.java
public class BeijingClothesFactory extendsClothesFactory {
public UpperClothescreateUpperClothes(intchestSize,int height){
return new WesternUpperClothes("北京牌西服上衣",chestSize,height);
}
public Trousers createTrousers(intwaistSize,int height){
return new WesternTrousers("北京牌西服裤子",waistSize,height);
}
}
ShanghaiClothesFactory.java
public class ShanghaiClothesFactory extendsClothesFactory {
public UpperClothescreateUpperClothes(intchestSize,int height){
return new WesternUpperClothes("上海牌牛仔上衣",chestSize,height);
}
public Trousers createTrousers(intwaistSize,int height){
return new WesternTrousers("上海牌牛仔裤",waistSize,height);
}
}
5.应用_1:Shop.java
public class Shop{
UpperClothes cloth;
Trousers trouser;
public void giveSuit(ClothesFactoryfactory,intchestSize,intwaistSize,int height){
cloth=factory.createUpperClothes(chestSize,height);
trouser=factory.createTrousers(waistSize,height);
showMess();
}
private void showMess(){
System.out.println("<套装信息>");
System.out.println(cloth.getName()+":");
System.out.print("胸围:"+cloth.getChestSize());
System.out.println("身高:"+cloth.getHeight());
System.out.println(trouser.getName()+":");
System.out.print("腰围:"+trouser.getWaistSize());
System.out.println("身高:"+trouser.getHeight());
}
}
5.应用_2: Application.java
public classApplication{
public static void main(String args[]){
Shop shop=new Shop();
ClothesFactory factory=new BeijingClothesFactory();
shop.giveSuit(factory,110,82,170);
factory=new ShanghaiClothesFactory();
shop.giveSuit(factory,120,88,180);
}
}
三、抽象工厂模式的优点