Design Patterns

De Banane Atomic
Aller à la navigationAller à la recherche

Decorator

Description

  • It provides a way of attaching new state and behaviour to an object dynamically.
  • The object does not know it is being “decorated”.
  • Decorators both inherit the original class and contain an instantiation of it.

When to use it

When you have:

  • An existing component class that may be unavailable for subclassing.

You want to:

  • Attach additional state or behavior to an object dynamically.
  • Make changes to some objects in a class without affecting others.
  • Avoid subclassing because too many classes could result.

But consider using instead:

  • The Adapter pattern, which sets up an interface between different classes.
  • The Composite pattern, which aggregates an object without also inheriting its interface.
  • The Proxy pattern, which specifically controls access to objects.
  • The Strategy pattern, which changes the original object rather than wrapping it.

Exemple

IUser.cs
public interface IUser
{
    string Name { get; set; }
}
User.cs
public class User : IUser
{
    public string Name { get; set; }

    public User(string name)
    {
        this.Name = name;
    }
}
UserTitle
public class UserTitle : IUser
{
    private IUser user;

    public string Name
    {
        get => $"{user.Name} ({Title})";
        set => user.Name = value;
    }

    public string Title { get; set; }

    public UserTitle(IUser user, string title)
    {
        this.user = user;
        this.Title = title;
    }
}
UserEmail.cs
public class UserEmail : IUser
{
    private IUser user;

    public string Name
    {
        get => $"{user.Name} - {Email}";
        set => user.Name = value;
    }

    public string Email { get; set; }

    public UserEmail(IUser user, string email)
    {
        this.user = user;
        this.Email = email;
    }
}
Cs.svg
var user = new User("Nicolas");
Console.WriteLine(user.Name);  // Nicolas

var userWithTitle = new UserTitle(user, "Big Boss");
Console.WriteLine(userWithTitle.Name);  // Nicolas (Big Boss)

var userWithEmail = new UserEmail(user, "nicolas@world-company.net");
Console.WriteLine(userWithEmail.Name);  // Nicolas (Big Boss) - nicolas@world-company.net

Factory Method

Permet d'instancier des objets dont les classes (Lion, Wolf) ne sont directement connues par l'appelant (AnimalsFactory).
Mais elles héritent toutes d'une classe abstraite (Animal) qui elle est connue de l'appelant.
FactoryMethod.png

Singleton

Cs.svg
public class Singleton
{
    private Singleton() { }
    private static readonly Singleton _instance = new Singleton();
    public static Singleton Instance
    {
        get { return _instance; }
    }
}

Double-checked locking

Cs.svg
public class Singleton
{
    private static readonly object Lock = new object();
    private static readonly Singleton _instance;

    private Singleton() { }

    public static Singleton Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = new Singleton();
                    }
                }
            }
        }
        // permet d'implémenter un set, là où Lazy ne le permet pas
    }
}

// In .NET Framework 4.0, the Lazy<T> class was introduced, which internally uses double-checked locking by default
public class Singleton
{
    private static readonly Lazy<Singleton> _instance = new Lazy<Singleton>(() => new Singleton());

    private Singleton() { }

    public static Singleton Instance
    {
        get
        {
            return _instance.Value;
        }
    }
}

Abstract Factory

Fournit une interface (ContinentFactory) pour créer des familles d'objets (Carnivore, Herbivore) sans avoir besoin de connaitre l'implémentation de ces classes (Lion, Wildebeest, Wolf, Bison).
AbstractFactory.png

Builder

Factorise le code de plusieurs