在結構型模式中包含七種模式:適配器模式、裝飾模式、橋接模式、組合模式、享元模式、代理模式、外觀模式。
6. 適配器模式
將一個類的接口轉換為客戶希望的一個接口。使得原本由於接口不兼容而不能一起工作的那些類可以一起工作。相當於一個翻譯的作用(就像姚明開始不會英語在國外打球,需要一個翻譯作為適配器)
UML圖:
代碼
view plaincopy to clipboardprint?//17.4找個籃球翻譯當適配器——中間調節作用
namespace 適配器
{
class Class1
{
static void Main(string[] args)
{
Player b = new ForWards("巴蒂爾");
b.Attack();
Player m = new Guards("麥蒂");
m.Attack();
//Player ym = new Center("姚明");
Player ym = new Translator("姚明"); //外籍中鋒
ym.Attack();
ym.Defense();
Console.Read();
}
}
abstract class Player //球員
{
protected string name;
public Player(string name)
{
this.name = name;
}
public abstract void Attack();
public abstract void Defense();
}
class ForWards : Player //前鋒
{
public ForWards(string name)
: base(name)
{ }
public override void Attack()
{
Console.WriteLine("前鋒{0}進攻",name );
}
public override void Defense()
{
Console.WriteLine("前鋒{0}防守",name );
}
}
class Center : Player //中鋒
{
public Center(string name)
: base(name)
{ }
public override void Attack()
{
Console.WriteLine("中鋒{0}進攻",name );
}
public override void Defense()
{
Console.WriteLine("中鋒¤0}防守",name );
}
}
class Guards : Player //後¨®衛¨¤
{
public Guards(string name)
: base(name)
{ }
public override void Attack()
{
Console.WriteLine("後衛{0}防守",name );
}
public override void Defense()
{
Console.WriteLine("後衛{0}進攻",name );
}
}
//外籍中鋒
class ForeignCenter //相當於姚明的位置,他打的是中國的球
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public void 進攻()
{
Console.WriteLine("外籍中鋒{0}進攻",name );
}
public void 防守()
{
Console.WriteLine("外籍中鋒{0}防守",name );
}
}
class Translator : Player //翻譯者——相當於姚明的翻譯為他去打球
{
private ForeignCenter wjzf = new ForeignCenter(); //外籍中鋒
public Translator(string name)
: base(name)
{
wjzf.Name = name;
}
public override void Attack()
{
wjzf.防守();
}
public override void Defense()
{
wjzf.進攻();
}
}
}
//17.4找個籃球翻譯當適配器——中間調節作用
namespace 適配器
{
class Class1
{
static void Main(string[] args)
{
Player b = new ForWards("巴蒂爾");
b.Attack();
Player m = new Guards("麥蒂");
m.Attack();
//Player ym = new Center("姚明");
Player ym = new Translator("姚明"); //外籍中鋒
ym.Attack();
ym.Defense();
Console.Read();
}
}
abstract class Player //球員
{
protected string name;
public Player(string name)
{
this.name = name;
}
public abstract void Attack();
public abstract void Defense();
}
class ForWards : Player //前鋒
{
public ForWards(string name)
: base(name)
{ }
public override void Attack()
{
Console.WriteLine("前鋒{0}進攻",name );
}
public override void Defense()
{
Console.WriteLine("前鋒{0}防守",name );
}
}
class Center : Player //中鋒
{
public Center(string name)
: base(name)
{ }
public override void Attack()
{
Console.WriteLine("中鋒{0}進攻",name );
}
public override void Defense()
{
Console.WriteLine("中鋒¤0}防守",name );
}
}
class Guards : Player //後¨®衛¨¤
{
public Guards(string name)
: base(name)
{ }
public override void Attack()
{
Console.WriteLine("後衛{0}防守",name );
}
public override void Defense()
{
Console.WriteLine("後衛{0}進攻",name );
}
}
//外籍中鋒
class ForeignCenter //相當於姚明的位置,他打的是中國的球
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public void 進攻()
{
Console.WriteLine("外籍中鋒{0}進攻",name );
}
public void 防守()
{
Console.WriteLine("外籍中鋒{0}防守",name );
}
}
class Translator : Player //翻譯者——相當於姚明的翻譯為他去打球
{
private ForeignCenter wjzf = new ForeignCenter(); //外籍中鋒
public Translator(string name)
: base(name)
{
wjzf.Name = name;
}
public override void Attack()
{
wjzf.防守();
}
public override void Defense()
{
wjzf.進攻();
}
}
}
7. 裝飾模式
動態的給一個對象添加一些額外的職責,就增加功能來說,裝飾模式比生成子類更靈活。裝飾最後形成的是一個對象鏈,下一個裝飾上一個,下一個被下下一個裝飾(就像一杯白開水,加入白糖裝飾之後還可以用加入咖啡裝飾,之後還可以在加入茶葉裝飾,之後還可以加入……形成一個鏈)
UML圖
代碼:
view plaincopy to clipboardprint?namespace 裝飾模式
{
class Class3
{
static void Main(string[] args)
{
Person2 xc = new Person2("小菜");
Console.WriteLine("\n第一種裝扮");
Sneakers2 pqx = new Sneakers2();
BigTrouser2 kk = new BigTrouser2();
TShirts2 dtx = new TShirts2();
pqx.Decorate(xc); //一條龍式的裝扮,裝扮有先後順序
kk.Decorate(pqx );
dtx.Decorate(kk);
dtx.Show();
Console.WriteLine("\n第二種裝扮");
LeatherShoes2 px = new LeatherShoes2();
Tie2 ld = new Tie2();
Suit2 xz = new Suit2();
px.Decorate(xc);
ld.Decorate(px);
xz.Decorate(ld);
xz.Show();
Console.WriteLine("\n第三種裝扮");
Sneakers2 pqx2 = new Sneakers2();
LeatherShoes2 px2 = new LeatherShoes2();
BigTrouser2 kk2 = new BigTrouser2();
Tie2 ld2 = new Tie2();
pqx2.Decorate(xc);
px2.Decorate(pqx);
kk2.Decorate(px2);
ld2.Decorate(kk2);
ld2.Show();
Console.Read();
}
}
class Person2
{
public Person2()
{ }
private string name;
public Person2(string name)
{
this.name = name;
}
public virtual void Show()
{
Console.WriteLine("裝扮的{0}",name );
}
}
class Finery2 : Person2 //裝飾抽象類
{
protected Person2 component;
public void Decorate(Person2 component)
{
this.component = component;
}
public override void Show()
{
if (component != null)
{
component.Show();
}
}
}
//具體裝飾類
class TShirts2 : Finery2
{
public override void Show()
{
Console.Write("大體恤");
base.Show();
}
}
class Sneakers2 : Finery2
{
public override void Show()
{
Console.Write("大褲衩");
base.Show();
}
}
class BigTrouser2 : Finery2
{
public override void Show()
{
Console.Write("垮褲");
base.Show();
}
}
class LeatherShoes2 : Finery2
{
public override void Show()
{
Console.Write("西裝");
base.Show();
}
}
class Tie2 : Finery2
{
public override void Show()
{
Console.Write("領帶");
base.Show();
}
}
class Suit2 : Finery2
{
public override void Show()
{
Console.Write("皮鞋");
base.Show();
}
}
}
namespace 裝飾模式
{
class Class3
{
static void Main(string[] args)
{
Person2 xc = new Person2("小菜");
Console.WriteLine("\n第一種裝扮");
Sneakers2 pqx = new Sneakers2();
BigTrouser2 kk = new BigTrouser2();
TShirts2 dtx = new TShirts2();
pqx.Decorate(xc); //一條龍式的裝扮,裝扮有先後順序
kk.Decorate(pqx );
dtx.Decorate(kk);
dtx.Show();
Console.WriteLine("\n第二種裝扮");
LeatherShoes2 px = new LeatherShoes2();
Tie2 ld = new Tie2();
Suit2 xz = new Suit2();
px.Decorate(xc);
ld.Decorate(px);
xz.Decorate(ld);
xz.Show();
Console.WriteLine("\n第三種裝扮");
Sneakers2 pqx2 = new Sneakers2();
LeatherShoes2 px2 = new LeatherShoes2();
BigTrouser2 kk2 = new BigTrouser2();
Tie2 ld2 = new Tie2();
pqx2.Decorate(xc);
px2.Decorate(pqx);
kk2.Decorate(px2);
ld2.Decorate(kk2);
ld2.Show();
Console.Read();
}
}
class Person2
{
public Person2()
{ }
private string name;
public Person2(string name)
{
this.name = name;
}
public virtual void Show()
{
Console.WriteLine("裝扮的{0}",name );
}
}
class Finery2 : Person2 //裝飾抽象類
{
protected Person2 component;
public void Decorate(Person2 component)
{
this.component = component;
}
public override void Show()
{
if (component != null)
{
component.Show();
}
}
}
//具體裝飾類
class TShirts2 : Finery2
{
public override void Show()
{
Console.Write("大體恤");
base.Show();
}
}
class Sneakers2 : Finery2
{
public override void Show()
{
Console.Write("大褲衩");
base.Show();
}
}
class BigTrouser2 : Finery2
{
public override void Show()
{
Console.Write("垮褲");
base.Show();
}
}
class LeatherShoes2 : Finery2
{
public override void Show()
{
Console.Write("西裝");
base.Show();
}
}
class Tie2 : Finery2
{
public override void Show()
{
Console.Write("領帶");
base.Show();
}
}
class Suit2 : Finery2
{
public override void Show()
{
Console.Write("皮鞋");
base.Show();
}
}
}
8. 橋接模式
將抽象部分與它的實現部分分離,使它們都可以獨立地變換。核心意圖就是將這些實現獨立出來,讓他們各自變化,使得每種變化不會影響其他的變化,從而達到應對變化的目的(就adidas和NIKE兩個品牌與服裝之間橋接起來)
UML圖:
代碼:
view plaincopy to clipboardprint?//橋接模式——將抽象部分(手機品牌)和實現部分(手機軟件)分離開使他們可以獨立的變化
namespace 橋接模式
{
class Class2
{
static void Main(string[] args)
{
HandsetBrand1 ab;
ab = new HandsetBrandN1();
ab.SetHandsetSoft(new HandsetGame1());
ab.Run();
ab.SetHandsetSoft(new HandsetAddressList());
ab.Run();
ab = new HandsetBrandM1();
ab.SetHandsetSoft(new HandsetGame1());
ab.Run();
ab.SetHandsetSoft(new HandsetAddressList());
ab.Run();
Console.Read();
}
}
abstract class HandsetSoft //手機軟件
{
public abstract void Run();
}
class HandsetGame1 : HandsetSoft //手機遊戲
{
public override void Run()
{
Console.WriteLine("運行手機遊戲");
}
}
class HandsetAddressList : HandsetSoft //手機通訊錄
{
public override void Run()
{
Console.WriteLine("運行手機通訊錄");
}
}
abstract class HandsetBrand1 //手機品牌
{
protected HandsetSoft soft;
//設置手機軟件
public void SetHandsetSoft(HandsetSoft soft)
{
this.soft = soft;
}
//運行
public abstract void Run();
}
class HandsetBrandN1 : HandsetBrand1 //手機品牌N
{
public override void Run()
{
soft.Run();
}
}
class HandsetBrandM1 : HandsetBrand1 //手機品牌M
{
public override void Run()
{
soft.Run();
}
}
//新增一個手機MP3播放器軟件
class HandsetMP3 : HandsetSoft
{
public override void Run()
{
Console.WriteLine("運行手機MP3播放器");
}
}
//新增手機品牌S
class HandsetBrandS : HandsetBrand1
{
public override void Run()
{
soft.Run();
}
}
}
//橋接模式——將抽象部分(手機品牌)和實現部分(手機軟件)分離開使他們可以獨立的變化
namespace 橋接模式
{
class Class2
{
static void Main(string[] args)
{
HandsetBrand1 ab;
ab = new HandsetBrandN1();
ab.SetHandsetSoft(new HandsetGame1());
ab.Run();
ab.SetHandsetSoft(new HandsetAddressList());
ab.Run();
ab = new HandsetBrandM1();
ab.SetHandsetSoft(new HandsetGame1());
ab.Run();
ab.SetHandsetSoft(new HandsetAddressList());
ab.Run();
Console.Read();
}
}
abstract class HandsetSoft //手機軟件
{
public abstract void Run();
}
class HandsetGame1 : HandsetSoft //手機遊戲
{
public override void Run()
{
Console.WriteLine("運行手機遊戲");
}
}
class HandsetAddressList : HandsetSoft //手機通訊錄
{
public override void Run()
{
Console.WriteLine("運行手機通訊錄");
}
}
abstract class HandsetBrand1 //手機品牌
{
protected HandsetSoft soft;
//設置手機軟件
public void SetHandsetSoft(HandsetSoft soft)
{
this.soft = soft;
}
//運行
public abstract void Run();
}
class HandsetBrandN1 : HandsetBrand1 //手機品牌N
{
public override void Run()
{
soft.Run();
}
}
class HandsetBrandM1 : HandsetBrand1 //手機品牌M
{
public override void Run()
{
soft.Run();
}
}
//新增一個手機MP3播放器軟件
class HandsetMP3 : HandsetSoft
{
public override void Run()
{
Console.WriteLine("運行手機MP3播放器");
}
}
//新增手機品牌S
class HandsetBrandS : HandsetBrand1
{
public override void Run()
{
soft.Run();
}
}
}
9. 組合模式
將對象形成樹形結構以表示“部分—整體”的層次結構。組合模式使得用戶對單個對象和組合對象的使用具有一致性。在需求中體現部分和整體的結構時,以及你希望用戶可以忽略組合對象與單個對象的不同,統一地使用組合結構中的所有對象時,就應該考慮使用組合模式。,組合對象可以不斷的遞歸下去。組合模式讓用戶可以一致地使用組合結構和單個對象。(就像樹——樹枝、樹葉的關系,樹枝就是一個組合對象,樹葉是單個對象)
UML圖:
代碼:
view plaincopy to clipboardprint?namespace 組合模式
{
class Class1
{
static void Main(string[] args)
{
ConcreateCompany root = new ConcreateCompany("北京總公司");
root.Add(new HRDepartment("總公司人力資源部"));
root.Add(new FinanceDepartment("總公司財務部"));
ConcreateCompany comp = new ConcreateCompany("上海華東分公司");
comp.Add(new HRDepartment("華東分公司人力資源部"));
comp.Add(new FinanceDepartment("華東分公司財務部"));
root.Add(comp );
ConcreateCompany comp1 = new ConcreateCompany("南京辦事處");
comp1.Add(new HRDepartment("南京辦事處人力資源部"));
comp1.Add(new FinanceDepartment("南京辦事處財務部"));
comp.Add(comp1);
ConcreateCompany comp2 = new ConcreateCompany("杭州辦事處");
comp2.Add(new HRDepartment("杭州辦事處人力資源部"));
comp2.Add(new FinanceDepartment("杭州辦事處財務部"));
comp.Add(comp2);
Console.WriteLine("\n結構圖:");
root.Display(1);
Console.WriteLine("\n職責");
root.LineOfDuty();
Console.Read();
}
}
abstract class Company
{
protected string name;
public Company(string name)
{
this.name = name;
}
public abstract void Add(Company c); //增加
public abstract void Remove(Company c); //移除
public abstract void Display(int depth); //顯示
public abstract void LineOfDuty(); //履行職責
}
class ConcreateCompany : Company //具體公司類樹枝節點
{
private List<Company> children = new List<Company>();
public ConcreateCompany(string name)
: base(name)
{ }
public override void Add(Company c)
{
children.Add(c);
}
public override void Remove(Company c)
{
children.Remove(c);
}
public override void Display(int depth)
{
Console.WriteLine(new string('-',depth )+name );
foreach (Company component in children)
{
component.Display(depth+2);
}
}
public override void LineOfDuty()
{
foreach (Company component in children)
{
component.LineOfDuty();
}
}
}
class HRDepartment : Company //樹枝節點——人力資源部和財務部
{
public HRDepartment(string name)
: base(name)
{ }
public override void Add(Company c)
{ }
public override void Remove(Company c)
{
}
public override void Display(int depth)
{
Console.WriteLine(new string('-',depth )+name );
}
public override void LineOfDuty()
{
Console.WriteLine("{0}員工招聘培訓管理",name );
}
}
class FinanceDepartment : Company //財務部
{
public FinanceDepartment(string name)
: base(name)
{ }
public override void Add(Company c)
{
}
public override void Remove(Company c)
{
}
public override void Display(int depth)
{
Console.WriteLine(new string('-',depth )+name);
}
public override void LineOfDuty()
{
Console.WriteLine("{0}公司財務收支管理",name );
}
}
}
namespace 組合模式
{
class Class1
{
static void Main(string[] args)
{
ConcreateCompany root = new ConcreateCompany("北京總公司");
root.Add(new HRDepartment("總公司人力資源部"));
root.Add(new FinanceDepartment("總公司財務部"));
ConcreateCompany comp = new ConcreateCompany("上海華東分公司");
comp.Add(new HRDepartment("華東分公司人力資源部"));
comp.Add(new FinanceDepartment("華東分公司財務部"));
root.Add(comp );
ConcreateCompany comp1 = new ConcreateCompany("南京辦事處");
comp1.Add(new HRDepartment("南京辦事處人力資源部"));
comp1.Add(new FinanceDepartment("南京辦事處財務部"));
comp.Add(comp1);
ConcreateCompany comp2 = new ConcreateCompany("杭州辦事處");
comp2.Add(new HRDepartment("杭州辦事處人力資源部"));
comp2.Add(new FinanceDepartment("杭州辦事處財務部"));
comp.Add(comp2);
Console.WriteLine("\n結構圖:");
root.Display(1);
Console.WriteLine("\n職責");
root.LineOfDuty();
Console.Read();
}
}
abstract class Company
{
protected string name;
public Company(string name)
{
this.name = name;
}
public abstract void Add(Company c); //增加
public abstract void Remove(Company c); //移除
public abstract void Display(int depth); //顯示
public abstract void LineOfDuty(); //履行職責
}
class ConcreateCompany : Company //具體公司類樹枝節點
{
private List<Company> children = new List<Company>();
public ConcreateCompany(string name)
: base(name)
{ }
public override void Add(Company c)
{
children.Add(c);
}
public override void Remove(Company c)
{
children.Remove(c);
}
public override void Display(int depth)
{
Console.WriteLine(new string('-',depth )+name );
foreach (Company component in children)
{
component.Display(depth+2);
}
}
public override void LineOfDuty()
{
foreach (Company component in children)
{
component.LineOfDuty();
}
}
}
class HRDepartment : Company //樹枝節點——人力資源部和財務部
{
public HRDepartment(string name)
: base(name)
{ }
public override void Add(Company c)
{ }
public override void Remove(Company c)
{
}
public override void Display(int depth)
{
Console.WriteLine(new string('-',depth )+name );
}
public override void LineOfDuty()
{
Console.WriteLine("{0}員工招聘培訓管理",name );
}
}
class FinanceDepartment : Company //財務部
{
public FinanceDepartment(string name)
: base(name)
{ }
public override void Add(Company c)
{
}
public override void Remove(Company c)
{
}
public override void Display(int depth)
{
Console.WriteLine(new string('-',depth )+name);
}
public override void LineOfDuty()
{
Console.WriteLine("{0}公司財務收支管理",name );
}
}
}
10. 享元模式
運用共享技術有效地支持大量細粒度的對象。如果一個應用使用瞭大量的對象,而大量的這些對象造成很大的存儲開銷應該考慮使用享元模式。享元模式可以避免大量非常相似類的開銷,如果發現一些實例除瞭幾個參數外基本相同,可以大大減少需要實例化的類的數量,,把參數移到類實例的外面,方法調用是傳遞進去。(比如在一個汽車4S店,通過id可以查到汽車的信息,第一次查BMW 730會返回數據庫寶馬730的配置信息,第二次查得時候先從上次查得緩存中找,沒有再找數據庫,其實就是緩存的意思)
UML圖:
代碼:
view plaincopy to clipboardprint?using System.Collections; //引用哈希表
namespace 享元模型
{
class Class1
{
static void Main(string[] args)
{
int extrinsicstate = 22;
FlyweightFactory f = new FlyweightFactory();
Flyweight fx = f.GetFlyweight("X");
fx.Operation(–extrinsicstate );
Flyweight fy = f.GetFlyweight("Y");
fy.Operation(–extrinsicstate );
Flyweight fz = f.GetFlyweight("Z");
fz.Operation(–extrinsicstate);
Flyweight uf = new UnsharedConcreteFlyweight();
uf.Operation(–extrinsicstate );
Console.Read();
}
}
abstract class Flyweight
{
//具體類和享元類的接口
public abstract void Operation(int extrinsicstate);
}
class ConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("具體Flyweight:"+extrinsicstate );
}
}
class UnsharedConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("不共享的具體Flyweight:" + extrinsicstate);
}
}
class FlyweightFactory //享元工廠
{
private Hashtable flyweights = new Hashtable(); //哈希表
public FlyweightFactory()
{
flyweights.Add("X",new ConcreteFlyweight());
flyweights.Add("Y", new ConcreteFlyweight());
flyweights.Add("Z", new ConcreteFlyweight());
}
public Flyweight GetFlyweight(string key)
{
return ((Flyweight )flyweights[key]);
}
}
}
using System.Collections; //引用哈希表
namespace 享元模型
{
class Class1
{
static void Main(string[] args)
{
int extrinsicstate = 22;
FlyweightFactory f = new FlyweightFactory();
Flyweight fx = f.GetFlyweight("X");
fx.Operation(–extrinsicstate );
Flyweight fy = f.GetFlyweight("Y");
fy.Operation(–extrinsicstate );
Flyweight fz = f.GetFlyweight("Z");
fz.Operation(–extrinsicstate);
Flyweight uf = new UnsharedConcreteFlyweight();
uf.Operation(–extrinsicstate );
Console.Read();
}
}
abstract class Flyweight
{
//具體類和享元類的接口
public abstract void Operation(int extrinsicstate);
}
class ConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("具體Flyweight:"+extrinsicstate );
}
}
class UnsharedConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("不共享的具體Flyweight:" + extrinsicstate);
}
}
class FlyweightFactory //享元工廠
{
private Hashtable flyweights = new Hashtable(); //哈希表
public FlyweightFactory()
{
flyweights.Add("X",new ConcreteFlyweight());
flyweights.Add("Y", new ConcreteFlyweight());
flyweights.Add("Z", new ConcreteFlyweight());
}
public Flyweight GetFlyweight(string key)
{
return ((Flyweight )flyweights[key]);
}
}
}
11. 代理模式
為其他對象提供一種代理以控制對這個對象的訪問。代理就是真實對象的代表。(例如代理別人去追一個女生)
UML圖:
代碼:
view plaincopy to clipboardprint?namespace 代理模式
{
class Class2
{
static void Main(string[] args)
{
SchoolGirl jiaojiao = new SchoolGirl();
jiaojiao.Name = "李嬌嬌";
Proxy1 daili = new Proxy1(jiaojiao);
daili.GiveDolls();
daili.GiveFlowers();
daili.GiveChocolate();
Console.ReadLine();
}
}
interface IGiveGift //追求者(真實實體)和代理的接口,代理和真實實體有相同的功能
{
void GiveDolls();
void GiveFlowers();
void GiveChocolate();
}
class Pursuit1 : IGiveGift //追求者(真實實體)
{
SchoolGirl mm;
public Pursuit1(SchoolGirl mm)
{
this.mm = mm;
}
public void GiveDolls()
{
Console.WriteLine(mm.Name +"送你洋娃娃");
}
public void GiveFlowers()
{
Console.WriteLine(mm.Name+"送你鮮花");
}
public void GiveChocolate()
{
Console.WriteLine(mm.Name +"送你巧克力");
}
}
class Proxy1 : IGiveGift //代理
{
Pursuit1 gg;
public Proxy1(SchoolGirl mm)
{
gg = new Pursuit1(mm);
}
public void GiveDolls()
{
gg.GiveDolls();
}
public void GiveFlowers()
{
gg.GiveFlowers();
}
public void GiveChocolate()
{
gg.GiveChocolate();
}
}
}
namespace 代理模式
{
class Class2
{
static void Main(string[] args)
{
SchoolGirl jiaojiao = new SchoolGirl();
jiaojiao.Name = "李嬌嬌";
Proxy1 daili = new Proxy1(jiaojiao);
daili.GiveDolls();
daili.GiveFlowers();
daili.GiveChocolate();
Console.ReadLine();
}
}
interface IGiveGift //追求者(真實實體)和代理的接口,代理和真實實體有相同的功能
{
void GiveDolls();
void GiveFlowers();
void GiveChocolate();
}
class Pursuit1 : IGiveGift //追求者(真實實體)
{
SchoolGirl mm;
public Pursuit1(SchoolGirl mm)
{
this.mm = mm;
}
public void GiveDolls()
{
Console.WriteLine(mm.Name +"送你洋娃娃");
}
public void GiveFlowers()
{
Console.WriteLine(mm.Name+"送你鮮花");
}
public void GiveChocolate()
{
Console.WriteLine(mm.Name +"送你巧克力");
}
}
class Proxy1 : IGiveGift //代理
{
Pursuit1 gg;
public Proxy1(SchoolGirl mm)
{
gg = new Pursuit1(mm);
}
public void GiveDolls()
{
gg.GiveDolls();
}
public void GiveFlowers()
{
gg.GiveFlowers();
}
public void GiveChocolate()
{
gg.GiveChocolate();
}
}
}
12. 外觀模式
為子系統中的一組接口提供一個一致的界面,此模式定義瞭一個高層接口,這個接口使得這一子系統更加容易使用。增加外觀可以提供一個簡單的接口,減少他們之間的依賴。通過接口可以實現一系列已經封裝好的方法。(比如我們身邊的移動公司,10086的客服就扮演著一個外觀的角色,通過它你可以辦理你需要的若幹個業務)
UML圖:
代碼:
view plaincopy to clipboardprint?//外觀模式將不同的層之間進行分離,層與層之間建立外觀,為復雜的子系統提供一個簡單的接口,耦合性大大降低
namespace 外觀模式
{
class Class2
{
static void Main(string[] args)
{
Facde facde = new Facde();
facde.MethodA(); //有外觀類的作用,客戶可以不知道子系統的作用而通過外觀類進行調用(就像基金一樣,基金是由若幹支股票組成的)
facde.MethodB();
Console.Read();
}
}
//四個子系統類
class SubSystemOne
{
public void MethodOne()
{
Console.WriteLine("子系統方法1");
}
}
class SubSystemTwo
{
public void MethodTwo()
{
Console.WriteLine("子系統方法2");
}
}
class SubSystemThree
{
public void MethodThree()
{
Console.WriteLine("子系統方法3");
}
}
class SubSystemFour
{
public void MethodFour()
{
Console.WriteLine("子系統方法4");
}
}
//外觀類
class Facde
{
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
public Facde()
{
one = new SubSystemOne();
two = new SubSystemTwo();
three = new SubSystemThree();
four = new SubSystemFour();
}
public void MethodA()
{
Console.WriteLine("\n方法組A()——");
one.MethodOne();
two.MethodTwo();
four.MethodFour();
}
public void MethodB()
{
Console.WriteLine("\n方法組B()——");
two.MethodTwo();
three.MethodThree();
}
}
}
作者“許德鵬的專欄”