欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#設計模式之簡單工廠模式

 更新時間:2022年03月07日 10:50:17   作者:.NET開發(fā)菜鳥  
這篇文章介紹了C#設計模式之簡單工廠模式,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

設計模式分類:

  • 創(chuàng)建型模式。
  • 結構型模式。
  • 行為模式。

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

嚴格來說,簡單工廠模式并不是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)品類是依賴的關系。
在客戶端,它關聯(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
    {
        /// <summary>
        /// 根據(jù)客戶的選擇創(chuàng)建動物對象
        /// </summary>
        /// <param name="witch">動物編號</param>
        /// <returns></returns>
        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();
        }
    }
}

測試結果:

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

需求:使用面向對象的方式設計一個系統(tǒng),描述使用卡車從事貨運,使用公共汽車從事客運。使用工廠模式實現(xiàn)。

1、汽車接口:

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

namespace 簡單工廠模式
{
    /// <summary>
    /// 汽車接口
    /// </summary>
    public interface ICar
    {
        /// <summary>
        /// 描述汽車從事的活動
        /// </summary>
        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 簡單工廠模式
{
    /// <summary>
    /// 卡車類
    /// </summary>
    public class Truck : ICar
    {
        /// <summary>
        /// 卡車從事的活動
        /// </summary>
        public void Work()
        {
            Console.WriteLine("卡車從事貨運");
        }
    }
}

Bus類:

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

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

3、定義汽車的工廠類

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

namespace 簡單工廠模式
{
    /// <summary>
    /// 汽車工廠類:返回具體的汽車類
    /// </summary>
    public class CarFactory
    {
        /// <summary>
        /// 根據(jù)汽車編號創(chuàng)建具體的汽車對象
        /// </summary>
        /// <param name="witch">汽車編號</param>
        /// <returns></returns>
        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、程序運行結果

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

代碼下載地址:點擊下載

到此這篇關于C#設計模式之簡單工廠模式的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論