« Design Patterns » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
[[Category:CSharp]]
[[Category:CSharp]]
[[Category:Informatique Générale]]
[[Category:Informatique Générale]]
= Decorator =
= Structural patterns =
== Description ==
* [[Decorator]]
* It provides a way of attaching new state and behaviour to an object dynamically.
* Proxy
* The object does not know it is being “decorated”.
* Bridge
* Decorators both inherit the original class and contain an instantiation of it.
* Composite
* Flyweight
* Adapter
* Facade


== When to use it ==
= Creational patterns =
When you have:
* Prototype
* An existing component class that may be unavailable for subclassing.
* Factory method
You want to:
* Singleton
* 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 ==
= Behavioral patterns =
<filebox fn='IUser.cs'>
* Strategy
public interface IUser
* State
{
* Template method
    string Name { get; set; }
* Chain of responsablity
}
* Command
</filebox>
* Iterator
 
* Mediator
<filebox fn='User.cs'>
* Observer
public class User : IUser
* Visitor
{
* Interpreter
    public string Name { get; set; }
* Memento
 
    public User(string name)
    {
        this.Name = name;
    }
}
</filebox>
 
<filebox fn='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;
    }
}
</filebox>
 
<filebox fn='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;
    }
}
</filebox>
 
<kode lang='cs'>
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
</kode>


= [http://www.dofactory.com/Patterns/PatternFactory.aspx Factory Method] =
= [http://www.dofactory.com/Patterns/PatternFactory.aspx Factory Method] =

Version du 6 juin 2020 à 16:15

Structural patterns

  • Decorator
  • Proxy
  • Bridge
  • Composite
  • Flyweight
  • Adapter
  • Facade

Creational patterns

  • Prototype
  • Factory method
  • Singleton

Behavioral patterns

  • Strategy
  • State
  • Template method
  • Chain of responsablity
  • Command
  • Iterator
  • Mediator
  • Observer
  • Visitor
  • Interpreter
  • Memento

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