The design of a bridge and its players is shown in the UML diagram
From this diagram, we can see that the role players for the Bridge pattern are:
Abstraction : The interface that the client sees
Operation : A method that is called by the client
Bridge : An interface defining those parts of the Abstraction that might vary
ImplementationA and ImplementationB :Implementations of the Bridge interface
OperationImp : A method in the Bridge that is called from the Operation in the Abstraction
And this the implementation :
using System;
namespace BridgePattern {
class Abstraction
{
Bridge bridge;
public Abstraction(Bridge implementation) {
bridge =implementation;
}
public string Operation ( )
{
return "Abstraction" +" <<< BRIDGE >>>>"+bridge.OperationIm( );
}
}
interface Bridge
{
string OperationImp( );
}
class ImplementationA :Bridge {
public string OperationImp ( )
{
return"ImplementationA";
}
}
class ImplementationB :Bridge {
public string OperationImp ( )
{
return "ImplementationB";
}
}
class BridgeApp
{
static void Main()
{
Console.WriteLine("Bridge Pattern\n");
Console.WriteLine(new Abstraction(new
ImplementationA()).Operation());
Console.WriteLine(new Abstraction(new
ImplementationB()).Operation());
}
}
}
/* Output
Bridge Pattern
Abstraction <<< BRIDGE >>>> ImplementationA
Abstraction <<< BRIDGE >>>> ImplementationB
*/
Best Regards
No comments:
Post a Comment