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

gl账号注册网站百度极速版下载

gl账号注册网站,百度极速版下载,河北邢台路桥建设公司网站,重庆免费公司建网站设计模式 文章目录 设计模式工厂模式创建方法(Create Methods)简单工厂(Simple Factory)工厂方法(Method Factory)抽象工厂(Abstract Factroy) 策略模式 工厂模式 创建方法&#xf…

设计模式

文章目录

  • 设计模式
    • 工厂模式
      • 创建方法(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.hkea.cn/news/336975/

相关文章:

  • 厦门做网站设计电商seo优化
  • wordpress视频点播seo技术是干什么的
  • 网站推广是怎么做的网络营销专业如何
  • 平面设计线上兼职上海网站seo
  • 个性化网站定制价格今日热点
  • 做网站的艰辛免费个人网站申请
  • 网站改版需要多久网站设计与制作毕业论文范文
  • 深圳横岗网站建设网站建设的推广渠道
  • 有没有什么网站免费做名片2023年新闻小学生摘抄
  • 新网金商网站外链查询工具
  • 网站建设的进度竞价托管选择微竞价
  • 网站快速网站推广怎么做一个公司网站
  • 旅游网站模板htmlseo品牌优化整站优化
  • 方圆网站建设aso优化重要吗
  • 做购实惠网站的意义好用的搜索引擎有哪些
  • 怎么把自己笔记本做服务器做个网站搭建网站基本步骤
  • jeecms做企业网站成都网站建设公司排名
  • 沈阳招聘网站开发地推项目平台
  • 798艺术区成都seo达人
  • 平度网站建设抖音代运营收费详细价格
  • 株洲网站优化找哪家seo优化的价格
  • 找印度人做网站sem竞价推广公司
  • 山西网站推广公司网站关键词优化怎么弄
  • 微信分销是什么重庆优化seo
  • 武汉企业网站推广方案永久免费无代码开发平台网站
  • 网站开发岗位群怎样推广产品
  • 桐城市美丽乡村建设专题网站石家庄整站优化技术
  • 北京建网站的公司哪个比较好郑州seo价格
  • 进空间的网站网络营销常见的工具
  • wordpress发文章的id怎么不连续如何做好搜索引擎优化工作