Project – Improved message interpreter – Behavioral Patterns
Conclusion The Chain of Responsibility pattern is another great GoF pattern. It divides a large problem into smaller, cohesive units, each doing one job: handling its specific request(s).Now, let’s see how the Chain of Responsibility pattern can help us follow the SOLID principles: Next, let’s use the Template Method and Chain of Responsibility patterns to […]
Design – Behavioral Patterns-2
Each handler does two things: Let’s use Program.cs as the consumer of the Chain of Responsibility (the Client) and use a POST requests to interface with our REST API and build the message.Here is the first part of our REST API: var builder = WebApplication.CreateBuilder(args);builder.Services.AddSingleton<IMessageHandler>( new AlarmTriggeredHandler( new AlarmPausedHandler( new AlarmStoppedHandler()))); In the preceding code, […]
Design – Behavioral Patterns-1
The most basic Chain of Responsibility starts by defining an interface that handles a request (IHandler). Then we add classes that handle one or more scenarios (Handler1 and Handler2): Figure 12.2: Class diagram representing the Chain of Responsibility pattern A difference between the Chain of Responsibility pattern and many other patterns is that no central […]
Conclusion – Behavioral Patterns
The Template Method is a powerful and easy-to-implement design pattern allowing subclasses to reuse the algorithm’s skeleton while implementing (abstract) or overriding (virtual) subparts. It allows implementation-specific classes to extend the core algorithm. It can reduce the duplication of logic and improve maintainability while not cutting out any flexibility in the process. There are many […]
Project – Building a search machine – Behavioral Patterns-2
Now that we have defined the actors and explored the code, let’s see what is happening in our consumer (the Client): The Find method returns null when it does not find a value and, by extension, the IndexOf method.By running the program, we get the following output: =============================================Current search machine is LinearSearchMachineThe element ‘1’ was […]
Project – Building a search machine – Behavioral Patterns-1
Let’s start with a simple, classic example to demonstrate how the Template Method pattern works.Context: Depending on the collection, we want to use a different search algorithm. We want to use a binary search for sorted collections, but we want to use a linear search for unsorted collections.Let’s start with the consumer, a REST endpoint […]
Implementing the Template Method pattern – Behavioral Patterns
Before you begin: Join our book community on Discord Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the “architecting-aspnet-core-apps-3e” channel under EARLY ACCESS SUBSCRIPTION). https://packt.link/EarlyAccess This chapter explores two new design patterns from the well-known Gang of Four (GoF). They are behavioral patterns, meaning […]
Conclusion – Structural Patterns
The Façade pattern is handy for simplifying consumers’ lives, allowing us to hide subsystems’ implementation details behind a wall. There are multiple flavors to it; the two most prominent ones are: Now, let’s see how the transparent façade pattern can help us follow the SOLID principles: Finally, let’s see how the opaque façade pattern can […]
Flexibility in action – Structural Patterns
As discussed, the transparent façade adds more flexibility. Here, we explore this flexibility in action.Context: We want to change the behavior of the TransparentFacade class. At the moment, the result of the transparent/b endpoint looks like this: Component B, Operation CComponent B, Operation DComponent C, Operation F To demonstrate we can extend and change the […]
The program – Structural Patterns
Now, let’s analyze the consumer, an ASP.NET Core application that forwards HTTP requests to the façades and return the result as their response.The first step is to register the dependencies like this: var builder = WebApplication.CreateBuilder(args);builder.Services .AddOpaqueFacadeSubSystem() .AddTransparentFacadeSubSystem(); With these extension methods, the application root is so clean that it is hard to know that […]