« Proxy pattern » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
 
(Une version intermédiaire par le même utilisateur non affichée)
Ligne 2 : Ligne 2 :
[[Category:Design Patterns]]
[[Category:Design Patterns]]
= Description =
= Description =
* proxy object controls the creation and access to the subject object
Provide a surrogate or placeholder for another object to control access to it.
* proxy and subject objects share the same interface
* Control access to an object
* Proxy and subject objects share the same interface


== Proxy vs decorator pattern ==
== Proxy vs decorator pattern ==
* Decorator informs and empowers its client, it adds functionalities to an object.<br>
* Decorator informs and empowers its client, it adds functionalities to an object.<br>The client knows those functionalities.
The client knows those functionalities.
* Proxy restricts and disempowers its client, it controls access to an object.<br>The client doesn't know those control access functionalities.
* Proxy restricts and disempowers its client, it controls access to an object.<br>
The client doesn't know those control access functionalities.


= Exemple =
= Exemple =

Dernière version du 7 juin 2020 à 15:28

Description

Provide a surrogate or placeholder for another object to control access to it.

  • Control access to an object
  • Proxy and subject objects share the same interface

Proxy vs decorator pattern

  • Decorator informs and empowers its client, it adds functionalities to an object.
    The client knows those functionalities.
  • Proxy restricts and disempowers its client, it controls access to an object.
    The client doesn't know those control access functionalities.

Exemple

public class User
{
    public string Name { get; set; }
}
IUserClient.cs
public interface IUserClient
{
    IReadOnlyList<User> GetUsers();
}
UserClient.cs
public class UserClient : IUserClient
{
    public IReadOnlyList<User> GetUsers()
    {
        return new[]
        {
            new User { Name = "Nicolas" },
            new User { Name = "Paul" }
        };
    }
}
ProxyUserClient
public class ProxyUserClient : IUserClient
{
    private UserClient userClient;

    public bool Authenticate(string userName, string password)
    {
        if (userName == "Nicolas" && password == "0000")
        {
            userClient = new UserClient();
            return true;
        }

        return false;
    }

    public IReadOnlyList<User> GetUsers() => userClient?.GetUsers();
}
Cs.svg
IUserClient userClient = new UserClient();
// call GetUsers from UserClient: no restrictions
var users = userClient.GetUsers();
Console.WriteLine(string.Join('\n', users.Select(x => $"{x.Name}")));

userClient = new ProxyUserClient();

// authentication
((ProxyUserClient)userClient).Authenticate("Nicolas", "0000");

// call GetUsers from ProxyUserClient: authentication restriction
users = userClient.GetUsers();
if (users == null)
    Console.WriteLine("You need to authenticate first.");
else
    Console.WriteLine(string.Join('\n', users.Select(x => $"{x.Name}")));