文章出處

使用頻率:★★★★★

一、什么是抽象工廠模式

就是對一組具有相同主題的工廠進行封裝(維基百科解釋的很到位);

例如:生產一臺PC機,使用工廠方法模式的話,一般會有cpu工廠,內存工廠,顯卡工廠...但是使用抽象工廠模式的話,只有一個工廠就是PC工廠,但是一個PC工廠涵蓋了cpu工廠,內存工廠,顯卡工廠等要做的所有事;

二、補充說明

  • 注意這里的“相同主題”的概念,表示的是同一個產品族,不能將cpu工廠,面粉工廠封裝成一個工廠,因為他們不屬于同一個產品族;
  • 另外,還有一個產品等級的概念,還是以生產PC機為例,所謂的產品等級指的是不同廠商生產的CPU,如Intel和AMD的CPU,他們是同一個產品等級,如果只涉及產品等級的話,是不需要應用抽象工廠模式,使用工廠方法模式即可;
  • 工廠方法模式解決的范疇是產品等級(AMD處理器,Intel處理器等);抽象工廠模式解決的范疇是產品族等級(聯想PC、惠普PC等);

三、角色

  • 抽象工廠
  • 具體工廠
  • 抽象產品
  • 具體產品
  • 產品使用者

說明:

  • 具體工廠“繼承”抽象工廠;
  • 具體產品”繼承“抽象產品;
  • 每個具體工廠(如PC工廠)包含若干個子工廠方法(如cpu工廠方法、顯卡工廠方法...),子工廠方法負責生產對應的具體子產品,所有具體子產品(cpu、內存、顯卡...)組合成一個具體產品(如惠普XXX型號PC);
  • 產品使用者使用每個具體工廠生產的具體產品;

四、例子

這里就不用PC這個例子了,繼續前一個工廠模式的例子,在上一篇工廠模式的例子中,我們使用的是創建父親對象這個例子,其中中國父親和美國父親指的就是同一個產品等級;

但是當我們要創建一個家庭對象的時候,需要創建父親對象、母親對象、孩子對象等等,所謂的父親、母親、孩子就構成了一個產品族,中國家庭、美國家庭就是產品族等級;這個時候就需要使用抽象工廠模式了;

類之間的關系圖:

代碼實現:

先創建抽象產品(抽象母親、抽象父親),具體產品(具體母親、具體父親):

package com.pichen.dp.creationalpattern.abstractfactory;

public interface IMother {
    public void printName();
}
package com.pichen.dp.creationalpattern.abstractfactory;

public interface IFather {
    public void printName();
}
package com.pichen.dp.creationalpattern.abstractfactory;

public class ChineseMother implements IMother{
    private String name;
    public ChineseMother(String name) {
        this.name = name;
        System.out.println("create a cn mother.");
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void printName() {
        System.out.println(this.getClass().getName() + ":" + this.name);
        
    }
}
package com.pichen.dp.creationalpattern.abstractfactory;

public class AmericanMother implements IMother{
    private String name;
    public AmericanMother(String name) {
        this.name = name;
        System.out.println("create a us mother.");
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void printName() {
        System.out.println(this.getClass().getName() + ":" + this.name);
        
    }
}
package com.pichen.dp.creationalpattern.abstractfactory;

public class ChineseFather implements IFather{
    private String name;
    public ChineseFather(String name) {
        this.name = name;
        System.out.println("create a cn father.");
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void printName() {
        System.out.println(this.getClass().getName() + ":" + this.name);
        
    }
}
package com.pichen.dp.creationalpattern.abstractfactory;

public class AmericanFather implements IFather{
    private String name;
    public AmericanFather(String name) {
        this.name = name;
        System.out.println("create a us father.");
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void printName() {
        System.out.println(this.getClass().getName() + ":" + this.name);
        
    }
}

創建一個抽象家庭工廠接口:

package com.pichen.dp.creationalpattern.abstractfactory;

/**
 * 
 * abstract factory
 * father + mother + sister + ... = Product Family
 * cnfather + usfather + ukfather + ... = Product grade  //factory method
 * 〈功能詳細描述〉
 * @author    pi chen
 * @version   cp-lib V1.0.0, 2015年11月25日
 * @see       
 * @since     cp-lib V1.0.0
 */
public interface IFamilyFactory {
    public IFather createFather(String name);
    public IMother createMother(String name);
}

分別創建具體的中國家庭工廠和美國家庭工廠:

package com.pichen.dp.creationalpattern.abstractfactory;

public class ChineseFamilyFactory implements IFamilyFactory{

    @Override
    public IFather createFather(String name) {
        return new ChineseFather(name);
    }

    @Override
    public IMother createMother(String name) {
        return new ChineseMother(name);
    }

}
package com.pichen.dp.creationalpattern.abstractfactory;

public class AmericanFamilyFactory implements IFamilyFactory{

    @Override
    public IFather createFather(String name) {
        return new AmericanFather(name);
    }

    @Override
    public IMother createMother(String name) {
        return new AmericanMother(name);
    }

}

創面產品使用者main方法:

package com.pichen.dp.creationalpattern.abstractfactory;

public class Main {
    public static void main(String[] args) {
        IFamilyFactory cnFamilyFactory = new ChineseFamilyFactory();
        IFamilyFactory usFamilyFactory = new AmericanFamilyFactory();
        
        IFather cnFather = cnFamilyFactory.createFather("cn father-test");
        IMother cnMother = cnFamilyFactory.createMother("cn mother-test");
        
        IFather usFather = usFamilyFactory.createFather("us father-test");
        IMother usMother = usFamilyFactory.createMother("us mother-test");
        
        cnFather.printName();
        cnMother.printName();
        usFather.printName();
        usMother.printName();
    }
}

結果打印如下,功能正常:

create a cn father.
create a cn mother.
create a us father.
create a us mother.
com.pichen.dp.creationalpattern.abstractfactory.ChineseFather:cn father-test
com.pichen.dp.creationalpattern.abstractfactory.ChineseMother:cn mother-test
com.pichen.dp.creationalpattern.abstractfactory.AmericanFather:us father-test
com.pichen.dp.creationalpattern.abstractfactory.AmericanMother:us mother-test

文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()