This is the 13th day of my participation in the August More Text Challenge. For details, see:August is more challenging

The generator

Generator pattern: Encapsulates the construction process of a product and allows step-by-step construction.

Now there is a designated vacation plan, which needs to specify activities, hotels, meals, tickets and other things for each day of the vacation, but each guest’s vacation plan may be different. Such as days, types of activities, meals and so on.

 

 

 

We need an elastic data structure that represents the guest’s plan, and different variations, as well as a series of potentially complex sequences to create such a plan. How to provide a way to create a complex structure without getting mixed up with the steps to create it. The process of wrapping an iterator into a separate object hides the internal representation of the collection from the client. We take the same approach here: we encapsulate the process of creating a tour plan into an object called a generator, and then let the client call the generator to create a tour plan for it.

Design class diagram:

 

 

Implementation code:

① Storage data structure class

1 public class Vacation 2 { 3 public int Day { get; set; } 4 public string Hotel { get; set; } 5 public string Park { get; set; } 6 public string Activity { get; set; } 7 public string Meal { get; set; 8}}Copy the code

② Abstract generator

1 public abstract class AbstractBuilder 2 { 3 public abstract void BuildDay(int day); 4 public abstract void Hotel(string hotel); 5 public abstract void Park(string park); 6 public abstract void Activity(string activity); 7 public abstract void Meal(string meal); 8 public abstract Vacation GetVacationPlanner(); 9}Copy the code

③ Specific generator, specific generator can be multiple implementations.

1 public class VacationBuilder : AbstractBuilder 2 { 3 private Vacation vacation=new Vacation(); 4 5 public override void BuildDay(int day) 6 { 7 vacation.Day = day; 8 } 9 10 public override void Hotel(string hotel) 11 { 12 vacation.Hotel = hotel; 13 } 14 public override void Activity(string activity) 15 { 16 vacation.Activity = activity; 17 } 18 public override void Meal(string meal) 19 { 20 vacation.Meal = meal; 21 } 22 23 public override void Park(string park) 24 { 25 vacation.Park = park; 26 } 27 28 public override Vacation GetVacationPlanner() 29 { 30 return vacation; 31} 32}Copy the code

④ The customer uses the generator

Advantages:

1. Encapsulate complex object creation.

2. Allows objects to be created in multiple steps, and the process can be altered.

3. Hide product internal performance from customers.

4. The implementation of the product can be replaced because the customer only sees an abstract interface.

Uses and disadvantages:

1. Often used to create composite structures.

2. Compared to the factory pattern, customers who use generators to create objects need more domain knowledge to create objects correctly.

Chain of responsibility

Chain of responsibility pattern: The chain of responsibility pattern is used when more than one object has the opportunity to process a request.

There is such a scenario, the company’s specialized mail processing personnel need to receive daily mail to process, one is to be transferred to the department manager for processing, one is for their own processing, and one kind of junk mail directly delete. In such a scenario, we can create a chain of objects for processing through the chain of responsibility pattern. Each object examines the mail request in order and either processes it or passes it to the next object in the chain.

Design class diagram:

Implementation code:

① Chain of responsibility abstract class

Public enum EmailType 5 {6 Self=1, 7 Manager=2, 6 Self=1, 7 Manager=2, 6 Self=1, 7 Manager=2, 8 Del=3 9 } 10 11 12 public abstract class Handler 13 { 14 public Handler nextHandler; 15 public EmailType type; 16 17 public Handler(EmailType type) { 18 this.type = type; 19 } 20 21 public void SetNextHandler(Handler nextHandler) 22 { 23 this.nextHandler = nextHandler; 24 } 25 26 public abstract void HandleRequest(EmailType requsetType); 27 to 28}Copy the code

② Responsibility chain processing class

3 /// </summary> 4 class SelfHandler: Handler 5 {6 public SelfHandler() : base(EmailType.Self) 7 { 8 } 9 10 public override void HandleRequest(EmailType requsetType) 11 { 12 if (EmailType.Self == requsetType) 13 {14 console. WriteLine(" mail is handled by yourself "); 15 } 16 else 17 { 18 if (nextHandler ! = null) 19 { 20 nextHandler.HandleRequest(requsetType); 21} 22} 23} 24}Copy the code
1 /// <summary> 3 /// </summary> 4 class ManagerHandler: Handler 5 {6 public ManagerHandler() : base(EmailType.Manager) 7 { 8 } 9 10 public override void HandleRequest(EmailType requsetType) 11 { 12 if (emailType. Manager == requsetType) 13 {14 console. WriteLine(" Mail to Manager processing "); 15 } 16 else 17 { 18 if (nextHandler ! = null) 19 { 20 nextHandler.HandleRequest(requsetType); 21} 22} 23} 24}Copy the code
1 /// </summary> 3 /// </summary> 4 class DelHandler: Handler 5 {6 public DelHandler() : base(EmailType.Del) 7 { 8 } 9 10 public override void HandleRequest(EmailType requsetType) 11 { 12 if (EmailType.Del == RequsetType) 13 {14 console. WriteLine(" spam deleted "); 15 } 16 else 17 { 18 if (nextHandler ! = null) 19 { 20 nextHandler.HandleRequest(requsetType); 21} 22} 23} 24}Copy the code

Test the chain of responsibility

1 class Program 2 {3 static void Main(string[] args) 4 {5 Handler Handler = new SelfHandler(); 7 Handler handler1 = new ManagerHandler(); 8 Handler handler2 = new DelHandler(); 9 handler.SetNextHandler(handler1); 10 handler1.SetNextHandler(handler2); 11 // Test 12 Handler. HandleRequest(emailType.del); 13 handler.HandleRequest(EmailType.Manager); 14 handler.HandleRequest(EmailType.Self); 15 16 Console.ReadKey(); 18 17}}Copy the code

 

Advantages:

Decouple the sender and receiver of the request.

2. Simplify objects because you don’t need to know the structure of the chain.

3. Allows you to dynamically add or remove responsibilities by changing members of the chain or reordering them.

Uses and disadvantages:

Often used in windowing systems to handle events such as mouse and keyboard.

2. There is no guarantee that the request will be executed, and it may fall outside the end of the chain if no object handles it.

3, it is not easy to observe the allowed characteristics, and it is not easy to troubleshoot the problem.