【抽象工厂模式】

       当系统准备为用户提供一系列相关的对象,又不想让用户代码和创建这些对象的类形成耦合时,就可以使用抽象工厂方法模式来设计系统。

       抽象工厂模式的关键是在一个抽象类或接口中定义若干个抽象方法,这些抽象方法分别返回某个类的实例,该抽象类或接口让其子类或实现该接口的类重写这些抽象方法,为用户提供一系列相关的对象。

模式的结构中包括四种角色:

抽象产品(Prodcut):一个抽象类或接口,负责定义具体产品必须实现的方法。
具体产品(ConcreteProduct):具体产品是一个类,如果Product是一个抽象类,那么具体产品是Product的子类;如果Product是一个接口,那么具体产品是实现Product接口的类。 
抽象工厂(AbstractFactory):一个接口或抽象类,负责定义若干个抽象方法。
具体工厂(ConcreteFactory) :如果抽象工厂是抽象类,具体工厂是抽象工厂的子类;如果抽象工厂是接口,具体工厂是实现抽象工厂的类。具体工厂重写抽象工厂中的抽象方法,使该方法返回具体产品的实例。
实例
       建立一个系统,该系统可以为用户提供西服套装和牛仔套装。
    抽象产品
    UpperClothes.java
public abstract class UpperClothes{
   public abstract int getChestSize();
   public abstract int getHeight();
   public abstract String getName(); 
}

Trousers.java

public abstract class Trousers{
   public abstract int getWaistSize();
   public abstract int getHeight();
   public abstract String getName(); 
}

 InnerClothes.java

public  abstract class InnerClothes {
  public abstract int getSize();
  public abstract int getHeight();
  public abstract String getName();
}

具体产品

WesternUpperClothes.java  

public class WesternUpperClothes extends UpperClothes{
   private int chestSize;
   private int height;
   private String name;
   WesternUpperClothes(String name,int chestSize,int height){
       this.name=name;
       this.chestSize=chestSize;
       this.height=height;
   }
   public int getChestSize(){
       return chestSize;
   }
   public int getHeight(){
       return height;
   }
   public String getName(){
       return name;   
   } 
}

 CowboyUpperClothes .java

public class CowboyUpperClothes extends UpperClothes{
   private int chestSize;
   private int height;
   private String name;
   CowboyUpperClothes(String name,int chestSize,int height){
       this.name=name;
       this.chestSize=chestSize;
       this.height=height;
   }
   public int getChestSize(){
       return chestSize;
   }
   public int getHeight(){
       return height;
   }
   public String getName(){
       return name;
   } 
}

WesternTrousers .java

public class WesternTrousers extends Trousers{
   private int waistSize;
   private int height;
   private String name;
   WesternTrousers(String name,int waistSize,int height){
       this.name=name;
       this.waistSize=waistSize;
       this.height=height;
   }
    public int getWaistSize(){
       return waistSize;
   }
   public int getHeight(){
       return height;
   }
   public String getName(){
       return name;
   } 
}

CowboyTrousers.java

public class CowboyTrousers extends Trousers{  
   private int waistSize;
   private int height;
   private String name;
   CowboyTrousers(String name,int waistSize,int height){
       this.name=name;
       this.waistSize=waistSize;
       this.height=height;
   }
    public int getWaistSize(){
       return waistSize;
   }
   public int getHeight(){
       return height;
   }
   public String getName(){
       return name;
   } 
}

ShirtInnerClothes.java

public class ShirtInnerClothes extends InnerClothes {
    private int Size;
    private int height;
    private String name;
    ShirtInnerClothes(String name ,int size ,int height){
    this.name=name;
    this.Size=size;
    this.height=height;
}
 public int getSize(){
 return Size;
}
public int getHeight(){
 return height;
}
public String getName(){
 return name;
}
}

SweaterInnerClothes.java

public class SweaterInnerClothes extends InnerClothes {
    private int Size;
    private int height;
    private String name;
    SweaterInnerClothes (String name ,int size ,int height){
    this.name=name;
    this.Size=size;
    this.height=height;
}
 public int getSize(){
 return Size;
}
public int getHeight(){
 return height;
}
public String getName(){
 return name;
}
}   

抽象工厂

ClothesFactory.java

public abstract class ClothesFactory{
    public abstract UpperClothes createUpperClothes(int chestSize,int height);
    public abstract Trousers createTrousers(int waistSize,int height);
    public abstract InnerClothes createInnerClothes(int Size,int height);
}

具体工厂

 BeijingClothesFactory.java

public class BeijingClothesFactory extends ClothesFactory {
     public UpperClothes createUpperClothes(int chestSize,int height){
         return new WesternUpperClothes("北京牌西服上衣",chestSize,height);
     }
     public Trousers createTrousers(int waistSize,int height){
         return new WesternTrousers("北京牌西服裤子",waistSize,height);
    }
     public InnerClothes createInnerClothes(int Size,int height){
         return new SweaterInnerClothes("北京牌衬衫",Size,height);
    }
}

 ShanghaiClothesFactory.java

public class ShanghaiClothesFactory extends ClothesFactory {
    public UpperClothes createUpperClothes(int chestSize,int height){
         return new WesternUpperClothes("上海牌牛仔上衣",chestSize,height);
    }
    public Trousers createTrousers(int waistSize,int height){
         return new WesternTrousers("上海牌牛仔裤",waistSize,height);
    }
    public InnerClothes createInnerClothes(int Size,int height){
         return new ShirtInnerClothes("上海牌衬衫",Size,height);
    }
}

应用 

Shop.java

public class Shop{
    UpperClothes cloth;
    Trousers trouser; 
    InnerCloth inner;
    public void giveSuit(ClothesFactory factory,int chestSize,int waistSize,int Size,int height){
        cloth=factory.createUpperClothes(chestSize,height);
        trouser=factory.createTrousers(waistSize,height);
        inner=factory.createInnerClothes(Size, 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());
System.out.println(inner.getName()+":");
System.out.print("尺寸:"+inner.getSize());
System.out.println("身高:"+inner.getHeight());
    }
}

Application.java

public class Application{
       public static void main(String args[]){
       Shop shop=new Shop();
       ClothesFactory factory=new BeijingClothesFactory(); 
       shop.giveSuit(factory,110,82,45,170);
       factory=new ShanghaiClothesFactory(); 
       shop.giveSuit(factory,120,88,45,180);
       }
}

UML类图:

        应用程序在使用抽象工厂模式时,只和抽象产品、抽象工厂以及具体工厂打交道,用户只需了解抽象产品有哪些方法即可,不需要知道有哪些具体产品。

这里是不吃香菜也可做香菜头子。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值