抽象工厂模式

来源:互联网 发布:mac上微信接收的文件 编辑:程序博客网 时间:2024/05/17 04:16

抽象工厂模式(别名:配套)

     提供一个创建一系列或相互依赖对象的接口,而无需指定它们具体的类

一 、 概述

   当系统准备为用户提供一系列相关的对象,又不想让用户代码和创建这些对象的类形成耦合时,就可以使用抽象工厂方法模式来设计系统。抽象工厂模式的关键是在一个抽象类或接口中定义若干个抽象方法,这些抽象方法分别返回某个类的实例,该抽象类或接口让其子类或实现该接口的类重写这些抽象方法为用户提供一系列相关的对象。

二、抽象工厂模式的结构与使用

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

抽象产品(Prodcut
具体产品(ConcreteProduct
抽象工厂(AbstractFactory
具体构造者(ConcreteFactory
三、模式UML类图

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=newBeijingClothesFactory();

      shop.giveSuit(factory,110,82,170);

      factory=new ShanghaiClothesFactory();

      shop.giveSuit(factory,120,88,180);

   }

}

三、抽象工厂模式的优点 

抽象工厂模式可以为用户创建一系列相关的对象,使得用户和创建这些对象的类脱耦。
使用抽象工厂模式可以方便的为用户配置一系列对象。用户使用不同的具体工厂就能得到一组相关的对象,同时也能避免用户混用不同系列中的对象。
在抽象工厂模式中,可以随时增加具体工厂为用户提供一组相关的对象。

原创粉丝点击