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

學無先后,達者為師

網站首頁 編程語言 正文

C#設計模式之簡單工廠模式_C#教程

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

設計模式分類:

  • 創建型模式。
  • 結構型模式。
  • 行為模式。

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

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

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

示例:

抽象動物類:

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

namespace 簡單工廠模式
{
    /*
       動物抽象類
     * 抽象產品
     */
    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 簡單工廠模式
{
    /*
       具體的產品類,實現抽象產品類
     */
    public class Dog:Animal
    {
        // 實現抽象方法
        public override void Eat()
        {
            Console.WriteLine("狗在吃飯!");
        }
    }
}

具體動物企鵝類:

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

namespace 簡單工廠模式
{
    /*
      具體產品類,實現抽象產品類

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

動物工廠類:

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

namespace 簡單工廠模式
{
    /*
      動物工廠類:實現具體的動物

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

            return am;
        }
    }
}

主程序調用測試

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);
            // 調用Eat()方法
            am.Eat(); // 輸出狗在吃飯

            Console.ReadKey();
        }
    }
}

測試結果:

使用接口實現簡單工廠模式

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

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)實現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
    {
        /// 
        /// 根據汽車編號創建具體的汽車對象
        /// 
        /// 汽車編號
        /// 
        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、主程序調用

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);
            // 調用Work()方法
            car.Work();
            Console.ReadKey();
        }
    }
}

5、程序運行結果

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

代碼下載地址:點擊下載

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

欄目分類
最近更新