« Proxy pattern » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:CSharp Category:Design Patterns = Description = * proxy object controls the creation of and access to subject object * proxy and subject objects share the… »)
 
Ligne 69 : Ligne 69 :


<kode lang='cs'>
<kode lang='cs'>
var userClient = new UserClient();
IUserClient userClient = new UserClient();
// call GetUsers from UserClient: no restrictions
// call GetUsers from UserClient: no restrictions
var users = userClient.GetUsers();
var users = userClient.GetUsers();
Console.WriteLine(string.Join('\n', users.Select(x => $"{x.Name}")));
Console.WriteLine(string.Join('\n', users.Select(x => $"{x.Name}")));


var proxyUserClient = new ProxyUserClient();
userClient = new ProxyUserClient();
// call GetUsers from ProxyUserClient: authentication restriction
// call GetUsers from ProxyUserClient: authentication restriction
users = proxyUserClient.GetUsers();
users = userClient.GetUsers();
if (users == null)
if (users == null)
     Console.WriteLine("You to authenticate first.");
     Console.WriteLine("You to authenticate first.");
Ligne 83 : Ligne 83 :


// authentication
// authentication
proxyUserClient.Authenticate("Nicolas", "0000");
((ProxyUserClient)userClient).Authenticate("Nicolas", "0000");
users = proxyUserClient.GetUsers();
users = userClient.GetUsers();
if (users == null)
if (users == null)
     Console.WriteLine("You to authenticate first.");
     Console.WriteLine("You to authenticate first.");

Version du 6 juin 2020 à 17:57

Description

  • proxy object controls the creation of and access to subject object
  • proxy and subject objects share the same interface

Proxy vs decorator pattern

  • Decorator informs and empowers its client, it adds known functionalities to an object
  • Proxy restricts and disempowers its client, it controls access to an object with unknown functionalities like authentication

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

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