日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C#設(shè)計模式之簡單工廠模式_C#教程

作者:.NET開發(fā)菜鳥 ? 更新時間: 2022-05-03 編程語言

設(shè)計模式分類:

  • 創(chuàng)建型模式。
  • 結(jié)構(gòu)型模式。
  • 行為模式。

23種設(shè)計模式,如何記。面向?qū)ο蟮南到y(tǒng)中有很多對象,創(chuàng)建型模式解決的問題就是如何創(chuàng)建對象,何時創(chuàng)建對象,它努力的讓代碼不要太多的關(guān)注對象的具體類型,不用關(guān)注對象的創(chuàng)建細節(jié),而知需要了解對象的抽象類型,創(chuàng)建對象的工作由創(chuàng)建對象的工廠來實現(xiàn)。
面向?qū)ο蟮南到y(tǒng)中,對象如何組織,采用什么樣的結(jié)構(gòu)組織比較合理,這個是由結(jié)構(gòu)型模式來處理的。合理的使用結(jié)構(gòu)型模式可以使系統(tǒng)具備更好的靈活性、擴展性和維護性。
行為模式規(guī)定了各個對象間的應(yīng)該具備的職責。

嚴格來說,簡單工廠模式并不是23種標準模式之一,它是工廠家族中最簡單的模式,使用這個模式可以把對象的創(chuàng)建和對象的使用分離開,工廠只負責對象的創(chuàng)建,客戶端程序調(diào)用和使用對象,客戶端程序無需創(chuàng)建對象。這樣對象的創(chuàng)建放在一起,方便維護和擴展。現(xiàn)實中生產(chǎn)鞋子的工廠負責生產(chǎn)鞋子,我們不需要知道生產(chǎn)的鞋子的具體類型。

如圖所示:右上角是一個產(chǎn)品接口,我們可以使用接口或抽象類來定義一個產(chǎn)品對象。Animal類中有一個行為吃,Animal類派生出兩個子類:Dog、Penguin。這兩個類分別實現(xiàn)了吃的動作,這兩個動物其實是簡單工廠中具體的產(chǎn)品,通過他們實現(xiàn)抽象的產(chǎn)品;這些動物該如何去創(chuàng)建呢,我們可以用動物工廠AnimalFactory來創(chuàng)建具體的動物,AnimalFactory類中有一個GetAnimal的方法,在這個方法里我們可以根據(jù)傳進去的參數(shù)來創(chuàng)建具體的產(chǎn)品,工廠類和產(chǎn)品類是依賴的關(guān)系。
在客戶端,它關(guān)聯(lián)了抽象的產(chǎn)品類Animal和工廠類AnimalFactory,對客戶來說,他不需要了解具體的產(chǎn)品,只需要告訴工廠,需要什么具體的動物,動物工廠就會根據(jù)客戶端的要求來創(chuàng)建某個動物,通過簡單工廠模式,使客戶端程序與具體的產(chǎn)品之間減少耦合度。

示例:

抽象動物類:

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

namespace 簡單工廠模式
{
    /*
       動物抽象類
     * 抽象產(chǎn)品
     */
    public abstract class Animal
    {
        public abstract void Eat();
    }
}

具體動物狗類:

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

namespace 簡單工廠模式
{
    /*
       具體的產(chǎn)品類,實現(xiàn)抽象產(chǎn)品類
     */
    public class Dog:Animal
    {
        // 實現(xiàn)抽象方法
        public override void Eat()
        {
            Console.WriteLine("狗在吃飯!");
        }
    }
}

具體動物企鵝類:

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

namespace 簡單工廠模式
{
    /*
      具體產(chǎn)品類,實現(xiàn)抽象產(chǎn)品類

     */
    public class Penguin   :Animal
    {
        // 實現(xiàn)抽象方法
        public override void Eat()
        {
            Console.WriteLine("企鵝在吃飯!");
        }
    }
}

動物工廠類:

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

namespace 簡單工廠模式
{
    /*
      動物工廠類:實現(xiàn)具體的動物

     */
    public class AnimalFactory
    {
        /// 
        /// 根據(jù)客戶的選擇創(chuàng)建動物對象
        /// 
        /// 動物編號
        /// 
        public Animal GetAnimal(int witch)
        {
            Animal am = null;
            switch (witch)
            {
                case 1:
                    am = new Dog();
                    break;
                case 2:
                    am = new Penguin();
                    break;
            }

            return am;
        }
    }
}

主程序調(diào)用測試

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

namespace 簡單工廠模式
{
    class Program
    {
        static void Main(string[] args)
        {
            // 得到具體的動物 (里氏替換原則)
            Animal am = new AnimalFactory().GetAnimal(1);
            // 調(diào)用Eat()方法
            am.Eat(); // 輸出狗在吃飯

            Console.ReadKey();
        }
    }
}

測試結(jié)果:

使用接口實現(xiàn)簡單工廠模式

需求:使用面向?qū)ο蟮姆绞皆O(shè)計一個系統(tǒng),描述使用卡車從事貨運,使用公共汽車從事客運。使用工廠模式實現(xiàn)。

1、汽車接口:

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

namespace 簡單工廠模式
{
    /// 
    /// 汽車接口
    /// 
    public interface ICar
    {
        /// 
        /// 描述汽車從事的活動
        /// 
        void Work();
    }
}

2、分別定義卡車類(Truck)和公共汽車類(Bus)實現(xiàn)ICar接口

Truck類:

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

namespace 簡單工廠模式
{
    /// 
    /// 卡車類
    /// 
    public class Truck : ICar
    {
        /// 
        /// 卡車從事的活動
        /// 
        public void Work()
        {
            Console.WriteLine("卡車從事貨運");
        }
    }
}

Bus類:

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

namespace 簡單工廠模式
{
    /// 
    /// 公共汽車類
    /// 
    public class Bus:ICar
    {
        /// 
        /// 公共汽車從事的活動
        /// 
        public void Work()
        {
            Console.WriteLine("公共汽車從事客運");
        }
    }
}

3、定義汽車的工廠類

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

namespace 簡單工廠模式
{
    /// 
    /// 汽車工廠類:返回具體的汽車類
    /// 
    public class CarFactory
    {
        /// 
        /// 根據(jù)汽車編號創(chuàng)建具體的汽車對象
        /// 
        /// 汽車編號
        /// 
        public ICar GetCar(int witch)
        {
            ICar car = null;
            switch (witch)
            {
                case 1:
                    car= new Truck();
                    break;
                case 2:
                    car = new Bus();
                    break;
            }
            return car;
        }
    }
}

4、主程序調(diào)用

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

namespace 簡單工廠模式
{
    class Program
    {
        static void Main(string[] args)
        {
            // 得到具體的汽車
            ICar car = new CarFactory().GetCar(2);
            // 調(diào)用Work()方法
            car.Work();
            Console.ReadKey();
        }
    }
}

5、程序運行結(jié)果

簡單工廠模式的缺點:
增加具體產(chǎn)品時,需要修改工廠類里面生成具體產(chǎn)品的方法,這就違反了開閉原則。具體產(chǎn)品經(jīng)常發(fā)生變化時,不建議使用簡單工廠模式。

代碼下載地址:點擊下載

原文鏈接:https://www.cnblogs.com/dotnet261010/p/7352045.html

欄目分類
最近更新