设计模式-工厂模式

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

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

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

更易于扩展;

缺点:代码复杂度提高

相关推荐
Thanks_ks29 分钟前
单例模式与工厂模式精讲:设计模式的意图、实现与在 Java 项目中的实际应用
java·spring·单例模式·设计模式·工厂模式·线程安全·面向对象设计
lcj251121 小时前
【日志系统实战】同步/异步日志系统:手写日志等级模块,吃透enum class与7大分级,面试不慌
c++·设计模式·面试·c++11·日志系统
电子科技圈1 天前
UCIe桥接IP的选型与验证:行业高频问题解析
设计模式·软件工程·软件构建·设计规范
Bode_20021 天前
交互系统研究图谱
设计模式·设计方法
李航19831 天前
Codex现代风格卧室设计案例1
人工智能·python·计算机视觉·设计模式·ai
善良勤劳勇敢而又聪明的老杨1 天前
【AI编程系列】RAG 常用设计模式
设计模式·ai编程
Meteors.1 天前
Android 项目常见问题与 23 种设计模式应用对照表(含 Kotlin 示例)
android·设计模式·kotlin
Mh3 天前
基于踩过的坑,浅聊一下vh、svh、lvh、dvh
前端·css·设计模式
瑶池酒剑仙4 天前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio
HarmonSir6 天前
Codex 架构解析:一条对话如何长成一个运行时系统
后端·设计模式·架构