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-3
In the preceding Create method, we create the corporation, add two stores, then return the result.The CreateTaleTowersStore and CreateEpicNexusStore methods create a store, set their name, address, and manager, and create three sections each: private IComponent CreateTaleTowersStore(){ var store = new Store( “Tale Towers”, “125 Enchantment Street, Storyville, SV 72845”, “Malcolm Reynolds” ); store.Add(CreateFantasySection()); store.Add(CreateAdventureSection()); […]
Project – BookStore – Structural Patterns-2
The BookComposite class implements the following shared features: Using the LINQ Sum() extension method in the children.Sum(child => child.Count()); expression allowed us to replace a more complex for loop and an accumulator variable. Adding the virtual modifier to the Type property allows sub-types to override the property in case their type’s name does not reflect […]
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 […]
Conclusion – Structural Patterns
The Decorator pattern is one of our toolbox’s simplest yet most powerful design patterns. It augments existing classes without modifying them. A decorator is an independent block of logic that we can use to create complex and granular object trees that fit our needs.We also explored the Scrutor open-source library to assist us in registering […]
DecoratorB – Structural Patterns-2
In the preceding code, we registered ComponentA as the implementation of IComponent, with a singleton lifetime, just like the first time.Then, by using Scrutor, we told the IoC container to override that first binding and to decorate the already registered IComponent (ComponentA) with an instance of DecoratorA instead. Then, we overrode the second binding by […]