523,设计模式之工厂模式(1,简单工厂模式:简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现 。2...

三种模式

  • 简单工厂模式:简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
image.png
  1. Factory(工厂):核心部分,负责实现创建所有产品的内部逻辑,工厂类可以被外界直接调用,创建所需对象
  2. Product(抽象类产品):工厂类所创建的所有对象的父类,封装了产品对象的公共方法,所有的具体产品为其子类对象
  3. ConcreteProduct(具体产品):简单工厂模式的创建目标,所有被创建的对象都是某个具体类的实例。它要实现抽象产品中声明的抽象方法(有关抽象类)

实现

abstract class Product
{
    public void MethName()
    {
        //公共方法的实现
    }
    public abstract void MethodDiff();
    //声明抽象业务方法
}
class ConcreteProductA : Product
{
    public override void MethodDiff()
    {
        //业务方法的实现
    }
}

class Factory
{
    public static Product GetProduct(string arg)
    {
        Product product =  null;
        if(arg.Equals("A")
        {
            product = new ConcreteProductA();
            //init
        }
        else if(arg.Equals("B"))
        {
            product = new ConcreteProductB();
            //init
        }
        else
        {
        ....//其他情况
        }
        return product;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Product product;
        product = Factory.GetProduct("A");//工厂类创建对象
        Product.MethName();
        product.MethodDiff();
    }
}
  • 工厂模式:抽象了工厂接口的具体产品,应用程序的调用不同工厂创建不同产品对象。(抽象产品)
class MobileFactory:
        """ 工厂模式 生产手机的工厂"""

        def get_mobile(self):
            pass


    class HWFactory(MobileFactory):
        def get_mobile(self):
            return 'get a HW phone'


    class IphoneFactory(MobileFactory):
        def get_mobile(self):
            return 'get an iphone'


    hw, ip = HWFactory(), IphoneFactory()
    print(hw.get_mobile())  # get a HW phone
    print(ip.get_mobile())  # get an iphone
  • 抽象工厂模式:在工厂模式的基础上抽象了工厂,应用程序调用抽象的工厂发发创建不同产品对象。(抽象产品+抽象工厂)
"""
    抽象工厂模式
    【假设】华为和苹果都生产手机和屏幕,而我家只生产屏幕
    """


    # 有屏幕和手机两款产品
    class Screen:
        def __init__(self):
            print('i am a screen')


    class Mobile:
        def __init__(self):
            print('i am a mobile')


    # 三个厂家各自的屏幕和手机
    class HWScreen(Screen):
        def __init__(self):
            print('HW screen')


    class IphoneScreen(Screen):
        def __init__(self):
            print('Iphone screen')


    class MyScreen(Screen):
        def __init__(self):
            print('My screen')


    class HWMobile(Mobile):
        def __init__(self):
            print('HW Mobile')


    class IphoneMobile(Mobile):
        def __init__(self):
            print('Iphone Mobile')


    # 生产工厂
    class Factory:
        def get_screen(self):
            pass

        def get_mobile(self):
            pass


    # 各家自己的工厂
    class HWFactory(Factory):
        def get_mobile(self):
            return HWMobile()

        def get_screen(self):
            return HWScreen()


    class IphoneFactory(Factory):
        def get_screen(self):
            return IphoneScreen()

        def get_mobile(self):
            return IphoneMobile()


    class MyFactory(Factory):
        def get_screen(self):
            return MyScreen()


    # 我要生产东西咯
    hw, ip, my = HWFactory(), IphoneFactory(), MyFactory()
    hw.get_mobile()  # HW Mobile
    hw.get_screen()  # HW screen
    ip.get_mobile()  # Iphone Mobile
    ip.get_screen()  # Iphone screen
    my.get_screen()  # My screen

你可能感兴趣的:(523,设计模式之工厂模式(1,简单工厂模式:简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现 。2...)