Lädt...


🔧 C# Design Pattern: Strategy


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The Strategy pattern allows you to define a family of algorithms, encapsulate them in separate classes, and make them interchangeable. The object using the algorithm can select which strategy to use at runtime. It’s useful when you have multiple ways to perform an operation and want to switch between them flexibly, such as different ways to sort a list or calculate a price.

C# Code Example:

// Strategy interface
public interface IPriceCalculation
{
    decimal CalculatePrice(decimal basePrice);
}

// Implementation of a strategy for normal price
public class NormalPrice : IPriceCalculation
{
    public decimal CalculatePrice(decimal basePrice)
    {
        return basePrice;
    }
}

// Implementation of a strategy for discounted price
public class DiscountPrice : IPriceCalculation
{
    private decimal _discountPercentage;

    public DiscountPrice(decimal discountPercentage)
    {
        _discountPercentage = discountPercentage;
    }

    public decimal CalculatePrice(decimal basePrice)
    {
        return basePrice - (basePrice * _discountPercentage / 100);
    }
}

// Implementation of a strategy for premium price
public class PremiumPrice : IPriceCalculation
{
    public decimal CalculatePrice(decimal basePrice)
    {
        return basePrice + (basePrice * 0.20m); // Adds 20% to the price
    }
}

// Class that uses a pricing strategy
public class Product
{
    private IPriceCalculation _priceStrategy;
    private decimal _basePrice;

    public Product(IPriceCalculation priceStrategy, decimal basePrice)
    {
        _priceStrategy = priceStrategy;
        _basePrice = basePrice;
    }

    public void SetStrategy(IPriceCalculation priceStrategy)
    {
        _priceStrategy = priceStrategy;
    }

    public void DisplayFinalPrice()
    {
        Console.WriteLine($"Final price: {_priceStrategy.CalculatePrice(_basePrice)}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Create a product with a base price of 100
        Product product = new Product(new NormalPrice(), 100);

        // Display price with normal strategy
        product.DisplayFinalPrice(); // Output: Final price: 100

        // Change to discount strategy and display the price
        product.SetStrategy(new DiscountPrice(10)); // 10% discount
        product.DisplayFinalPrice(); // Output: Final price: 90

        // Change to premium strategy and display the price
        product.SetStrategy(new PremiumPrice());
        product.DisplayFinalPrice(); // Output: Final price: 120
    }
}

Code Explanation:
In this example, we have three different strategies for calculating the price of a product: NormalPrice, DiscountPrice, and PremiumPrice. The Product class uses one of these strategies to calculate the final price. The SetStrategy method allows switching the price calculation strategy at runtime. In the main code, the product’s price is calculated using the three different strategies.

Conclusion:
The Strategy pattern is useful when you want an object to have multiple ways of performing an operation and want to make these options interchangeable. It helps to separate the logic of different algorithms, allowing for greater flexibility and code reuse.

Source code: GitHub

...

🔧 Design Pattern #5 - Adapter Pattern


📈 26.99 Punkte
🔧 Programmierung

🔧 Module-Pattern | Javascript Design Pattern Simplified | Part 4


📈 26.99 Punkte
🔧 Programmierung

🔧 Decorator-Pattern | Javascript Design Pattern Simplified | Part 5


📈 26.99 Punkte
🔧 Programmierung

🔧 Factory-Pattern | Javascript Design Pattern Simplified | Part 2


📈 26.99 Punkte
🔧 Programmierung

🔧 Observer-Pattern | Javascript Design Pattern Simplified | Part 3


📈 26.99 Punkte
🔧 Programmierung

🔧 Singleton-Pattern | Javascript Design Pattern Simplified | Part 1


📈 26.99 Punkte
🔧 Programmierung

🔧 Design Pattern #4 - Publisher/Subscriber Pattern


📈 26.99 Punkte
🔧 Programmierung

🔧 Design Pattern #3 - Observer Pattern


📈 26.99 Punkte
🔧 Programmierung

🔧 Design Pattern #2 - Facade Pattern


📈 26.99 Punkte
🔧 Programmierung

🐧 Observer Pattern: Was steckt hinter dem Observer Design Pattern?


📈 26.99 Punkte
🐧 Server

🔧 Implementing Chain of Responsibility Pattern in C# : Middleware's Design Pattern


📈 26.99 Punkte
🔧 Programmierung

🔧 Design Pattern #7 - Builder Pattern


📈 26.99 Punkte
🔧 Programmierung

🔧 Design Pattern #6 - Composite Pattern


📈 26.99 Punkte
🔧 Programmierung

🔧 Implementing a Database Helper in C# Using the Strategy Design Pattern Step-by-Step Guide


📈 25.45 Punkte
🔧 Programmierung

🔧 Strategy Design Pattern in Spring


📈 25.45 Punkte
🔧 Programmierung

📰 (g+) Strategy Design Pattern: Schema X für C++


📈 25.45 Punkte
📰 IT Nachrichten

🔧 C# Design Pattern: Strategy (Portugues)


📈 25.45 Punkte
🔧 Programmierung

🔧 C# Design Pattern: Strategy


📈 25.45 Punkte
🔧 Programmierung

🔧 Strategy Design Pattern in Java


📈 25.45 Punkte
🔧 Programmierung

🔧 Strategy design pattern


📈 25.45 Punkte
🔧 Programmierung

🔧 The Strategy Design Pattern in C# Applications


📈 25.45 Punkte
🔧 Programmierung

🔧 Understanding the Strategy Design Pattern in Java


📈 25.45 Punkte
🔧 Programmierung

🔧 ⚛️ Applying Design Patterns in React: Strategy Pattern (Part 2)


📈 25.45 Punkte
🔧 Programmierung

🔧 Design Pattern: Strategy (TS)


📈 25.45 Punkte
🔧 Programmierung

🔧 Become a Python Design Strategist using the Strategy Pattern


📈 25.45 Punkte
🔧 Programmierung

🔧 Applying Design Patterns in React: Strategy Pattern


📈 25.45 Punkte
🔧 Programmierung

🔧 Strategy Design Pattern


📈 25.45 Punkte
🔧 Programmierung

🔧 Mastering the Strategy Design Pattern: A Guide for Developers


📈 25.45 Punkte
🔧 Programmierung

🔧 Webkul pattern question advance pattern for interview with python


📈 21.75 Punkte
🔧 Programmierung

🔧 Service: O pattern que virou anti-pattern


📈 21.75 Punkte
🔧 Programmierung

🔧 Pub/Sub pattern vs Observer Pattern: what's the difference?


📈 21.75 Punkte
🔧 Programmierung

🔧 Go program pattern 01: Functional Options Pattern


📈 21.75 Punkte
🔧 Programmierung

🔧 A transição do Higher-Order Component pattern para o React Hooks pattern


📈 21.75 Punkte
🔧 Programmierung

📰 Neu in .NET 7 [5]: List Pattern und Slice Pattern mit C# 11


📈 21.75 Punkte
📰 IT Nachrichten

matomo