日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

抽象工廠模式的個人理解列子

系統 2022 0
很久前就像看看設計模式了,在此記錄下自己的學習過程。
設計模式分為三種:靜態工廠模式,工廠方法模式,抽象工廠模式。其中抽象工廠模式是三個里面最抽象的,也是最具有一般性的。
在看教程的同時,花了一上午時間寫了個例子。
例子配置如圖:
抽象工廠模式的個人理解列子

我認為比較重要的事:有幾個產品,抽象工廠中就有幾個方法;有幾個產品族,就應該有幾個具體的工廠類。 (有什么不對的地方希望給予指出)
代碼如下:
抽象工廠:
package com.topnet.af.exercise.auto;
import com.topnet.af.exercise.auto.benz.BenzAuto;
import com.topnet.af.exercise.auto.bmw.BmwAuto;
/**
* 生產產品的接口(抽象類), 有幾種產品,就有幾個方法
* @author anfei
*
*/
public interface Factory {
//生產寶馬方法
public BmwAuto factoryBmw();
//生產奔馳方法
public BenzAuto factoryBenz();
}

具體工廠類:
package com.topnet.af.exercise.auto;
import com.topnet.af.exercise.auto.benz.BenzAuto;
import com.topnet.af.exercise.auto.benz.SportBenzAuto;
import com.topnet.af.exercise.auto.bmw.BmwAuto;
import com.topnet.af.exercise.auto.bmw.SportBwmAuto;
/**
* 生產跑車型車具體工廠類, 有幾種產品族,就有個具體工廠類 * @author anfei
*
*/
public class FactorySport implements Factory {
public BmwAuto factoryBmw() {
return new SportBwmAuto();
}
public BenzAuto factoryBenz() {
return new SportBenzAuto();
}
}

package com.topnet.af.exercise.auto;
import com.topnet.af.exercise.auto.benz.BenzAuto;
import com.topnet.af.exercise.auto.benz.BusinessBenzAuto;
import com.topnet.af.exercise.auto.bmw.BusinessBwmAuto;
import com.topnet.af.exercise.auto.bmw.BmwAuto;
/**
* 生產商務型車具體工廠類, 有幾種產品族,就有幾個具體工廠類 * @author anfei
*
*/
public class FactoryBusiness implements Factory {
public BmwAuto factoryBmw() {
return new BusinessBwmAuto();
}
public BenzAuto factoryBenz() {
return new BusinessBenzAuto();
}
}


抽象產品類:
package com.topnet.af.exercise.auto.bmw;
public interface BmwAuto {//奔馳
public void showFunction();
}


package com.topnet.af.exercise.auto.benz;
public interface BenzAuto {//寶馬
public void showFunction();
}


具體產品類:

package com.topnet.af.exercise.auto.benz;
public class BusinessBenzAuto implements BenzAuto {
public void showFunction() {
System.out.println("奔馳商務車");
}
}


package com.topnet.af.exercise.auto.benz;
public class SportBenzAuto implements BenzAuto {
public void showFunction() {
System.out.println("奔馳跑車");
}
}


package com.topnet.af.exercise.auto.bmw;
public class BusinessBwmAuto implements BmwAuto {
public void showFunction() {
System.out.println("寶馬商務車");
}
}


package com.topnet.af.exercise.auto.bmw;
public class SportBwmAuto implements BmwAuto {
public void showFunction() {
System.out.println("寶馬跑車");
}
}


測試類:
package com.topnet.af.exercise.auto;
import com.topnet.af.exercise.auto.benz.BenzAuto;
import com.topnet.af.exercise.auto.benz.SportBenzAuto;
import com.topnet.af.exercise.auto.bmw.BmwAuto;
/**
* 調用類
* @author anfei
*
*/
public class Magnate {
public static void main(String[] args) {
//跑車型工廠
Factory fs = new FactorySport();

//商務型工廠
Factory fb = new FactoryBusiness();

//奔馳
BenzAuto benz;
benz = fs.factoryBenz();
benz.showFunction();
benz = fb.factoryBenz();
benz.showFunction();

System.out.println("-----------------------");

//寶馬
BmwAuto bmw;
bmw = fs.factoryBmw();
bmw.showFunction();
bmw = fb.factoryBmw();
bmw.showFunction();
}
}


輸出結果:

奔馳跑車
奔馳商務車
-----------------------
寶馬跑車
寶馬商務車

抽象工廠模式的個人理解列子


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 福海县| 即墨市| 尖扎县| 栾川县| 镇沅| 绵竹市| 达尔| 游戏| 磴口县| 白城市| 吕梁市| 奈曼旗| 重庆市| 新乡县| 寿阳县| 布尔津县| 靖江市| 南通市| 沁源县| 留坝县| 公主岭市| 搜索| 常山县| 莱州市| 灵武市| 大余县| 桃江县| 旬邑县| 孝昌县| 金阳县| 乌拉特前旗| 乌苏市| 万载县| 余江县| 铜鼓县| 奈曼旗| 高陵县| 敦煌市| 六盘水市| 双江| 资阳市|