设计模式(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)
- 创建子类工厂
如果现在又又需要增加工种,那么在简单工厂的基础上我们只能通过修改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();