The Bridge design pattern decouples an abstraction from its implementation so that the two can vary independently.
簡單來說~ 就是要將抽象跟實作分離~ 這樣他們就可以獨立變化~
換句話說~ 橋梁能將抽象概念跟實作方法給鬆散耦合~ 這樣就教不會互相影響
學習目標: 橋梁模式的概念及實務
學習難度: ☆☆☆
using System;
namespace ConsoleApp1
{
//概念定義
public class Programmer
{
public string name { get; set; }
protected Working working;
public Working Working
{
set { working = value; }
}
public virtual void Unity()
{
working.Unity();
}
public virtual void MongoDB()
{
working.MongoDB();
}
}
//精煉概念
public class GameProgrammer : Programmer
{
public override void Unity()
{
working.Unity();
}
}
//精煉概念
public class DataBaseProgrammer : Programmer
{
public override void MongoDB()
{
working.MongoDB();
}
}
//實作抽象類
public abstract class Working
{
public virtual void Unity()
{
}
public virtual void MongoDB()
{
}
}
//實作類1
public class CreateGame : Working
{
public override void Unity()
{
Console.WriteLine("Making Game");
}
}
//實作類2
public class ControDB : Working
{
public override void MongoDB()
{
Console.WriteLine("Control DataBase");
}
}
public class MainProgram
{
public static void Main(string[] args)
{
//Game工程師
Programmer gameprogrammer = new GameProgrammer();
gameprogrammer.Working = new CreateGame();
gameprogrammer.Unity();
//DB工程師
Programmer DBprogrammer = new DataBaseProgrammer();
DBprogrammer.Working = new ControDB();
DBprogrammer.MongoDB();
}
}
}
參考資料:
https://www.dofactory.com/net/bridge-design-pattern