[Architecture Pattern] Factory Builder Purpose
The two functions of the deferred injection object and the Mount injection project are also provided.
Scene
When developing a system, if you need to generate and inject objects at run time, you can apply the Factory mode to provide deferred injection object functionality. For example: A monitoring system in the event of a fire, the establishment of functional objects to start the relevant equipment (sprinkler equipment, alarm equipment, warning bulletin).
However, in the implementation process, the delay injection of the object this function, often need to accompany the load injection project function, to increase the ductility of the system, you can then apply the builder mode to provide Mount injection project functionality. For example: A monitoring system in the event of a fire, according to the purchase version of the establishment of functional objects to start the relevant equipment (V1: sprinkler equipment, alarm equipment; V2: sprinkler equipment, alarm equipment, warning bulletin; V3: ...).
This article describes the Factory builder pattern, which is a combination of the factory model and the builder pattern. This pattern defines the functions and interactions between objects, which are used to provide the system with the two functions of deferred injection, loading and injecting projects, to increase the ductility of the system. The main record for themselves, but also hope to help the needs of developers. (with the DI Framework also provides relevant features )
Structure
Participant system-has a factory
Factory
Have multiple builder
Use Builder to generate product.
In the conditional generation product scenario, factory encapsulates the conditional logic.
Builder
Build product, which is provided by builder.
In the conditional generation product scenario, the builder encapsulates the condition parameters.
Productm
Cooperation method
Practice
Category diagram
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(); } }}
-
System mount: Sprinkler, alarm device
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 (); }}
System mount: Sprinkler equipment, alarm equipment, police bulletin
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(); }}
Download
Sample program code: Click here to download
[Architecture Pattern] Factory Builder