C#23中設計模式——簡單工場模式(創建型模式)

每天早上從家裏上班到公司門口可以選擇的交通工具

常規寫法

/// <summary>
    /// 地鐵類
    /// </summary>
    class metro
    {
        public metro()
        {
            Name = "地鐵";
            merit = "不會堵車";
            defect = "距離家很遠";
        }
        public string Name { get; set; }
        public string merit { get; set; }
        public string defect { get; set; }
    }

/// <summary>
    /// 公交類
    /// </summary>
    class transit
    {
        public transit()
        {
            Name = "公交";
            merit = "離家很近";
            defect = "會堵車";
        }
        public string Name { get; set; }
        public string merit { get; set; }
        public string defect { get; set; }
    }

/// <summary>
    /// 爬樓梯類
    /// </summary>
    class ascendthestairs
    {
        public ascendthestairs()
        {
            Name = "爬樓梯";
            merit = "可以鍛鍊身體";
            defect = "很累";
        }
        public string Name { get; set; }
        public string merit { get; set; }
        public string defect { get; set; }
    }

/// <summary>
    /// 坐電梯
    /// </summary>
    class takethelift
    {
        public takethelift()
        {
            Name = "坐電梯";
            merit = "很快就到了";
            defect = "不能鍛鍊身體";
        }
        public string Name { get; set; }
        public string merit { get; set; }
        public string defect { get; set; }
    }

上述類分成兩種,一種是從家到公司用的交通工具,一種是從公司到辦公室用的交通工具。

前臺代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            transit me = new transit();
            takethelift asc = new takethelift();
            Console.WriteLine("第一天");
            Console.WriteLine("上班乘坐的交通工具是{0},它的優點是{1},缺點是{2}", me.Name, me.merit, me.defect);
            Console.WriteLine("來到公司樓下我選擇的是{0}到達辦公室,它的優點是{1},缺點是{2}", asc.Name, asc.merit, asc.defect);


            transit me1 = new transit();
            takethelift asc1 = new takethelift();
            Console.WriteLine("第二天");
            Console.WriteLine("上班乘坐的交通工具是{0},它的優點是{1},缺點是{2}", me1.Name, me1.merit, me1.defect);
            Console.WriteLine("來到公司樓下我選擇的是{0}到達辦公室,它的優點是{1},缺點是{2}", asc1.Name, asc1.merit, asc1.defect);
            Console.Read();
        }
    }
}

 

用簡單工廠來實現:

1.創建一個接口Iduty用於抽象從家到公司的交通工具

public interface IComeUp
{
}

2.創建一個接口IComeUp用於抽象公司到辦公室的交通工具

 public interface Iduty
 {
 }

3.創建一個枚舉

    public enum transitType
    {
        metero = 1,
        transit = 2,
        takethelift = 3,
        ascendthestairs = 4
    }

4.將4個類分別繼承對應的接口

class metro : Iduty 
class takethelift : IComeUp
class transit : Iduty
class ascendthestairs : IComeUp

5.創建一個工廠類,用於實例化對象,不用客戶知道具體是怎麼實現的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class Factory
    {
        public static Iduty Show(transitType i)
        {
            Iduty id = null;
            switch (i)
            {
                case transitType.metero:
                    id = new metro();
                    break;
                case transitType.transit:
                    id = new transit();
                    break;
                default:
                    break;
            }
            return id;
        }

        public static IComeUp Show1(transitType i)
        {
            IComeUp id = null;
            switch (i)
            {
                case transitType.takethelift:
                    id = new takethelift();
                    break;
                case transitType.ascendthestairs:
                    id = new ascendthestairs();
                    break;
                default:
                    break;
            }
            return id;
        }


    }
}

6.前臺調用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            metro me = Factory.Show(transitType.metero) as metro;
            ascendthestairs asc = Factory.Show1(transitType.ascendthestairs) as ascendthestairs;
            Console.WriteLine("第一天");
            Console.WriteLine("上班乘坐的交通工具是{0},它的優點是{1},缺點是{2}", me.Name, me.merit, me.defect);
            Console.WriteLine("來到公司樓下我選擇的是{0}到達辦公室,它的優點是{1},缺點是{2}", asc.Name, asc.merit, asc.defect);


            transit me1 = new transit();
            takethelift asc1 = new takethelift();
            Console.WriteLine("第二天");
            Console.WriteLine("上班乘坐的交通工具是{0},它的優點是{1},缺點是{2}", me1.Name, me1.merit, me1.defect);
            Console.WriteLine("來到公司樓下我選擇的是{0}到達辦公室,它的優點是{1},缺點是{2}", asc1.Name, asc1.merit, asc1.defect);
            Console.Read();
        }
    }
}

總結

上述代碼用簡單工廠來編寫和常規編寫的區別:

前臺調用的時候,不需要知道對象是怎麼來的,直接調用對應的方法就可以了,對象的實例化是由工廠類去完成。

缺點:

當每加一次新類型時都要修改工廠類的代碼,這裏就違反了開閉原則,所以簡單工廠很少回去使用,但是學習簡單工廠才能學習其他的工廠模式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章