Last active
December 20, 2015 11:00
-
-
Save ErikZhou/6120306 to your computer and use it in GitHub Desktop.
Structural Patterns - Bridge C# sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Bridge pattern -- Structural example | |
| using System; | |
| // "Abstraction" | |
| class Abstraction | |
| { | |
| // Fields | |
| protected Implementor implementor; | |
| // Properties | |
| public Implementor Implementor | |
| { | |
| set{ implementor = value; } | |
| } | |
| // Methods | |
| virtual public void Operation() | |
| { | |
| implementor.Operation(); | |
| } | |
| } | |
| // "Implementor" | |
| abstract class Implementor | |
| { | |
| // Methods | |
| abstract public void Operation(); | |
| } | |
| // "RefinedAbstraction" | |
| class RefinedAbstraction : Abstraction | |
| { | |
| // Methods | |
| override public void Operation() | |
| { | |
| implementor.Operation(); | |
| } | |
| } | |
| // "ConcreteImplementorA" | |
| class ConcreteImplementorA : Implementor | |
| { | |
| // Methods | |
| override public void Operation() | |
| { | |
| Console.WriteLine("ConcreteImplementorA Operation"); | |
| } | |
| } | |
| // "ConcreteImplementorB" | |
| class ConcreteImplementorB : Implementor | |
| { | |
| // Methods | |
| override public void Operation() | |
| { | |
| Console.WriteLine("ConcreteImplementorB Operation"); | |
| } | |
| } | |
| /// <summary> | |
| /// Client test | |
| /// </summary> | |
| public class Client | |
| { | |
| public static void Main(string[] args) | |
| { | |
| Abstraction abstraction = new RefinedAbstraction(); | |
| // Set implementation and call | |
| abstraction.Implementor = new ConcreteImplementorA(); | |
| abstraction.Operation(); | |
| // Change implemention and call | |
| abstraction.Implementor = new ConcreteImplementorB(); | |
| abstraction.Operation(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment