亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統 2083 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條評論
主站蜘蛛池模板: 97久久精品人人澡人人爽 | 一级毛片日韩 | 国产三级观看久久 | 色综合五月激情综合色一区 | 四虎地址8848在线观看 | 福利午夜最新 | 天天躁狠狠躁狠狠躁夜夜躁 | 91久久精品国产91性色tv | 伊人97| 神马97| 美女福利在线观看 | 波多野结衣乳巨码无在线观看 | 在线看亚洲 | 成人毛片基地 | 日本黄页在线观看 | 不卡精品国产_亚洲人成在线 | 午夜视频福利在线观看 | 久久久在线 | 99热这里只有精品首页精品 | 国产一区二区在线视频观看 | 欧美亚洲另类综合 | 欧美午夜网站 | 中文字幕欧美日韩高清 | 一级作爱视频免费观看 | 在线观看国产一区二三区 | 国内精品视频免费观看 | 女女同免费播放毛片 | 国产高清自拍一区 | 天天草b | 特黄十八岁大片 | 色综合久久综合欧美综合网 | 亚洲视频日韩视频 | 99热热热| 四虎影院2022| 美女日日日 | 日本国产亚洲 | 国产免费资源 | 日韩精品一二三区 | 欧美视频在线不卡 | 欧美中文字幕一二三四区 | 久久精品国产精品亚洲 |