[Architecture Pattern] Factory Builder

Source: Internet
Author: User

[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).

    • Object map

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: ...).

    • Object map

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
    • Object map

Participant system-has a factory
    • Use factory to generate product.

    • Use product to provide system functionality.

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
    • Encapsulation system functionality.

    • Build and inject into the system through builder.

Cooperation method
    • Sequence diagram

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.