Skip to content

Instantly share code, notes, and snippets.

@ErikZhou
Last active December 20, 2015 11:00
Show Gist options
  • Select an option

  • Save ErikZhou/6120306 to your computer and use it in GitHub Desktop.

Select an option

Save ErikZhou/6120306 to your computer and use it in GitHub Desktop.
Structural Patterns - Bridge C# sample
// 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