日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

设计模式(十)------23种设计模式(3):抽象工厂模式

發(fā)布時間:2023/12/20 asp.net 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式(十)------23种设计模式(3):抽象工厂模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載自:https://www.cnblogs.com/chenpi/p/5156801.html (風一樣的碼農)


閱讀目錄

使用頻率:★★★★★
  • 一、什么是抽象工廠模式
    二、補充說明
    三、角色
    四、例子

使用頻率:★★★★★

一、什么是抽象工廠模式

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

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

二、補充說明

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

三、角色

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

說明:

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

四、例子

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

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

類之間的關系圖:

代碼實現:

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

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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic void printName() {System.out.println(this.getClass().getName() + ":" + this.name);} }

創(chuàng)建一個抽象家庭工廠接口:

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

分別創(chuàng)建具體的中國家庭工廠和美國家庭工廠:

package com.pichen.dp.creationalpattern.abstractfactory;public class ChineseFamilyFactory implements IFamilyFactory{@Overridepublic IFather createFather(String name) {return new ChineseFather(name);}@Overridepublic IMother createMother(String name) {return new ChineseMother(name);}} package com.pichen.dp.creationalpattern.abstractfactory;public class AmericanFamilyFactory implements IFamilyFactory{@Overridepublic IFather createFather(String name) {return new AmericanFather(name);}@Overridepublic IMother createMother(String name) {return new AmericanMother(name);}}

創(chuàng)面產品使用者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

轉載于:https://www.cnblogs.com/chz-blogs/p/9380977.html

總結

以上是生活随笔為你收集整理的设计模式(十)------23种设计模式(3):抽象工厂模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。