« Decorator pattern » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:CSharp Category:Design Pattern = Description = * It provides a way of attaching new state and behaviour to an object dynamically. * The object does not kn… »)
 
 
(5 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
[[Category:CSharp]]
[[Category:CSharp]]
[[Category:Design Pattern]]
[[Category:Design Patterns]]
= Description =
= Description =
* It provides a way of attaching new state and behaviour to an object dynamically.
Attach additional responsibilities to an object dynamically.<br>Decorators provide a flexible alternative to subclassing for extending functionality
* The object does not know it is being “decorated”.
* Add new functionalities dynamically to existing objects
* Decorators both inherit the original class and contain an instantiation of it.
* 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 =
* It is an alternative to subclass to add new functionalities
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 =
= Exemple =
Ligne 39 : Ligne 28 :
</filebox>
</filebox>


<filebox fn='UserTitle'>
<filebox fn='UserTitle.cs'>
public class UserTitle : IUser
public class UserTitle : IUser
{
{

Dernière version du 3 avril 2022 à 20:36

Description

Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending functionality

  • Add new functionalities dynamically to existing objects
  • The object does not know it is being « decorated »
  • Decorators both inherit the original class and contain an instantiation of it
  • It is an alternative to subclass to add new functionalities

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.cs
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