设计模式-工厂模式

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

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();
    }
}

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

更易于扩展;

缺点:代码复杂度提高

相关推荐
繁华似锦respect7 小时前
C++ 智能指针底层实现深度解析
linux·开发语言·c++·设计模式·代理模式
繁华似锦respect14 小时前
单例模式出现多个单例怎么确定初始化顺序?
java·开发语言·c++·单例模式·设计模式·哈希算法·散列表
繁华似锦respect15 小时前
C++ 内存分配器-allocator
开发语言·c++·设计模式
Zaralike15 小时前
Java设计模式
java·开发语言·设计模式
开心香辣派小星16 小时前
23种设计模式-14迭代器模式
设计模式·迭代器模式
2301_8035545218 小时前
Pimpl(Pointer to Implementation)设计模式详解
c++·算法·设计模式
__万波__19 小时前
二十三种设计模式(七)--桥接模式
设计模式·桥接模式
老朱佩琪!19 小时前
在Unity中实现状态机设计模式
开发语言·unity·设计模式
达斯维达的大眼睛19 小时前
常用设计模式总结
设计模式
老朱佩琪!20 小时前
Unity建造者设计模式
设计模式