当前位置: 首页 > news >正文

设计模式(Unity)——更新中

设计模式

文章目录

  • 设计模式
    • 工厂模式
      • 创建方法(Create Methods)
      • 简单工厂(Simple Factory)
      • 工厂方法(Method Factory)
      • 抽象工厂(Abstract Factroy)
    • 策略模式

工厂模式

创建方法(Create Methods)

调用构造函数进行封装,隐藏创建细节体现代码意图,增强代码可维护性

例如我们有一个部门,部门中有一个工种程序员

public class Programmer { } // 程序员类
public class Department // 部门类
{// 这是一个创建方法(也可以写成静态)public Programmer CreateEmployee(){return new Programmer();}
}

调用创建方法

Department department = new Department();
Programmer programmer = department.CreateEmployee();

简单工厂(Simple Factory)

现在部门增加了一个工种,设计师工种,那么我们使用简单工厂模式,通过给定参数的不同来创建不同的对象

// 工种枚举
public enum EEmployeeType
{Programmer,Designer
}
public class Employee {} // 工种父类
public class Programmer : Employee { } // 程序员类
public class Designer : Employee { } // 设计师类
public class Department // 部门类
{// 用简单工厂实现创建方法public Employee CreateEmployee(EEmployeeType employeeType){Employee employee = new Employee();switch (employeeType){case EEmployeeType.Programmer:employee = new Programmer();break;case EEmployeeType.Designer:employee = new Designer();break;default:throw new System.ArgumentException("Invalid employee type");}return employee;}
}

调用创建方法

Department department = new Department();
Employee programmer = department.CreateEmployee(EEmployeeType.Programmer);
Employee designer = department.CreateEmployee(EEmployeeType.Designer);

工厂方法(Method Factory)

  1. 创建子类工厂

如果现在又又需要增加工种,那么在简单工厂的基础上我们只能通过修改CreateEmployee方法的实现细节,不符合违背了开闭原则(对象应该对扩展开放,修改封闭),那么我们使用工厂方法,将对象的创建方法下发到子类工厂中

public abstract class Department // 父类工厂
{public abstract Employee CreateEmployee();
}public class ITDepartment : Department // 程序员工厂
{public override Employee CreateEmployee(){return new Programmer();}
}public class UIDepartment : Department // 设计师工厂
{public override Employee CreateEmployee(){return new Designer();}
}

创建方法调用,通过使用不同的子类工厂来完成创建,这样扩展新工种时只需要创建类和子类工厂

ITDepartment itDepartment = new ITDepartment();
itDepartment.CreateEmployee();
UIDepartment uiDepartment = new UIDepartment();
uiDepartment.CreateEmployee();

2.创建模板方法

如果这个时候需要对每个新创建的员工都实现一个注册账户的方法,可以发现从创建对象到注册的业务执行是固定的,但是每一个接口所对应的具体实现不同

加入注册方法

public abstract class Employee // 工种父类
{public abstract string RegisterAccount(); // 注册方法
}public class Programmer : Employee // 程序员类
{public override string RegisterAccount(){return "IT" + Random.Range(0, 100);}}public class Designer : Employee // 设计师类
{public override string RegisterAccount(){return "UI" + Random.Range(100, 200);}
} 

实现父类工厂中的模板方法

public abstract class Department // 部门抽象类
{protected abstract Employee CreateEmployee();public Employee Onboard(){employee = CreateEmployee(); // 具体实现通过子类employee.RegisterAccount();return employee;}
}

创建方法调用

ITDepartment itDepartment = new ITDepartment();
itDepartment.Onboard();UIDepartment uiDepartment = new UIDepartment();
uiDepartment.Onboard();

抽象工厂(Abstract Factroy)

加入程序员有程序项目,设计师有设计项目,使用抽象工厂

public abstract class Project // 项目父类
{public abstract void AssignTo(); // 项目接口
}public class ITProject : Project // 程序项目类
{public override void AssignTo(){Debug.Log("Assign an IT to project");}
}public class UIProject : Project // 设计师项目类
{public override void AssignTo(){Debug.Log("Assign an UI to project");}
}

重构部门抽象类,加入项目类

public abstract class Department // 部门抽象类
{public abstract Employee CreateEmployee();public abstract Project CreateProject();
}
public class ITDepartment : Department
{public override Employee CreateEmployee(){return new Programmer();}public override Project CreateProject(){return new ITProject();}}public class UIDepartment : Department
{public override Employee CreateEmployee(){return new Designer();}public override Project CreateProject(){return new UIProject();}
}

创建工厂管理类,实现一对多的关系,例如一个程序员部门,可以有多个项目,多个员工

// 工厂管理类
public class DepartManager
{private Department _department;public List<Project> _projects;public Dictionary<string, Employee> _employees;public DepartManager(Department department){_department = department;}public Project CreateProject(){if (_projects == null) _projects = new List<Project>();Project project = _department.CreateProject();_projects.Add(project);return project;}public string CreateEmployee(){if (_employees == null) _employees = new Dictionary<string, Employee>();Employee employee = _department.CreateEmployee();string accountNo = employee.RegisterAccount();_employees.Add(accountNo, employee);return accountNo;}
}

方法调用

DepartManager itDepManager = new DepartManager(new ITDepartment());
itDepManager.CreateProject();
itDepManager.CreateEmployee();DepartManager uiDepManager = new DepartManager(new UIDepartment());
itDepManager.CreateProject();
itDepManager.CreateEmployee();

策略模式

http://www.lryc.cn/news/482483.html

相关文章:

  • 小程序中引入下载到本地的iconfont字体图标加载不出来问题解决
  • 百度富文本禁止编辑
  • C++开发基础之使用librabbitmq库实现RabbitMQ消息队列通信
  • 头歌网络安全(11.12)
  • 洛谷 P1725 琪露诺(线段树优化dp)
  • 【LeetCode】【算法】19. 删除链表的倒数第N个结点
  • Python爬虫 | 爬取豆瓣电影Top250的数据
  • mac 中python 安装mysqlclient 出现 ld: library ‘ssl‘ not found错误
  • 完全清除:苹果手机照片怎么彻底删除
  • 高德地图多个图片组成标点(自定义点标记内容)
  • 02-1_MVCC版本链清理
  • 探索Python视频处理的瑞士军刀:ffmpeg-python库
  • 进程间通信 - 通道
  • 华为数通HCIA系列第5次考试-【2024-46周-周一】
  • 【Linux】如何通过终端命令查看当前可用网络 WIFI + 设置已配置网络的连接优先级 + 连接/断连网络
  • 华为路由策略配置
  • Debezium日常分享系列之:异步 Debezium 嵌入式引擎
  • leetcode206. Reverse Linked List
  • 【MATLAB源码-第291期】基于matlab的AMI编码解码系统仿真,输出各个节点波形。
  • springboot苍穹外卖实战:十一:复盘总结
  • 基于Python的药房管理系统
  • chat2db数据库图形化工具
  • 弱口令整改方案:借助双因子认证加强账号密码安全
  • 动态代理的优势是什么?
  • 将大型语言模型(如GPT-4)微调用于文本续写任务
  • 引入了JUnit框架 却报错找不到:java.lang.ClassNotFoundException
  • 深度学习:tensor的定义与维度
  • 基于Python的膳食健康系统
  • FFmpeg 4.3 音视频-多路H265监控录放C++开发十三:将AVFrame转换成AVPacket。视频编码原理.编码相关api
  • 算法——移除元素(leetcode27)