◎Design Pattern: Abstract Factory 模式
我們平日走在步人街上都能看到很多專賣店,服裝、珠寶等等;
拿服裝專賣店來說,一個(gè)專賣店里面肯定有好幾個(gè)品牌的服裝,比如Giordano、Baleno,這些品牌都是不同公司生產(chǎn)的。
沒人見個(gè)專賣店自己生產(chǎn)服裝的吧,專賣店需要某個(gè)品牌的服裝時(shí),就去聯(lián)系相應(yīng)的廠家供貨即可,具體的生產(chǎn)是由廠家去完成。
抽象工廠模式也是如此,抽象工廠提供多個(gè)抽象方法,由具體的子工廠去實(shí)現(xiàn)。
現(xiàn)在我想開一家服裝店,經(jīng)營(yíng)上衣和短褲,至于具體什么品牌的等選門面在裝修之時(shí)再定。
// 生產(chǎn)Baleno上衣和短褲的工廠
拿服裝專賣店來說,一個(gè)專賣店里面肯定有好幾個(gè)品牌的服裝,比如Giordano、Baleno,這些品牌都是不同公司生產(chǎn)的。
沒人見個(gè)專賣店自己生產(chǎn)服裝的吧,專賣店需要某個(gè)品牌的服裝時(shí),就去聯(lián)系相應(yīng)的廠家供貨即可,具體的生產(chǎn)是由廠家去完成。
抽象工廠模式也是如此,抽象工廠提供多個(gè)抽象方法,由具體的子工廠去實(shí)現(xiàn)。
現(xiàn)在我想開一家服裝店,經(jīng)營(yíng)上衣和短褲,至于具體什么品牌的等選門面在裝修之時(shí)再定。
interface ISpecialityShop {
Shirt createShirt();
Pants createPants();
}
Shirt createShirt();
Pants createPants();
}
好了,現(xiàn)在門面選好且已裝修完畢,具體經(jīng)營(yíng)什么品牌也早想好,就佐丹奴和班尼路吧。
開始聯(lián)系廠家,廠家要能生產(chǎn)Shirt和Pants,而且要有生產(chǎn)Giordano、Baleno這兩個(gè)品牌。
我們都知道服裝都有一些共同的特征,每件衣服都有所屬的品牌、每條短褲都有一個(gè)尺碼。
interface Shirt {
// 品牌
String getBrand();
}
interface Pants {
// 尺寸
double getSize();
}
// 品牌
String getBrand();
}
interface Pants {
// 尺寸
double getSize();
}
佐丹奴的衣服自然會(huì)印上Giordano字樣的標(biāo)志
class GiordanoTShirt implements Shirt {
public String getBrand() {
return "Giordano";
}
}
class GiordanoPants implements Pants {
public double getSize() {
return 31;
}
}
public String getBrand() {
return "Giordano";
}
}
class GiordanoPants implements Pants {
public double getSize() {
return 31;
}
}
班尼路的也不例外,加上自己的品牌標(biāo)志
class BalenoTShirt implements Shirt {
public String getBrand() {
return "Baleno";
}
}
class BalenoPants implements Pants {
public double getSize() {
return 29;
}
}
public String getBrand() {
return "Baleno";
}
}
class BalenoPants implements Pants {
public double getSize() {
return 29;
}
}
運(yùn)氣不錯(cuò),很快就找到了廠家。
// 生產(chǎn)Giordano上衣和短褲的工廠
class GiordanoFactory implements ISpecialityShop {
Shirt createShirt() {
return new GiordanoTShirt();
}
Pants createPants() {
return new GiordanoPants();
}
}
Shirt createShirt() {
return new GiordanoTShirt();
}
Pants createPants() {
return new GiordanoPants();
}
}
// 生產(chǎn)Baleno上衣和短褲的工廠
class BalenoFactory implements ISpecialityShop {
Shirt createShirt() {
return new BalenoTShirt();
}
Pants createPants() {
return new BanlenoPants();
}
}
Shirt createShirt() {
return new BalenoTShirt();
}
Pants createPants() {
return new BanlenoPants();
}
}
廠家開始供貨,開業(yè)大吉,哈哈。
public class TestAbstractFactory extends TestCase {
public static void main(String[] args) {
TestRunner.run(TestAbstractFactory.class);
}
public void testFac() {
setShop(new GiordanoFactory());
IShirt shirt = shopFactory.createShirt();
shirt.getBrand();
IPants pants = shopFactory.createPants();
pants.getSize();
setShop(new BalenoFactory());
shirt = shopFactory.createShirt();
shirt.getBrand();
pants = shopFactory.createPants();
pants.getSize();
}
private void setShop(ISpecialityShop factory) {
shopFactory = factory;
}
protected void setUp() throws Exception {
}
protected void tearDown() throws Exception {
shopFactory = null;
}
private ISpecialityShop shopFactory;
}
public static void main(String[] args) {
TestRunner.run(TestAbstractFactory.class);
}
public void testFac() {
setShop(new GiordanoFactory());
IShirt shirt = shopFactory.createShirt();
shirt.getBrand();
IPants pants = shopFactory.createPants();
pants.getSize();
setShop(new BalenoFactory());
shirt = shopFactory.createShirt();
shirt.getBrand();
pants = shopFactory.createPants();
pants.getSize();
}
private void setShop(ISpecialityShop factory) {
shopFactory = factory;
}
protected void setUp() throws Exception {
}
protected void tearDown() throws Exception {
shopFactory = null;
}
private ISpecialityShop shopFactory;
}
以后想多經(jīng)營(yíng)幾個(gè)品牌,只需直接去找了廠家供貨即可。
最后,為了充分理解抽象工廠模式,畫出它的UML圖是很有必要的。
posted on 2007-04-19 15:24 聽風(fēng)的歌 閱讀(369) 評(píng)論(0) 編輯 收藏