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 […]
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 […]
Transparent façade – Structural Patterns
The transparent façade is the most flexible type of façade and is exceptionally suitable for a system that leverages dependency injection. The implementation is similar to the opaque façade, but the public visibility modifier changes how consumers can access the class library elements. For this system, it was worth adding interfaces to allow the consumers […]
Project – The façades – Structural Patterns
In this example, we play with the following C# projects: Let’s start with the class libraries. To follow the SOLID principles, adding some interfaces representing the elements of the subsystem seemed appropriate. In subsequent chapters, we explore how to organize our abstractions to be more reusable, but for now, both abstractions and implementations are in […]
Conclusion – Structural Patterns
The Composite pattern effectively builds, manages, and maintains complex non-linear data structures. Its power is primarily in its self-management capabilities. Each node, component, or composite is responsible for its own logic, leaving little to no work for the composite’s consumers. Of course, a more complex scenario would have led to a more complex interface.Using the […]