设计模式-工厂模式

工厂模式:该模式是将简单工厂模式中的工厂作为一个基类(接口),当需要创建的产品(具体子类实例)增加时,不修改原工厂的创建方法,而是增加一个具体工厂子类,由该子类去创建新增的产品,将产品子类的实例化延迟到工厂子类中。

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface Phone
{
    void Name();
}

public class IPhone : Phone
{
    public void Name()
    {
        Debug.Log("苹果手机");
    }
}

public class AZPhone : Phone
{
    public void Name()
    {
        Debug.Log("安卓手机");
    }
}

这是工厂的实现

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface IFactory
{
    Phone Creat();
}

public class IPhoneFactory : IFactory
{
    public Phone Creat()
    {
        return new IPhone();
    }
}

public class AZPhoneFactory : IFactory
{
    public Phone Creat()
    {
        return new AZPhone();
    }
}

优点:降低了耦合性,只需要知道要什么产品,不用管创建过程;

更易于扩展;

缺点:代码复杂度提高

相关推荐
Sam_Deep_Thinking5 小时前
结算分摊的策略模式:不同营销活动的扣点计算方案
java·设计模式·架构·系统架构
故渊at8 小时前
系列一:架构思想进阶 | 第3篇 SOLID 原则与设计模式实战:从“代码搬运工”到“架构师”的必经之路
观察者模式·设计模式·重构·架构·代理模式
老码观察1 天前
设计模式实战解读(十一):外观模式——给复杂系统套一层壳
python·设计模式·外观模式
AI大法师1 天前
奥迪 AUDI 案例:母品牌和新业务怎么拆?
大数据·设计模式·汽车
bryant_meng1 天前
【Design Patterns】23 Design Patterns: The Ultimate Developer‘s Toolkit
设计模式·编程·计算机科学·设计·工程
狂人开飞机1 天前
18. 中介者模式(Mediator Pattern)
设计模式·c#·中介者模式
咖啡八杯1 天前
GoF设计模式——外观模式
java·设计模式·外观模式
江湖中的阿龙1 天前
23种设计模式
java·开发语言·设计模式
basketball6161 天前
设计模式入门:7. 策略模式详解 C++实现
c++·设计模式·策略模式
thisiszdy1 天前
<设计模式> 生产者-消费者模式
设计模式