다음을 통해 공유


Builder pattern in C#

Builder Pattern states: Separate the construction of a complex object from its representation so that the same construction process can create different representations.Below example shows the way of using a builder pattern.

Abstract Builder

using System.Collections.Generic;
  
namespace Builders
{
    internal abstract  class ProcessBuilder
    {
        protected List<ManageProcess> Data { get; private  set; }
  
        internal List<ManageProcess> Construct(List<string> selectedItems)
        {
            Data = InitProcessCollection(selectedItems);
            var service = new  ListDataService();
            var docs = service.GetAllManageProcessDocs();
            var iterations = service.GetAllIterations();
  
            ApplyVisitors(docs, iterations);
            return Data;
        }
  
        internal abstract  List<ManageProcess> InitProcessCollection(List<string> selectedItems);
        internal abstract  void ApplyVisitors(IList<ManageProcessDoc> documents, IList<Iteration> iterations);
    }
}

Concrete Builder 1

using System.Collections.Generic;
using System.Linq;
  
  
namespace Builders
{
    internal class  ManageProcessReleaseBuilder : ProcessBuilder
    {
        internal static  ProcessBuilder Instance
        {
            get
            {
                return new  ManageProcessReleaseBuilder();
            }
        }
  
        internal override  List<ManageProcess> InitProcessCollection(List<string> items)
        {
            var manageProcessList = new  List<ManageProcess>();
  
            ProductService.Instance.GetProductsByReleasesList(items).ToList()
                  .ForEach(p =>
                  {
                      manageProcessList.AddRange(CollectionMapper<ManageProcess, Product, ManageProcessMapper>
                               .From(new List<Product>() { p })
                               .ToList());
                     
                  });
            return manageProcessList;
        }
  
        internal override  void ApplyVisitors(IList<ManageProcessDoc> docs,IList<Iteration> iterations )
        {
            VisitorFactory<ManageProcess, ManageProcessDocumentVisitorAttribute>.Current.All(docs)
                .ToList()
                .ForEach(Visit);
        }
  
        public void  Visit(IVisitor<ManageProcess> visitor)
        {
            Data.ForEach(p => p.Accept(visitor));
        }
    }
}

Concrete Builder 2

using System.Collections.Generic;
using System.Linq;
  
  
namespace Builders
{
    internal class  ManageProcessBuilder : ProcessBuilder
    {
        internal static  ProcessBuilder Instance
        {
            get
            {
                return new  ManageProcessBuilder();
            }
       }
  
        internal override  List<ManageProcess> InitProcessCollection(List<string> selectedItems)
        {
            var manageProcessList = new  List<ManageProcess>();
            foreach (var selectedProduct in ProductService.Instance.GetProducts(selectedItems)
                    .Where(p => p.Releases.Any()))
                {
                    manageProcessList.AddRange(CollectionMapper<ManageProcess, Product, ManageProcessMapper>
                        .From(new List<Product>() { selectedProduct })
                        .ToList());
                }
            return manageProcessList;
        }
      
        internal override  void ApplyVisitors(IList<ManageProcessDoc> docs,IList<Iteration> iterations )
        {
            VisitorFactory<ManageProcess, ManageProcessDocumentVisitorAttribute>.Current.All(docs)
                .ToList()
                .ForEach(Visit);
        }
  
        public void  Visit(IVisitor<ManageProcess> visitor)
        {
            Data.ForEach(p => p.Accept(visitor));
        }
    }
}

Builder call

ManageProcessBuilder.Instance.Construct(selectedItems);
ManageProcessReleaseBuilder.Instance.Construct(selectedItems);