軟件設(shè)計(jì)師23種設(shè)計(jì)模式總結(jié)_第1頁(yè)
軟件設(shè)計(jì)師23種設(shè)計(jì)模式總結(jié)_第2頁(yè)
軟件設(shè)計(jì)師23種設(shè)計(jì)模式總結(jié)_第3頁(yè)
軟件設(shè)計(jì)師23種設(shè)計(jì)模式總結(jié)_第4頁(yè)
軟件設(shè)計(jì)師23種設(shè)計(jì)模式總結(jié)_第5頁(yè)
已閱讀5頁(yè),還剩48頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、創(chuàng)建型結(jié)構(gòu)型行為型類Factory MethodAdapterInterpreterTemplate Method對(duì)象Abstract FactoryBuilderPrototypeSingletonApapter(對(duì)象)BridgeCompositeDecoratorFaadeFlyweightProxyChain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategyVisitor1. Abstract Factory(抽象工廠)提供一個(gè)創(chuàng)建一系列相關(guān)或互相依賴對(duì)象的接口,而無(wú)須制定它們具體的類。Abstra

2、ct Factory抽象工廠 class Program static void Main(string args) AbstractFactory factory1 = new ConcreteFactory1(); Client c1 = new Client(factory1); c1.Run(); AbstractFactory factory2 = new ConcreteFactory2(); Client c2 = new Client(factory2); c2.Run(); Console.Read(); abstract class AbstractFactory publ

3、ic abstract AbstractProductA CreateProductA(); public abstract AbstractProductB CreateProductB(); class ConcreteFactory1 : AbstractFactory public override AbstractProductA CreateProductA() return new ProductA1(); public override AbstractProductB CreateProductB() return new ProductB1(); class Concret

4、eFactory2 : AbstractFactory public override AbstractProductA CreateProductA() return new ProductA2(); public override AbstractProductB CreateProductB() return new ProductB2(); abstract class AbstractProductA abstract class AbstractProductB public abstract void Interact(AbstractProductA a); class Pro

5、ductA1 : AbstractProductA class ProductB1 : AbstractProductB public override void Interact(AbstractProductA a) Console.WriteLine(this.GetType().Name + interacts with + a.GetType().Name); class ProductA2 : AbstractProductA class ProductB2 : AbstractProductB public override void Interact(AbstractProdu

6、ctA a) Console.WriteLine(this.GetType().Name + interacts with + a.GetType().Name); class Client private AbstractProductA AbstractProductA; private AbstractProductB AbstractProductB; / Constructor public Client(AbstractFactory factory) AbstractProductB = factory.CreateProductB(); AbstractProductA = f

7、actory.CreateProductA(); public void Run() AbstractProductB.Interact(AbstractProductA); 2. Builder(生成器)將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表示分離,使得同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示。Builder 生成器 class Program static void Main(string args) Director director = new Director(); Builder b1 = new ConcreteBuilder1(); Builder b2 = new ConcreteBuilde

8、r2(); director.Construct(b1); Product p1 = b1.GetResult(); p1.Show(); director.Construct(b2); Product p2 = b2.GetResult(); p2.Show(); Console.Read(); class Director public void Construct(Builder builder) builder.BuildPartA(); builder.BuildPartB(); abstract class Builder public abstract void BuildPar

9、tA(); public abstract void BuildPartB(); public abstract Product GetResult(); class ConcreteBuilder1 : Builder private Product product = new Product(); public override void BuildPartA() product.Add(部件A); public override void BuildPartB() product.Add(部件B); public override Product GetResult() return p

10、roduct; class ConcreteBuilder2 : Builder private Product product = new Product(); public override void BuildPartA() product.Add(部件X); public override void BuildPartB() product.Add(部件Y); public override Product GetResult() return product; class Product IList parts = new List(); public void Add(string

11、 part) parts.Add(part); public void Show() Console.WriteLine(n產(chǎn)品 創(chuàng)建 -); foreach (string part in parts) Console.WriteLine(part); 3. Factory Method(工廠方法)定義一個(gè)用于創(chuàng)建對(duì)象的接口,讓子類決定實(shí)例化哪一個(gè)類。Factory Method使一個(gè)實(shí)例化延遲到其子類。Factory Method 工廠方法 class Program static void Main(string args) / /基本方式:薛磊風(fēng)代表大學(xué)生學(xué)習(xí)雷鋒 LeiFeng xu

12、eleifeng = new Undergraduate(); xueleifeng.BuyRice(); xueleifeng.Sweep(); xueleifeng.Wash(); LeiFeng student1 = new Undergraduate(); student1.BuyRice(); LeiFeng student2 = new Undergraduate(); student2.Sweep(); LeiFeng student3 = new Undergraduate(); student3.Wash(); /簡(jiǎn)單工廠模式 LeiFeng studentA = Simpl

13、eFactory.CreateLeiFeng(學(xué)雷鋒的大學(xué)生); studentA.BuyRice(); LeiFeng studentB = SimpleFactory.CreateLeiFeng(學(xué)雷鋒的大學(xué)生); studentB.Sweep(); LeiFeng studentC = SimpleFactory.CreateLeiFeng(學(xué)雷鋒的大學(xué)生); studentC.Wash(); /工廠方法模式 IFactory factory = new UndergraduateFactory(); LeiFeng student = factory.CreateLeiFeng();

14、student.BuyRice(); student.Sweep(); student.Wash(); Console.Read(); /雷鋒 class LeiFeng public void Sweep() Console.WriteLine(掃地); public void Wash() Console.WriteLine(洗衣); public void BuyRice() Console.WriteLine(買米); /學(xué)雷鋒的大學(xué)生 class Undergraduate : LeiFeng /社區(qū)志愿者 class Volunteer : LeiFeng /簡(jiǎn)單雷鋒工廠 clas

15、s SimpleFactory public static LeiFeng CreateLeiFeng(string type) LeiFeng result = null; switch (type) case 學(xué)雷鋒的大學(xué)生: result = new Undergraduate(); break; case 社區(qū)志愿者: result = new Volunteer(); break; return result; /雷鋒工廠 interface IFactory LeiFeng CreateLeiFeng(); /學(xué)雷鋒的大學(xué)生工廠 class UndergraduateFactory

16、 : IFactory public LeiFeng CreateLeiFeng() return new Undergraduate(); /社區(qū)志愿者工廠 class VolunteerFactory : IFactory public LeiFeng CreateLeiFeng() return new Volunteer(); 4. Prototype(原型)用原型實(shí)例制定創(chuàng)建對(duì)象的種類,并且通過(guò)復(fù)制這些原型創(chuàng)建新的對(duì)象。Prototype 原型 class Program static void Main(string args) ConcretePrototype1 p1 = ne

17、w ConcretePrototype1(I); ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone(); Console.WriteLine(Cloned: 0, c1.Id); ConcretePrototype2 p2 = new ConcretePrototype2(II); ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone(); Console.WriteLine(Cloned: 0, c2.Id); / Wait for user Console.Read(); abstr

18、act class Prototype private string id; / Constructor public Prototype(string id) this.id = id; / Property public string Id get return id; public abstract Prototype Clone(); class ConcretePrototype1 : Prototype / Constructor public ConcretePrototype1(string id) : base(id) public override Prototype Cl

19、one() / Shallow copy return (Prototype)this.MemberwiseClone(); class ConcretePrototype2 : Prototype / Constructor public ConcretePrototype2(string id) : base(id) public override Prototype Clone() / Shallow copy return (Prototype)this.MemberwiseClone(); 5. Singleton(單例)保證一個(gè)類僅有一個(gè)實(shí)例,并提供一個(gè)訪問(wèn)它的全局訪問(wèn)點(diǎn)。Sing

20、leton class Program static void Main(string args) Singleton s1 = Singleton.GetInstance(); Singleton s2 = Singleton.GetInstance(); if (s1 = s2) Console.WriteLine(Objects are the same instance); Console.Read(); class Singleton private static Singleton instance; private static readonly object syncRoot

21、= new object(); private Singleton() public static Singleton GetInstance() if (instance = null) lock (syncRoot) if (instance = null) instance = new Singleton(); return instance; 1. Adapter(適配器)將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另一個(gè)接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作。Adapter class Program static void Main(string arg

22、s) Target target = new Adapter(); target.Request(); Console.Read(); class Target public virtual void Request() Console.WriteLine(普通請(qǐng)求); class Adaptee public void SpecificRequest() Console.WriteLine(特殊請(qǐng)求); class Adapter : Target private Adaptee adaptee = new Adaptee(); public override void Request()

23、adaptee.SpecificRequest(); 2. Bridge(橋接)將抽象部分與其實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立的變化。Bridge class Program static void Main(string args) Abstraction ab = new RefinedAbstraction(); ab.SetImplementor(new ConcreteImplementorA(); ab.Operation(); ab.SetImplementor(new ConcreteImplementorB(); ab.Operation(); Console.Read();

24、class Abstraction protected Implementor implementor; public void SetImplementor(Implementor implementor) this.implementor = implementor; public virtual void Operation() implementor.Operation(); class RefinedAbstraction : Abstraction public override void Operation() implementor.Operation(); abstract

25、class Implementor public abstract void Operation(); class ConcreteImplementorA : Implementor public override void Operation() Console.WriteLine(具體實(shí)現(xiàn)A的方法執(zhí)行); class ConcreteImplementorB : Implementor public override void Operation() Console.WriteLine(具體實(shí)現(xiàn)B的方法執(zhí)行); 3. Composite(組合)將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示“部分-整體”的層

26、次結(jié)構(gòu)。Composite使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性。Composite class Program static void Main(string args) Composite root = new Composite(root); root.Add(new Leaf(Leaf A); root.Add(new Leaf(Leaf B); Composite comp = new Composite(Composite X); comp.Add(new Leaf(Leaf XA); comp.Add(new Leaf(Leaf XB); root.Add(comp); C

27、omposite comp2 = new Composite(Composite XY); comp2.Add(new Leaf(Leaf XYA); comp2.Add(new Leaf(Leaf XYB); comp.Add(comp2); root.Add(new Leaf(Leaf C); Leaf leaf = new Leaf(Leaf D); root.Add(leaf); root.Remove(leaf); root.Display(1); Console.Read(); abstract class Component protected string name; publ

28、ic Component(string name) = name; public abstract void Add(Component c); public abstract void Remove(Component c); public abstract void Display(int depth); class Composite : Component private List children = new List(); public Composite(string name) : base(name) public override void Add(Co

29、mponent c) children.Add(c); public override void Remove(Component c) children.Remove(c); public override void Display(int depth) Console.WriteLine(new String(-, depth) + name); foreach (Component component in children) component.Display(depth + 2); class Leaf : Component public Leaf(string name) : b

30、ase(name) public override void Add(Component c) Console.WriteLine(Cannot add to a leaf); public override void Remove(Component c) Console.WriteLine(Cannot remove from a leaf); public override void Display(int depth) Console.WriteLine(new String(-, depth) + name); 4. Decorator(裝飾)動(dòng)態(tài)地給一個(gè)對(duì)象添加一些額外的職責(zé)。就增

31、加功能而言,Decorator模式比生成子類更加靈活。Decorator class Program static void Main(string args) ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); Console.Read();

32、 abstract class Component public abstract void Operation(); class ConcreteComponent : Component public override void Operation() Console.WriteLine(具體對(duì)象的操作); abstract class Decorator : Component protected Component component; public void SetComponent(Component component) ponent = component; p

33、ublic override void Operation() if (component != null) component.Operation(); class ConcreteDecoratorA : Decorator private string addedState; public override void Operation() base.Operation(); addedState = New State; Console.WriteLine(具體裝飾對(duì)象A的操作); class ConcreteDecoratorB : Decorator public override

34、 void Operation() base.Operation(); AddedBehavior(); Console.WriteLine(具體裝飾對(duì)象B的操作); private void AddedBehavior() 5. Faade(外觀)為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,F(xiàn)aade模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用。Facade class Program static void Main(string args) Facade facade = new Facade(); facade.MethodA(); facade.MethodB(); Con

35、sole.Read(); class SubSystemOne public void MethodOne() Console.WriteLine( 子系統(tǒng)方法一); class SubSystemTwo public void MethodTwo() Console.WriteLine( 子系統(tǒng)方法二); class SubSystemThree public void MethodThree() Console.WriteLine( 子系統(tǒng)方法三); class SubSystemFour public void MethodFour() Console.WriteLine( 子系統(tǒng)方法四

36、); class Facade SubSystemOne one; SubSystemTwo two; SubSystemThree three; SubSystemFour four; public Facade() 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(); 6. Flyweight(享元)運(yùn)用共享技術(shù)有效地支持大量細(xì)粒度的對(duì)象。Flyweight class Program static void Main(string args) int extrinsicstate

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論