Skip to main content

Posts

Showing posts with the label structural pattern

Understanding the Adapter Design Pattern in C#

  The Adapter design pattern is a structural pattern used to allow incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces, enabling them to communicate and interact. The pattern is particularly useful when integrating legacy systems or third-party libraries with a new system that has different interfaces. Understanding the Bridge Design Pattern in C# Example Without the Adapter Pattern Let's consider a scenario where we have an existing class LegacyPrinter that prints documents and a new interface IPrinter that the new system expects. Without the Adapter pattern, the LegacyPrinter cannot be used directly because it doesn't implement the IPrinter interface. using System; namespace WithoutAdapterPattern { // Legacy class that needs to be adapted class LegacyPrinter { public void PrintDocument ( string document ) { Console.WriteLine( "Printing document using legacy printer: ...

Understanding the Bridge Design Pattern in C#

  The Bridge design pattern is a structural pattern that separates an object's abstraction from its implementation so that the two can vary independently. This pattern decouples the abstraction from the implementation, allowing the implementation to change without affecting the client, and vice versa. The Bridge pattern is useful when both the class and its behavior might need to be extended using inheritance. Understanding the Composite Design Pattern in C# Example Without the Bridge Pattern Consider a scenario where we have different types of devices, like TVs and Radios, and different remote controls, such as BasicRemote and AdvancedRemote. Without the Bridge pattern, we might end up with a separate class for each combination of device and remote control. using System; namespace WithoutBridgePattern { // Basic remote control for TV class BasicRemoteTV { public void Power () => Console.WriteLine( "TV Power button pressed" ); publ...

Understanding the Composite Design Pattern in C#

  The Composite design pattern is a structural pattern used to treat individual objects and compositions of objects uniformly. It allows you to compose objects into tree structures to represent part-whole hierarchies. This pattern is particularly useful when dealing with complex tree structures and operations that you want to perform uniformly across both individual and composite objects. Understanding the Decorator Design Pattern in C# Example Without the Composite Pattern Let's consider a file system where we have files and directories. Each directory can contain multiple files and subdirectories. Without the Composite pattern, handling such a structure can become complex and error-prone. using System; using System.Collections.Generic; namespace WithoutCompositePattern { // File class class File { public string Name { get ; } public File ( string name ) { Name = name; } public void Display () ...

Understanding the Decorator Design Pattern in C#

  The Decorator design pattern is a structural pattern that allows behavior to be added to individual objects, either statically or dynamically, without affecting the behavior of other objects from the same class. It provides a flexible alternative to subclassing for extending functionality. The Decorator pattern involves a set of decorator classes that are used to wrap concrete components. Understanding the Facade Design Pattern in C# Example Without the Decorator Pattern Let's consider a simple example of a coffee shop where we have different types of beverages, such as Espresso and Latte, and different add-ons like milk and sugar. In a non-pattern approach, we might create subclasses for each combination of beverages and add-ons. using System; namespace WithoutDecoratorPattern { // Base class abstract class Beverage { public abstract string GetDescription () ; public abstract double GetCost () ; } // Concrete classes class ...

Understanding the Facade Design Pattern in C#

  The Facade design pattern is a structural pattern that provides a simplified interface to a complex subsystem or a set of interfaces. It hides the complexities of the system and provides an easy-to-use interface for the client. The Facade pattern is particularly useful when working with large systems with numerous interdependent components, as it helps to decouple the system from the clients that use it. Understanding the Flyweight Method Design Pattern in C# Example Without the Facade Pattern Let's consider a simple scenario of a home theater system that involves multiple components: Amplifier , DVDPlayer , Projector , and Lights . To watch a movie, each of these components needs to be configured and controlled separately. using System; namespace WithoutFacadePattern { // Subsystems class Amplifier { public void On () => Console.WriteLine( "Amplifier on" ); public void SetVolume ( int level ) => Console.WriteLine( $"...

Understanding the Flyweight Method Design Pattern in C#

  The Flyweight Method design pattern is a structural pattern that helps reduce memory usage by sharing as much data as possible with other similar objects. It is particularly useful in scenarios where a large number of objects with similar properties are needed. The Flyweight Method  pattern achieves this by storing common data externally and referencing it, thereby minimizing memory consumption. Understanding the Proxy Method Design Pattern in C# Example Without the Flyweight Method Pattern Let's consider a simple graphics application where we need to display a large number of circles with varying colors and coordinates. In a non-pattern approach, we might create a separate object for each circle, even if many circles share the same color. using System; using System.Collections.Generic; namespace WithoutFlyweightMethodPattern { // Circle class with color, x and y coordinates class Circle { public string Color { get ; private set ; } ...