( Design Patterns ) Creational Design Patterns 1 -- Factory Pattern

Definition

Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses

Advantages

  1. Good Encapsulation: The called only need to care about the class name, which helps to decouple interdependence.
  2. Good Expandability: Product class can be easily added by modifying or extending subclasses.
  3. Hide products Details:Callers and product classes are communicating by interfaces. The change of class implementation will not affect how to use these classes.
  4. Obey SOLID principle:

Simple Factory Pattern(Static Factory Pattern)

In simple factory pattern, we have a factory class which has a method that returns different types of object based on given input

Simple Factory Pattern UML
Simple Factory Pattern UML

public abstract class BMW
    {
        public string CarName { get; set; }
    }

    public class BMW320 : BMW
    {
        public BMW320()
        {
            this.CarName = "BMW320";
        }
    }

    public class BMW532 : BMW
    {
        public BMW532()
        {
            this.CarName = "BMW532";
        }
    }

    public class SimpleFactoryPattern
    {
        public BMW createBMW(int type)
        {
            switch (type)
            {
                case 3:
                    return new BMW320();
                case 5:
                    return new BMW532();
                default:
                    return new BMW320();
            }
        }

        public static void Main()
        {
            var factory = new SimpleFactoryPattern();
            var bmw3 = factory.createBMW(3);
            var bmw5 = factory.createBMW(5);
        }
    }
  • Simple Factory Pattern Issues
    • Simple factory pattern violates Open Close Principle of SOLID principles. If we need to add another subclass, we have to change the function in factory class.

Factory Method Pattern

  • The factory method pattern is a design pattern that allows for the creation of objects without specifying the type of object that is to be created in code. A factory class contains a method that allows determination of the created type at run-time.

  • Components:

    • Abstract Factory Class: Base class of Concrete Factory Class祠肥。
    • Concrete Factory Class:Concrete factory class, which contains the detailed logical related code. It is responsible for creating concrete product objects.
    • Abstract Product Class:Base class of Concrete Product Class悬秉。
    • Concrete Product Class:Concrete product class, which is created by Concrete Factory Class.
Factory Method Pattern UML
    public abstract class BMW
{
    public string CarName { get; set; }
}

public class BMW320 : BMW
{
    public BMW320()
    {
        this.CarName = "BMW320";
    }
}

public class BMW532 : BMW
{
    public BMW532()
    {
        this.CarName = "BMW532";
    }
}

public interface FactoryBMW
{
    BMW CreateBMW();
}

public class FactoryBMW320 : FactoryBMW
{
    public BMW CreateBMW()
    {
        return new BMW320();
    }
}

public class FactoryBMW532 : FactoryBMW
{
    public BMW CreateBMW()
    {
        return new BMW532();
    }
}

public class FactoryMethodPattern
{
    public static void Main()
    {
        var factoryBMW320 = new FactoryBMW320();
        var bmw3 = factoryBMW320.CreateBMW();

        var factoryBMW532= new FactoryBMW532();
        var bmw5 = factoryBMW532.CreateBMW();
    }
}

Advantage of Factory Method Pattern

  • Follows Open Close Principle. When new requirement came, just need to create an additional Factory.

Abstract Factory Pattern

The abstract factory pattern is a design pattern that allows for the creation of groups of related objects without the requirement of specifying the exact concrete classes that will be used. One of a number of factory classes generates the object sets.

  • Abstract Factory Pattern VS Factory Method/Simple Factory Pattern

    • Abstract Factory Pattern: It provides different kind of Factories, each provide a particular kind of related objects.
    • Factory Method/Simple Factory Pattern: It provides one kind on objects only.
  • Components:

    • Abstract Factory Class: Base class of Concrete Factory Class照瘾。
    • Concrete Factory Class:Concrete factory class, which contains the detailed logical related code. It is responsible for creating concrete product objects.
    • Abstract Product Class:Base class of Concrete Product Class辆它。
    • Concrete Product Class:Concrete product class, which is created by Concrete Factory Class.
Abstract Factory Pattern UML
public abstract class BMW
{
    public string CarName { get; set; }
}

public class BMW320 : BMW
{
    public BMW320()
    {
        this.CarName = "BMW320";
    }
}

public class BMW532 : BMW
{
    public BMW532()
    {
        this.CarName = "BMW532";
    }
}

public abstract class aircondition
{
    public string ConditionName { get; set; }
}
public class AirConditionBMW320 : aircondition
{
    public AirConditionBMW320()
    {
        this.ConditionName = "BMW320 AC";
    }
}
public class AirConditionBMW532 : aircondition
{
    public AirConditionBMW532()
    {
        this.ConditionName = "BMW532 AC";
    }
}


public interface FactoryBMW
{
    BMW createBMW();
    aircondition createAirC();
}

public class FactoryBMW320 : FactoryBMW
{
    public BMW createBMW()
    {
        return new BMW320();
    }
    public aircondition createAirC()
    {
        return new AirConditionBMW320();
    }
}

public class FactoryBMW523 : FactoryBMW
{
    public BMW createBMW()
    {
        return new BMW532();
    }
    public aircondition createAirC()
    {
        return new AirConditionBMW532();
    }

    public static void Main()
    {
        var factoryBMW320 = new FactoryBMW320();
        var bmw320 = factoryBMW320.createBMW();
        var bmw320Ac = factoryBMW320.createAirC();
    }
}
  • Disadvantages:
    If we change the interface provide by Abstract Factory itself, all factories need to change.

Reference.

Design Patterns 1 of 3 - Creational Design Patterns - CodeProject
Factory Patterns - Abstract Factory Pattern - CodeProject

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末谣光,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子半醉,更是在濱河造成了極大的恐慌隔嫡,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件书在,死亡現(xiàn)場離奇詭異灰伟,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蕊温,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門袱箱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人义矛,你說我怎么就攤上這事发笔。” “怎么了凉翻?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵了讨,是天一觀的道長。 經(jīng)常有香客問我,道長前计,這世上最難降的妖魔是什么胞谭? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮男杈,結(jié)果婚禮上丈屹,老公的妹妹穿的比我還像新娘。我一直安慰自己伶棒,他們只是感情好旺垒,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著肤无,像睡著了一般先蒋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上宛渐,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天竞漾,我揣著相機(jī)與錄音,去河邊找鬼窥翩。 笑死业岁,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的寇蚊。 我是一名探鬼主播叨襟,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼幔荒!你這毒婦竟也來了糊闽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤爹梁,失蹤者是張志新(化名)和其女友劉穎右犹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體姚垃,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡念链,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了积糯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掂墓。...
    茶點(diǎn)故事閱讀 40,505評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖看成,靈堂內(nèi)的尸體忽然破棺而出君编,到底是詐尸還是另有隱情,我是刑警寧澤川慌,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布吃嘿,位于F島的核電站祠乃,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏兑燥。R本人自食惡果不足惜亮瓷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望降瞳。 院中可真熱鬧嘱支,春花似錦、人聲如沸挣饥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽亮靴。三九已至,卻和暖如春于置,著一層夾襖步出監(jiān)牢的瞬間茧吊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工八毯, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留搓侄,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓话速,卻偏偏與公主長得像讶踪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子泊交,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容