物件轉接器 模式
物件轉接器 模式
物件轉接器 模式
物件轉接器 模式
使用時機想使用一個已經存在的類別,但如果它的介面,
也就是它的方法和你的要求不同時,就應該考慮用轉接器
模式。
客戶程式碼可以統一調用同一個介面就行,可以更簡單、
直接、緊湊。
雙方都不太容易修改時,再使用轉接器模式轉接。
class Target
{
public virtual void Request()
{
Console.WriteLine(“普通請求!”);
}
}
class Adaptee
{
public void SpecificRequest()
{
Console.WriteLine(“特殊請求!”);
}
}
class Adaptee : Target
{
private Adaptee adaptee = new Adaptee();
public override void Request()
{
adaptee.SpecificRequest();
}
}
static void Main(string[] args)
{
Target target = new Adaptee();
target.Request();
Console.Read();
}