« Bridge pattern » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:CSharp Category:Design Patterns = Description = * Decouple an abstraction from its implementation so that the two can vary independently »)
 
 
(5 versions intermédiaires par le même utilisateur non affichées)
Ligne 2 : Ligne 2 :
[[Category:Design Patterns]]
[[Category:Design Patterns]]
= Description =
= Description =
* Decouple an abstraction from its implementation so that the two can vary independently
Decouple an abstraction from its implementation so that the two can vary independently.
* Select or switch implementations at runtime
* Replace the implementation by another one without having to modify the referencing class by using an interface
* It can also be thought of as two layers of abstraction
 
= Exemple =
* The client can change the {{boxx|*Logger}} classes by another without having to modify the {{boxx|UserService}}.
 
<filebox fn='ILogger.cs'>
public interface ILogger
{
    void WriteLine(string text);
}
</filebox>
 
<filebox fn='ConsoleLogger'>
public class ConsoleLogger : ILogger
{
    public void WriteLine(string text)
    {
        Console.WriteLine(text);
    }
}
</filebox>
 
<filebox fn='IUserService.cs'>
public interface IUserService
{
    void ChangePassword(int userId, string newPassword);
}
</filebox>
 
<filebox fn='UserService.cs'>
public class UserService : IUserService
{
    private ILogger logger;
 
    public UserService(ILogger logger)
    {
        this.logger = logger;
    }
 
    public void ChangePassword(int userId, string newPassword)
    {
        logger.WriteLine($"Password has been change for user {userId}.");
    }
}
</filebox>
 
<kode lang='cs'>
IUserService userService = new UserService(new ConsoleLogger());
userService.ChangePassword(1, "0000");
</kode>

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

Description

Decouple an abstraction from its implementation so that the two can vary independently.

  • Select or switch implementations at runtime
  • Replace the implementation by another one without having to modify the referencing class by using an interface
  • It can also be thought of as two layers of abstraction

Exemple

  • The client can change the *Logger classes by another without having to modify the UserService.
ILogger.cs
public interface ILogger
{
    void WriteLine(string text);
}
ConsoleLogger
public class ConsoleLogger : ILogger
{
    public void WriteLine(string text)
    {
        Console.WriteLine(text);
    }
}
IUserService.cs
public interface IUserService
{
    void ChangePassword(int userId, string newPassword);
}
UserService.cs
public class UserService : IUserService
{
    private ILogger logger;

    public UserService(ILogger logger)
    {
        this.logger = logger;
    }

    public void ChangePassword(int userId, string newPassword)
    {
        logger.WriteLine($"Password has been change for user {userId}.");
    }
}
Cs.svg
IUserService userService = new UserService(new ConsoleLogger());
userService.ChangePassword(1, "0000");