[Architecture Pattern] Factory Builder
[Architecture Pattern] Factory Builder
目的
同时提供延迟注入对象、挂载注入项目这两个功能
情景
在开发系统时,如果需要在运行时间才生成并注入对象,可以套用Factory模式来提供延迟注入对象功能。例如:一个监控系统在火警发生时,建立功能对象来启动相关设备(洒水设备、警报设备、警消通报)。
物件图
但在实作过程中,延迟注入对象这个功能,常常需要伴随挂载注入项目功能,用以增加系统的延展性,这时可以接着套用Builder模式,来提供挂载注入项目功能。例如:一个监控系统在火警发生时,依照购买版本建立功能对象来启动相关设备(V1:洒水设备、警报设备;V2:洒水设备、警报设备、警消通报;V3:....)。
物件图
本篇文章介绍上述这个Factory模式、Builder模式组合而成的Factory Builder模式。这个模式定义对象之间的职责跟互动,用来为系统同时提供延迟注入对象、挂载注入项目这两个功能,用以增加系统的延展性。主要为自己做个纪录,也希望能帮助到有需要的开发人员。(使用DI Framework也能提供相关功能)
示意图
结构
物件图
参与者
System
拥有一个Factory
使用Factory来生成Product。
使用Product来提供系统功能。
Factory
拥有多个Builder
使用Builder来生成Product。
在条件生成Product情景中,Factory封装条件逻辑。
Builder
生成Product,生成参数由Builder提供。
在条件生成Product情景中,Builder封装条件参数。
Productm
封装系统功能。
透过Builder生成并注入到系统。
合作方式
顺序图
实作
类别图
ActionFactory
public class ActionFactory
{
// Fields
private readonly IEnumerable<ActionBuilder> _actionBuilderCollection = null; // Constructors
public ActionFactory(IEnumerable<ActionBuilder> actionBuilderCollection)
{
// Default
_actionBuilderCollection = actionBuilderCollection;
} // Methods
public IEnumerable<IAction> Create()
{
// Result
List<IAction> actionList = new List<IAction>(); // Create
foreach (var actionBuilder in _actionBuilderCollection)
{
var action = actionBuilder.Create();
if (action == null) throw new InvalidOperationException();
actionList.Add(action);
} // Return
return actionList;
}
}
ActionBuilder
public abstract class ActionBuilder
{
// Methods
public abstract IAction Create();
}
Action
public interface IAction
{
// Methods
void Execute();
}
SecuritySystem
public class SecuritySystem
{
// Fields
private readonly ActionFactory _actionFactory = null; // Constructors
public SecuritySystem(ActionFactory actionFactory)
{
// Default
_actionFactory = actionFactory;
} // Methods
public void Execute()
{
// Create
var actionCollection = _actionFactory.Create();
if (actionCollection == null) throw new InvalidOperationException(); // Execute
foreach(var action in actionCollection)
{
action.Execute();
}
}
}
系统挂载:洒水设备、警报设备
class Program
{
static void Main(string[] args)
{
// Initialize
var actionBuilderList = new List<ActionBuilder>();
actionBuilderList.Add(new WateringActionBuilder());
actionBuilderList.Add(new AlarmActionBuilder());
var securitySystem = new SecuritySystem(new ActionFactory(actionBuilderList)); // Execute
securitySystem.Execute(); // End
Console.ReadLine();
}
}
系统挂载:洒水设备、警报设备、警消通报
class Program
{
static void Main(string[] args)
{
// Initialize
var actionBuilderList = new List<ActionBuilder>();
actionBuilderList.Add(new WateringActionBuilder());
actionBuilderList.Add(new AlarmActionBuilder());
actionBuilderList.Add(new NotifyActionBuilder());
var securitySystem = new SecuritySystem(new ActionFactory(actionBuilderList)); // Execute
securitySystem.Execute(); // End
Console.ReadLine();
}
}
下载
范例程序代码:点此下载
[Architecture Pattern] Factory Builder的更多相关文章
- [Architecture Pattern] Singleton Locator
[Architecture Pattern] Singleton Locator 目的 组件自己提供Service Locator模式,用来降低组件的耦合度. 情景 在开发系统时,底层的Infrast ...
- [Architecture Pattern] Repository实作查询功能
[Architecture Pattern] Repository实作查询功能 范例下载 范例程序代码:点此下载 问题情景 在系统的BLL与DAL之间,加入Repository Pattern的设计, ...
- [Design Pattern] Factory Pattern 简单案例
Factory Pattern , 即工厂模式,用于创建对象的场景,属于创建类的设计模式 . 下面是一个工厂模式案例. Shape 作为接口, Circle, Square, Rectangle 作为 ...
- Software Architecture Pattern(Mark Richards)笔记
软件架构模式 缺少规范架构的程序通常会变得紧耦合.脆弱.难以更改,缺少清晰的发展方向和愿景.这本小书用50多页介绍了常用的5种常见架构模式,相信不管是大牛还是萌新都会有所收获,特别是对我这种偏爱系统设 ...
- Architecture pattern & Architecture style
Architecture pattern: context + problem -> solution Architecture style: solution part of architec ...
- design pattern factory method #Reprinted#
引入人.工厂.和斧子的问题: (1),原始社会时,劳动社会基本没有分工,需要斧子的人(调用者)只好自己去磨一把斧子,每个人拥有自己的斧子,如果把大家的石斧改为铁斧,需要每个人都要学会磨铁斧的本领,工作 ...
- Java Design Pattern(Factory,Singleton,Prototype,Proxy)
一.Factory 设计模式: the most common pattern,create a new object ,eg. A a=new A();工厂模式的好处:工厂模式可以做到把创建对象单独 ...
- Design Pattern ->Factory Method
Layering & Contract Philosophy With additional indirection Factory Method The example code is as ...
- Architecture Pattern: Publish-subscribe Pattern
1. Brief 一直对Observer Pattern和Pub/Sub Pattern有所混淆,下面打算通过这两篇Blog来梳理这两种模式.若有纰漏请大家指正. 2. Role Publisher: ...
随机推荐
- bootstrap fileinput 文件上传工具
这是我上传的第二个plugin 首先第一点就是因为这个好看 符合bootstrap的界面风格 第二是可以拖拽(虽然我不常用这个功能 但是这样界面看起来就丰满了很多) 最后不得不吐槽这个的回发事件 我百 ...
- python-redis 入门
redis官网http://redis.io Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis是一 ...
- [转]C# dataGridview 报“索引-1没有值”的解决办法
很多WINFORM的开发人员在DataGridView的开发当中,都会出现“索引-1没有值”这个烦人的问题,其实较早之前,我已经大概知道问题的所在,也找到了解决方法,不过一直没有时间去深入研究一下,今 ...
- 字符集与Mysql字符集处理(一)
一.字符集总结 其实大多数的知识在这篇文章里已经讲得非常清楚了.这里只是讲一下自己的感悟. 1. UTF-8虽然是以UTF(unicode transfermation format)开头的,但是 ...
- 【翻译】C# Tips & Tricks: Weak References - When and How to Use Them
原文:C# Tips & Tricks: Weak References - When and How to Use Them Sometimes you have an object whi ...
- IIS限制ip访问
1.禁止IP访问 http://jingyan.baidu.com/article/22fe7ced0462633002617f39.html 2.限制IP访问频率 http://q.cnblogs. ...
- C#读写Json
C#处理json文件主要有两种方式: (1)使用JavaScriptSerializer类,需要引入System.Web.Extension库,并添加下面两个引用: using System.Web; ...
- java 去掉html标签
使用正则表达式删除HTML标签. import java.util.regex.Matcher; import java.util.regex.Pattern; public class HTMLSp ...
- Container Stack
- ASP.NET MVC数组模型绑定
在ASP.NET MVC中使用Razor语法可以在视图中方便地展示数组,如果要进行数组模型绑定,会遇到索引断裂问题,如下示例: <input type="text" name ...