




已阅读5页,还剩67页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1.DoFactory.GangOfFour.Abstract.Structural Abstract Factory:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 工厂模式:客户类和工厂类分开。消费者任何时候需要某种产品,只需向工厂请求即可。消费者无须修改就可以接纳新产品。缺点是当产品修改时,工厂类也要做相应的修改。如:如何创建及如何向客户端提供。Codeusing System;namespace DoFactory.GangOfFour.Abstract.Structural / / MainApp startup class for Structural / Abstract Factory Design Pattern. / class MainApp / / Entry point into console application. / public static void Main() / Abstract factory #1 AbstractFactory factory1 = new ConcreteFactory1(); Client client1 = new Client(factory1); client1.Run(); / Abstract factory #2 AbstractFactory factory2 = new ConcreteFactory2(); Client client2 = new Client(factory2); client2.Run(); / Wait for user input Console.Read(); / AbstractFactory abstract class AbstractFactory public abstract AbstractProductA CreateProductA(); public abstract AbstractProductB CreateProductB(); / ConcreteFactory1 class ConcreteFactory1 : AbstractFactory public override AbstractProductA CreateProductA() return new ProductA1(); public override AbstractProductB CreateProductB() return new ProductB1(); / ConcreteFactory2 class ConcreteFactory2 : AbstractFactory public override AbstractProductA CreateProductA() return new ProductA2(); public override AbstractProductB CreateProductB() return new ProductB2(); / AbstractProductA abstract class AbstractProductA / AbstractProductB abstract class AbstractProductB public abstract void Interact(AbstractProductA a); / ProductA1 class ProductA1 : AbstractProductA / ProductB1 class ProductB1 : AbstractProductB public override void Interact(AbstractProductA a) Console.WriteLine(this.GetType().Name + interacts with + a.GetType().Name); / ProductA2 class ProductA2 : AbstractProductA / ProductB2 class ProductB2 : AbstractProductB public override void Interact(AbstractProductA a) Console.WriteLine(this.GetType().Name + interacts with + a.GetType().Name); / Client - the interaction environment of the products class Client private AbstractProductA AbstractProductA; private AbstractProductB AbstractProductB; / Constructor public Client(AbstractFactory factory) AbstractProductB = factory.CreateProductB(); AbstractProductA = factory.CreateProductA(); public void Run() AbstractProductB.Interact(AbstractProductA); 2.DoFactory.GangOfFour.Adapter.Structural Adapter:将一个类的接口转换成客户希望的另一个接口,使得原来由于接口不兼容而不能一起工作的那些类可以一起工作。适配器(变压器)模式:把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参数返还一个合适的实例给客户端。using System;namespace DoFactory.GangOfFour.Adapter.Structural / / MainApp startup class for Structural / Adapter Design Pattern. / class MainApp / / Entry point into console application. / static void Main() / Create adapter and place a request Target target = new Adapter(); target.Request(); / Wait for user Console.Read(); / Target class Target public virtual void Request() Console.WriteLine(Called Target Request(); / Adapter class Adapter : Target private Adaptee adaptee = new Adaptee(); public override void Request() / Possibly do some other work / and then call SpecificRequest adaptee.SpecificRequest(); / Adaptee class Adaptee public void SpecificRequest() Console.WriteLine(Called SpecificRequest(); 3.DoFactory.GangOfFour.Bridge.Structural Bridge:将抽象部分与它的实现部分分离,使之可以独立变化。 桥梁模式:将抽象化与实现化脱耦,使得二者可以独立的变化,也就是说将他们之间的强关联变成弱关联,也就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系,从而使两者可以独立的变化。Codeusing System;namespace DoFactory.GangOfFour.Bridge.Structural / / MainApp startup class for Structural / Bridge Design Pattern. / class MainApp / / Entry point into console application. / static void Main() Abstraction ab = new RefinedAbstraction(); / Set implementation and call ab.Implementor = new ConcreteImplementorA(); ab.Operation(); / Change implemention and call ab.Implementor = new ConcreteImplementorB(); ab.Operation(); / Wait for user Console.Read(); / Abstraction class Abstraction protected Implementor implementor; / Property public Implementor Implementor set implementor = value; public virtual void Operation() implementor.Operation(); / Implementor abstract class Implementor public abstract void Operation(); / RefinedAbstraction class RefinedAbstraction : Abstraction public override void Operation() implementor.Operation(); / ConcreteImplementorA class ConcreteImplementorA : Implementor public override void Operation() Console.WriteLine(ConcreteImplementorA Operation); / ConcreteImplementorB class ConcreteImplementorB : Implementor public override void Operation() Console.WriteLine(ConcreteImplementorB Operation); 4.DoFactory.GangOfFour.Builder.StructuralBuilder:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。建造者模式:将产品的内部表象和产品的生成过程分割开来,从而使一个建造过程生成具有不同的内部表象的产品对象。建造模式使得产品内部表象可以独立的变化,客户不必知道产品内部组成的细节。建造模式可以强制实行一种分步骤进行的建造过程。Codeusing System;using System.Collections;namespace DoFactory.GangOfFour.Builder.Structural / / MainApp startup class for Real-World / Builder Design Pattern. / public class MainApp / / Entry point into console application. / public static void Main() / Create director and builders Director director = new Director(); Builder b1 = new ConcreteBuilder1(); Builder b2 = new ConcreteBuilder2(); / Construct two products director.Construct(b1); Product p1 = b1.GetResult(); p1.Show(); director.Construct(b2); Product p2 = b2.GetResult(); p2.Show(); / Wait for user Console.Read(); / Director class Director / Builder uses a complex series of steps public void Construct(Builder builder) builder.BuildPartA(); builder.BuildPartB(); / Builder abstract class Builder public abstract void BuildPartA(); public abstract void BuildPartB(); public abstract Product GetResult(); / ConcreteBuilder1 class ConcreteBuilder1 : Builder private Product product = new Product(); public override void BuildPartA() product.Add(PartA); public override void BuildPartB() product.Add(PartB); public override Product GetResult() return product; / ConcreteBuilder2 class ConcreteBuilder2 : Builder private Product product = new Product(); public override void BuildPartA() product.Add(PartX); public override void BuildPartB() product.Add(PartY); public override Product GetResult() return product; / Product class Product ArrayList parts = new ArrayList(); public void Add(string part) parts.Add(part); public void Show() Console.WriteLine(nProduct Parts -); foreach (string part in parts) Console.WriteLine(part); 5.DoFactory.GangOfFour.Chain.StructuralChain of Responsibility:为解除请求的发送者和接收者之间的耦合,而使多个对象有机会处理这个请求。将这些请求连成一个链,并沿着这条链传递该请求,直到有个对象处理它。 责任链模式:在责任链模式中,很多对象由每一个对象对其下家的引用而接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。客户并不知道链上的哪一个对象最终处理这个请求,系统可以在不影响客户端的情况下动态的重新组织链和分配责任。处理者有两个选择:承担责任或者把责任推给下家。一个请求可以最终不被任何接收端对象所接受。Codeusing System;namespace DoFactory.GangOfFour.Chain.Structural / / MainApp startup class for Structural / Chain of Responsibility Design Pattern. / class MainApp / / Entry point into console application. / static void Main() / Setup Chain of Responsibility Handler h1 = new ConcreteHandler1(); Handler h2 = new ConcreteHandler2(); Handler h3 = new ConcreteHandler3(); h1.SetSuccessor(h2); h2.SetSuccessor(h3); / Generate and process request int requests = 2, 5, 14, 22, 18, 3, 27, 20; foreach (int request in requests) h1.HandleRequest(request); / Wait for user Console.Read(); / Handler abstract class Handler protected Handler successor; public void SetSuccessor(Handler successor) this.successor = successor; public abstract void HandleRequest(int request); / ConcreteHandler1 class ConcreteHandler1 : Handler public override void HandleRequest(int request) if (request = 0 & request = 10 & request = 20 & request 30) Console.WriteLine(0 handled request 1, this.GetType().Name, request); else if (successor != null) successor.HandleRequest(request); 6.DoFactory.GangOfFour.Command.StructuralCommand:将一个请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可以取消的操作。命令模式:命令模式把一个请求或者操作封装到一个对象中。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。命令模式允许请求的一方和发送的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否执行,何时被执行以及是怎么被执行的。系统支持命令的撤消。using System;namespace DoFactory.GangOfFour.Command.Structural / / MainApp startup class for Structural / Command Design Pattern. / class MainApp / / Entry point into console application. / static void Main() / Create receiver, command, and invoker Receiver receiver = new Receiver(); Command command = new ConcreteCommand(receiver); Invoker invoker = new Invoker(); / Set and execute command invoker.SetCommand(command); invoker.ExecuteCommand(); / Wait for user Console.Read(); / Command abstract class Command protected Receiver receiver; / Constructor public Command(Receiver receiver) this.receiver = receiver; public abstract void Execute(); / ConcreteCommand class ConcreteCommand : Command / Constructor public ConcreteCommand(Receiver receiver) : base(receiver) public override void Execute() receiver.Action(); / Receiver class Receiver public void Action() Console.WriteLine(Called Receiver.Action(); / Invoker class Invoker private Command command; public void SetCommand(Command command) mand = command; public void ExecuteCommand() command.Execute(); 7.DoFactory.GangOfFour.Composite.StructuralComposite:将对象组合成树形结构以表示“部分整体”的层次结构。Composite使得客户对单个对象和复合对象的使用具有一致性。 合成模式:合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式就是一个处理对象的树结构的模式。合成模式把部分与整体的关系用树结构表示出来。合成模式使得客户端把一个个单独的成分对象和由他们复合而成的合成对象同等看待。 Codeusing System;using System.Collections;namespace DoFactory.GangOfFour.Composite.Structural / / MainApp startup class for Structural / Composite Design Pattern. / class MainApp / / Entry point into console application. / static void Main() / Create a tree structure 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); root.Add(new Leaf(Leaf C); / Add and remove a leaf Leaf leaf = new Leaf(Leaf D); root.Add(leaf); root.Remove(leaf); / Recursively display tree root.Display(1); / Wait for user Console.Read(); / Component abstract class Component protected string name; / Constructor public Component(string name) = name; public abstract void Add(Component c); public abstract void Remove(Component c); public abstract void Display(int depth); / Composite class Composite : Component private ArrayList children = new ArrayList(); / Constructor public Composite(string name) : base(name) public override void Add(Component component) children.Add(component); public override void Remove(Component component) children.Remove(component); public override void Display(int depth) Console.WriteLine(new String(-, depth) + name); / Recursively display child nodes foreach (Component component in children) component.Display(depth + 2); / Leaf class Leaf : Component / Constructor public Leaf(string name) : base(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); 8. DoFactory.GangOfFour.Decorator.StructuralDecorator:动态地给一个对象添加一些额外的职责。就扩展功能而言,Decorator模式比生成子类方式更加灵活。 装饰模式:装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案,提供比继承更多的灵活性。动态给一个对象增加功能,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安全检查仪表培训内容课件
- 餐厅员工退休申请书
- 出差申请书文案
- 申请书怎么开
- 儿童重病救助申请书
- 承办方申请书
- 春节农村低保申请书
- 四平市中石化2025秋招笔试模拟题含答案炼油工艺技术岗
- 威海市中石化2025秋招笔试模拟题含答案行测综合英语
- 沧州市中石油2025秋招面试半结构化模拟题及答案机械与动力工程岗
- 职高课件模板
- 【生物】第四节 激素调节课件-2025-2026学年人教版生物八年级上册
- 卫生院安全生产培训课件
- 医院信息安全保密培训课件
- 物流紧急事件应急预案方案
- 期中专题复习-词汇句型训练-2025-2026学年 译林版2024 英语八年级上册 原卷
- 2025年全国中小学校科普知识竞赛题库(+答案)
- 2.2创新永无止境教学课件 2025-2026学年度九年级上册 道德与法治 统编版
- 幼儿创意玉米课件
- 矿山爆破作业安全培训课件
- 2025-2026学年九年级英语上学期第一次月考 (四川成都专用) 2025-2026学年九年级英语上学期第一次月考 (四川成都专用)解析卷
评论
0/150
提交评论