Posts

Showing posts from December, 2023

Bridge Pattern | Structural Design Pattern

  Bridge Pattern The Bridge Pattern is a structural design pattern that  separates abstraction from implementation . It allows both to evolve independently without the need to modify each other. This pattern is particularly useful when you want to avoid a permanent binding between an abstraction and its implementation. Here are the key components of the Bridge Pattern: Abstraction: Defines the abstraction's interface and maintains a reference to an object of type Implementor. Refined Abstraction: Extends the abstraction and adds finer details. Implementor: Defines the interface for implementation classes. It doesn't have to correspond directly to the abstraction's interface. Concrete Implementor: Implements the Implementor interface and defines its concrete implementation. This pattern is beneficial when there are multiple dimensions of variability, and you want to avoid a Cartesian product of variations. It promotes flexibility and extensibility by allowing the abstraction...

Adaptor Pattern | Structural Design Pattern

There are two main types of adapters: Class Adapter: Uses inheritance to adapt the interface of the Adaptee to the Target interface. Object Adapter: Uses composition to contain an instance of the Adaptee and implements the Target interface by delegating calls to the Adaptee. Example in C#: ______ using System; // Target interface public interface ITarget { void Request(); } // Adaptee class with an incompatible interface public class Adaptee { public void SpecificRequest() { Console.WriteLine("SpecificRequest called in Adaptee"); } } // Class Adapter (using inheritance) public class ClassAdapter : Adaptee, ITarget { public void Request() { // Adapting the interface to meet the client's expectations SpecificRequest(); } } // Object Adapter (using composition) public class ObjectAdapter : ITarget { private readonly Adaptee _adaptee; public ObjectAdapter(Adaptee adaptee) { _adaptee = adaptee; } ...

Composite Pattern | Structural Design Pattern

Composite Pattern   The Composite Pattern is a structural design pattern that allows you to compose objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.  In other words, you can use the same operations on both individual objects and composite objects. a "partial-whole hierarchy" refers to a structure where individual objects (leaves) and composed objects (composites) share a common interface, allowing them to be treated uniformly. "uniformly" means that both individual objects (leaves) and composite objects share a common interface. This common interface allows clients to treat them the same way, without needing to distinguish between them. Key components of the Composite Pattern include : Component : Declares the interface for objects in the composition. It can be an abstract class or interface with operations that are common to both leaf and composite elements. Leaf : Repr...

Decorator Pattern | Structural Design Pattern

Decorator Pattern The Decorator Pattern is a structural pattern that allows behavior to be added to an individual object , either statically or dynamically, without affecting the behavior of other objects from the same class. _____________ using System; // Component interface public interface ICoffee { string GetDescription(); double GetCost(); } // ConcreteComponent class public class SimpleCoffee : ICoffee { public string GetDescription() { return "Simple coffee"; } public double GetCost() { return 1.0; } } // Decorator abstract class public abstract class CoffeeDecorator : ICoffee { private readonly ICoffee _decoratedCoffee; protected CoffeeDecorator(ICoffee decoratedCoffee) { _decoratedCoffee = decoratedCoffee; } public virtual string GetDescription() { return _decoratedCoffee.GetDescription(); } public virtual double GetCost() { return _decoratedCoffee.GetCost(); ...

Logarithm | log

logarithm (log)  In mathematics, a logarithm (log) is the inverse operation to exponentiation. It answers the question "To what power must the base be raised to obtain a certain number?" In other words, if ( b^x = y ), then ( log b (y)= x). Real-world applications of logarithms are prevalent in various fields, including: 1. Finance: Logarithms are used to calculate compound interest, helping in financial modeling and investment analysis. 2. Computer Science: Logarithmic time complexity is desirable in algorithms. Data structures like binary trees and binary search benefit from logarithmic time for efficient search operations. 3. Acoustics: Logarithmic scales, such as the decibel scale, are used to measure sound intensity. This scale reflects human perception of sound, where equal intervals represent equal increases in perceived loudness. 4. Physics: Logarithmic scales are used to describe phenomena with wide-ranging values, such as the Richter scale for measuring earthquakes ...