Mid GoF Patterns

Dynamic Features:?

  • Add functionality to dynamically change the properties of the decorated

object. For instance, you could add a feature to customize the size of the

coffee (Small, Medium, Large) which would change both the cost and

description.

Visual Diagram:

+-------------------------+

| ICoffee | <-- Component Interface

+-------------------------+

/ \

+-------------------------+

| SimpleCoffee | <-- Concrete Component (Core Coffee)

+-------------------------+

+---------------------+ +----------------------+

| CoffeeDecorator |<--- | MilkDecorator |

+---------------------+ +----------------------+

| |

Follow:

+---------------------+ +----------------------+

| SugarDecorator | <-- | WhippedCreamDecorator |

+---------------------+ +----------------------+

  • SimpleCoffee is the base coffee object.
  • MilkDecorator and SugarDecorator are decorators that extend the behavior of

SimpleCoffee.

Conclusion:

The Decorator Pattern provides a powerful and flexible way to extend the functionality of

objects at runtime. In real-time applications like customizing a coffee order, decorating UI

elements, or adding functionality to text, this pattern helps in achieving clean, modular, and

extensible code. You can add or remove features dynamically, ensuring that your base

classes remain unaltered and your system remains flexible for future extensions.

Facade Pattern: Real-Time Example - Simplifying a Home Theater

System

Definition:

The Facade Pattern provides a simplified interface to a complex subsystem, making it

easier for clients to interact with multiple components. It hides the complexity of the

subsystem and exposes only what is necessary, offering a higher-level interface to users.

Use Case:

A common example is a home theater system, where the user needs to interact with

multiple components like an amplifier, DVD player, or projector. The Facade Pattern

simplifies the process by providing a unified interface to these various components, making

the system easier to use.

Code Explanation:

More from Design Patterns in C#

All questions for this course