建造者模式-C#改良实现
2024-09-19 19:08:01
区分网上已有的一般建造者模式实现,个人觉得实现太单一了,自己google查了一些好的实现,挑了其中比较适合的,做个笔记。
# region 标准Builder模式实现
// 产品
class Television
{
public string Code { get; set; }
public string DisplayTechnology { get; set; }
public string HDFormat { get; set; }
public string ScreenType { get; set; }
public string Size { get; set; }
public string Feature { get; set; }
}
// 抽象Builer
abstract class TelevisionBuilder{
protected Television television;
public abstract void SetCode();//ProductCode
public abstract void SetDisplayTechnology(); //LCD, LED, OLED, Plasma, CRT
public abstract void SetHDFormat(); // Full HD, HD Ready, Ultra HD, None
public abstract void SetScreenType(); //Curved, Flat, Standard
public abstract void SetSize(); // 22, 32, 40, 42, 54
public abstract void SetFeature(); //3D, Smart, Standard , HDMI Ports, USB Ports, Built in WIFI, Ethernet, Remote
//获取产品
public Television Television { get => television; }
}
// 具体Builder(具体实现1)
class FullHD40TVBuilder : TelevisionBuilder
{
public FullHD40TVBuilder() => television = new Television();
public override void SetCode() => television.Code = "FullHD40TV";
public override void SetDisplayTechnology() => television.DisplayTechnology = "LCD";
public override void SetHDFormat() => television.HDFormat = "FullHD";
public override void SetScreenType() => television.ScreenType = "Flat";
public override void SetSize() => television.Size = "40";
public override void SetFeature() => television.Feature = "1 HDMI Ports, 1 USB Ports, Remote";
}
// 具体Builder(具体实现2)
class SMARTLED54TVBuilder : TelevisionBuilder
{
public SMARTLED54TVBuilder() => television = new Television();
public override void SetCode() => television.Code = "SMARTLED54TV";
public override void SetDisplayTechnology() => television.DisplayTechnology = "LED";
public override void SetHDFormat() => television.HDFormat = "FullHD";
public override void SetScreenType() => television.ScreenType = "Flat";
public override void SetSize() => television.Size = "54";
public override void SetFeature() => television.Feature = "2 HDMI Ports, 2 USB Ports, Built in WIFI, Ethernet, Remote";
}
// 环境角色(解耦作用)
class TelevisionContext {
public void Construction(TelevisionBuilder builder)
{
builder.SetCode();
builder.SetDisplayTechnology();
builder.SetFeature();
builder.SetHDFormat();
builder.SetScreenType();
builder.SetSize();
}
}
#endregion
// 在客户端调用:
var builder1 = new SMARTLED54TVBuilder();
var builder2 = new FullHD40TVBuilder();
var ctx = new TelevisionContext();
ctx.Construction(builder1);
ctx.Construction(builder2);
var product1 = builder1.Television;
var product2 = builder2.Television;
Console.WriteLine($"产品1生产完成,详细:{JsonConvert.SerializeObject(product1)}");
Console.WriteLine($"产品2生产完成,详细:{JsonConvert.SerializeObject(product2)}");
相比较来讲,这个实现更细致,而且充分利用了C#的基本语法,并没有额外在创建一个函数返回产品,相对更简洁更"高大上",哈哈....
最新文章
- 关于 devbridge-autocomplete 插件多选操作的实现方法
- 解决“chrome提示adobe flash player 已经过期”的小问题
- jquery的each()详细介绍
- VBA 操作数字
- PHP面向对象常见的关键字和魔术方法
- linux系统的目录结构
- Linux用户名显示-bash-4.1$快速排查
- 安装成功的nginx如何添加未编译安装模块
- android 61 logcat
- 玩javaweb的web.xml编译路径
- swift3 UIColor扩展
- chkconfig命令(管理开机自启)
- 30分钟快速学习Shell脚本编程
- 怎么让普通用户使用root权限执行用户命令
- spring copy中的一个很气人的问题(初学者渣渣的一些感受)
- selenium+java破解极验滑动验证码的示例代码
- Codeforces 923 C. Perfect Security
- 快讯:微软安卓版个人助理(Cortana)在美国境内进行公測
- 如何减少block的嵌套层次?
- cogs 1962. [HAOI2015]树上染色
热门文章
- SpringSecurity框架下实现CSRF跨站攻击防御
- Calling the Web Service dynamically (.NET 动态访问Web Service)
- Vue + TypeScript 踩坑总结
- Python小数据保存,有多少中分类?不妨看看他们的类比与推荐方案...
- 转:Spring Boot启动过程
- 一道题反映Java的类初始化过程
- git 使用详解(5)—— get log 查看提交历史
- MYSQL“错误代码#1045 Access denied for user 'root'@'********8' (using password:YES)”
- CoderForces985F-Isomorphic Strings
- WebAPI接口的自动化测试2