最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

【設(shè)計模式(一)】簡單工廠——計算器用例

2019-07-29 00:13 作者:HarrickHeng  | 我要投稿

溫故而知新,工作半年來最大的感觸就是知識儲備的不足與基礎(chǔ)知識的薄弱,特此復習一下23種設(shè)計模式,并用一些典型的應用場景來模擬該種模式的使用。

暫時使用的語言是微軟的Typescript,與C#、JAVA等語言在某些地方會有一些出入。

簡單工廠(Simple Factory)

首先它不屬于23種設(shè)計模式,但之后的抽象工廠、工廠方法模式都由此演化而來,而且在工廠創(chuàng)建類比較少的情況下也會使用,因此算是設(shè)計模式中的”父類“了。

廢話不多說,先上需求:做一個簡單的計算器(加減乘除)。

類圖

?

Operation 運算類

class Operation {
? ?private _numberA: number;
? ?private _numberB: number;

? ?constructor(){
? ?}

? ? get numberA(): number
? ?{
? ? ? ?return this._numberA;
? ?}

? ? set numberA(num: number)
? ?{
? ? ? ?this._numberA = num;
? ?}

? ? get numberB(): number
? ?{
? ? ? ?return this._numberB;
? ?}

? ? set numberB(num: number)
? ?{
? ? ? ?this._numberB = num;
? ?}
? ?
? ?/**
? ? * 得到結(jié)果
? ? */

? ?public GetResult(): number
? ?{
? ? ? ?let result: number = 0;
? ? ? ?return result;
? ?}
}

OperationAdd、OperationSub、OperationMul、OperationDiv 加減乘除類

(繼承于運算類)

/**加類**/
class OperationAdd extends Operation {

? ?constructor() {
? ? ? ?super(); ? ? ?
? ?}

? ?public GetResult(): number
? ?{
? ? ? ?super.GetResult();
? ? ? ?let result: number = 0;
? ? ? ?result = this.numberA + this.numberB;
? ? ? ?return result;
? ?}
}

/**減類**/
class OperationSub extends Operation {
? ?
? ?constructor() {
? ? ? ?super(); ? ? ?
? ?}

? ?public GetResult(): number
? ?{
? ? ? ?super.GetResult();
? ? ? ?let result: number = 0;
? ? ? ?result = this.numberA - this.numberB;
? ? ? ?return result;
? ?}
}

/**乘類**/
class OperationMul extends Operation {
? ?
? ?constructor() {
? ? ? ?super(); ? ? ?
? ?}

? ?public GetResult(): number
? ?{
? ? ? ?super.GetResult();
? ? ? ?let result: number = 0;
? ? ? ?result = this.numberA * this.numberB;
? ? ? ?return result;
? ?}
}

/**除類**/
class OperationDiv extends Operation {
? ?
? ?constructor() {
? ? ? ?super(); ? ? ?
? ?}

? ?public GetResult(): number
? ?{
? ? ? ?super.GetResult();
? ? ? ?let result: number = 0;
? ? ? ?if (this.numberB == 0)
? ? ? ? ? ?throw (new Error("除數(shù)不能為0!"));
? ? ? ?result = this.numberA / this.numberB;
? ? ? ?return result;
? ?}
}

OperationFactory 簡單運算工廠類(將以上類通過不同條件實例化)

class OperationFactory {

? ?constructor() {
? ?}

? ?public static createOperate (operate: string): Operation {
? ? ? ?let oper: Operation = null;
? ? ? ?switch(operate) {
? ? ? ? ? ?case "+":
? ? ? ? ? ? ? ?oper = new OperationAdd();
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case "-":
? ? ? ? ? ? ? ?oper = new OperationSub();
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case "*":
? ? ? ? ? ? ? ?oper = new OperationMul();
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case "/":
? ? ? ? ? ? ? ?oper = new OperationDiv();
? ? ? ? ? ? ? ?break;
? ? ? ?}
? ? ? ?return oper;
? ?}
}

客戶端測試

let oper: Operation;
oper = OperationFactory.createOperate("/"); //輸入條件,生產(chǎn)除類的實例
oper.numberA = 5; //輸入數(shù)字A
oper.numberB = 0; //輸入數(shù)字B
let res: number = oper.GetResult(); //得出運算結(jié)果
console.log(oper.numberA,oper.numberB,res); //報錯:除數(shù)不能為0!

優(yōu)缺點

優(yōu)點:只需要傳入一個正確的參數(shù),就可以獲取所需要的對象而無須知道其創(chuàng)建細節(jié)。

缺點:工廠類的職責相對過重,增加新的產(chǎn)品需要修改工廠類的判斷邏輯,違背開閉原則。


我的博客園:https://www.cnblogs.com/harrickheng/


【設(shè)計模式(一)】簡單工廠——計算器用例的評論 (共 條)

分享到微博請遵守國家法律
怀安县| 福贡县| 湾仔区| 东兰县| 响水县| 富民县| 梁平县| 肃南| 合作市| 浮梁县| 哈巴河县| 五台县| 广平县| 陆丰市| 长宁县| 和龙市| 和政县| 观塘区| 德钦县| 思南县| 万宁市| 靖远县| 博兴县| 武威市| 溧阳市| 辉南县| 新营市| 武义县| 南城县| 庆安县| 鄂温| 陇川县| 儋州市| 赣榆县| 游戏| 宁德市| 仙居县| 高陵县| 香港 | 突泉县| 邵武市|