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 […]
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 […]
Implementing the Façade design pattern – Structural Patterns
The Façade pattern is a structural pattern that simplifies the access to a complex system. It is very similar to the Adapter pattern, but it creates a wall (a façade) between one or more subsystems. The big difference between the adapter and the façade is that instead of adapting an interface to another, the façade […]
Project – Greeter – Structural Patterns
Context: We’ve programmed a highly sophisticated greeting system that we want to reuse in a new program. However, its interface does not match the new design, and we cannot modify it because other systems use that greeting system.To fix this problem, we decided to apply the Adapter pattern. Here is the code of the external […]
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 […]
Project – BookStore – Structural Patterns-1
Context: We built a program in the past to support a bookstore. However, the store is going so well that our little program is not enough anymore. Our fictional company now owns multiple stores. They want to divide those stores into sections and manage book sets and single books. After a few minutes of gathering […]