• java设计模式2--抽象工厂模式(Abstract Factory)


    本文地址:http://www.cnblogs.com/archimedes/p/java-abstract-factory-pattern.html,转载请注明源地址。

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

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

    概述

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

    适用性

    1.一个系统要独立于它的产品的创建、组合和表示时。

    2.一个系统要由多个产品系列中的一个来配置时。

    3.当你要强调一系列相关的产品对象的设计以便进行联合使用时。

    4.当你提供一个产品类库,而只想显示它们的接口而不是实现时。

    参与者

    1.AbstractFactory 声明一个创建抽象产品对象的操作接口。

    2.ConcreteFactory 实现创建具体产品对象的操作。

    3.AbstractProduct 为一类产品对象声明一个接口。

    4.ConcreteProduct 定义一个将被相应的具体工厂创建的产品对象。 实现AbstractProduct接口。

    5.Client 仅使用由AbstractFactory和AbstractProduct类声明的接口

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

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

    •抽象产品(Prodcut)

    •具体产品(ConcreteProduct)

    •抽象工厂(AbstractFactory)

    •具体工厂(ConcreteFactory) 

    模式的UML类图

    实战部分

    例1】:建立一个系统,该系统可以为用户提供西服套装(上衣+裤子)和牛仔套装(上衣+裤子)。

    模式的结构的描述与使用 

    1.抽象产品(Product) :

     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(); 
    }

    2.具体产品(ConcreteProduct)_1: 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;
       } 
    }

    2.具体产品(ConcreteProduct)_2: 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;
       } 
    }

    2.具体产品(ConcreteProduct)_3: 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;
       } 
    }

    2.具体产品(ConcreteProduct)_4: 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;
       } 
    }

    3.抽象工厂(AbstractFactory): ClothesFactory.java

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

    4.具体工厂(ConcreteFactory): 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);
        }
    }

    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);
        }
    }

    5.应用_1: Shop.java

    public class Shop{
        UpperClothes cloth;
        Trousers trouser; 
        public void giveSuit(ClothesFactory factory,int chestSize,int waistSize,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 class Application{
        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);
        }
    }

    抽象工厂模式的优点

    •抽象工厂模式可以为用户创建一系列相关的对象,使得用户和创建这些对象的类脱耦。

    •使用抽象工厂模式可以方便的为用户配置一系列对象。用户使用不同的具体工厂就能得到一组相关的对象,同时也能避免用户混用不同系列中的对象。

    •在抽象工厂模式中,可以随时增加“具体工厂”为用户提供一组相关的对象。

  • 相关阅读:
    Entity Framework Code First 学习日记(1)精
    敏捷宣言
    VS2013中web项目中自动生成的ASP.NET Identity代码思考
    ReSharper 配置及用法
    MVC中的Repository模式
    关于Repository模式
    Entity Framework 学习总结之十一:POCO
    Linux Centos7 离线安装docker 【官网翻译和注释】
    企业服务总线ESB
    面向服务的架构SOA
  • 原文地址:https://www.cnblogs.com/wuyudong/p/java-abstract-factory-pattern.html
Copyright © 2020-2023  润新知