您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 23种设计模式_加示例代码
23种设计模式UML类图及对应示例代码(一)收藏1.DoFactory.GangOfFour.Abstract.StructuralAbstractFactory:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。工厂模式:客户类和工厂类分开。消费者任何时候需要某种产品,只需向工厂请求即可。消费者无须修改就可以接纳新产品。缺点是当产品修改时,工厂类也要做相应的修改。如:如何创建及如何向客户端提供。usingSystem;namespaceDoFactory.GangOfFour.Abstract.Structural{///summary///MainAppstartupclassforStructural///AbstractFactoryDesignPattern.////summaryclassMainApp{///summary///Entrypointintoconsoleapplication.////summarypublicstaticvoidMain(){//Abstractfactory#1AbstractFactoryfactory1=newConcreteFactory1();Clientclient1=newClient(factory1);client1.Run();//Abstractfactory#2AbstractFactoryfactory2=newConcreteFactory2();Clientclient2=newClient(factory2);client2.Run();//WaitforuserinputConsole.Read();}}//AbstractFactoryabstractclassAbstractFactory{publicabstractAbstractProductACreateProductA();publicabstractAbstractProductBCreateProductB();}//ConcreteFactory1classConcreteFactory1:AbstractFactory{publicoverrideAbstractProductACreateProductA(){returnnewProductA1();}publicoverrideAbstractProductBCreateProductB(){returnnewProductB1();}}//ConcreteFactory2classConcreteFactory2:AbstractFactory{publicoverrideAbstractProductACreateProductA(){returnnewProductA2();}publicoverrideAbstractProductBCreateProductB(){returnnewProductB2();}}//AbstractProductAabstractclassAbstractProductA{}//AbstractProductBabstractclassAbstractProductB{publicabstractvoidInteract(AbstractProductAa);}//ProductA1classProductA1:AbstractProductA{}//ProductB1classProductB1:AbstractProductB{publicoverridevoidInteract(AbstractProductAa){Console.WriteLine(this.GetType().Name+interactswith+a.GetType().Name);}}//ProductA2classProductA2:AbstractProductA{}//ProductB2classProductB2:AbstractProductB{publicoverridevoidInteract(AbstractProductAa){Console.WriteLine(this.GetType().Name+interactswith+a.GetType().Name);}}//Client-theinteractionenvironmentoftheproductsclassClient{privateAbstractProductAAbstractProductA;privateAbstractProductBAbstractProductB;//ConstructorpublicClient(AbstractFactoryfactory){AbstractProductB=factory.CreateProductB();AbstractProductA=factory.CreateProductA();}publicvoidRun(){AbstractProductB.Interact(AbstractProductA);}}}2.DoFactory.GangOfFour.Adapter.StructuralAdapter:将一个类的接口转换成客户希望的另一个接口,使得原来由于接口不兼容而不能一起工作的那些类可以一起工作。适配器(变压器)模式:把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参数返还一个合适的实例给客户端。usingSystem;namespaceDoFactory.GangOfFour.Adapter.Structural{///summary///MainAppstartupclassforStructural///AdapterDesignPattern.////summaryclassMainApp{///summary///Entrypointintoconsoleapplication.////summarystaticvoidMain(){//CreateadapterandplacearequestTargettarget=newAdapter();target.Request();//WaitforuserConsole.Read();}}//TargetclassTarget{publicvirtualvoidRequest(){Console.WriteLine(CalledTargetRequest());}}//AdapterclassAdapter:Target{privateAdapteeadaptee=newAdaptee();publicoverridevoidRequest(){//Possiblydosomeotherwork//andthencallSpecificRequestadaptee.SpecificRequest();}}//AdapteeclassAdaptee{publicvoidSpecificRequest(){Console.WriteLine(CalledSpecificRequest());}}}3.DoFactory.GangOfFour.Bridge.StructuralBridge:将抽象部分与它的实现部分分离,使之可以独立变化。桥梁模式:将抽象化与实现化脱耦,使得二者可以独立的变化,也就是说将他们之间的强关联变成弱关联,也就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系,从而使两者可以独立的变化。usingSystem;namespaceDoFactory.GangOfFour.Bridge.Structural{///summary///MainAppstartupclassforStructural///BridgeDesignPattern.////summaryclassMainApp{///summary///Entrypointintoconsoleapplication.////summarystaticvoidMain(){Abstractionab=newRefinedAbstraction();//Setimplementationandcallab.Implementor=newConcreteImplementorA();ab.Operation();//Changeimplementionandcallab.Implementor=newConcreteImplementorB();ab.Operation();//WaitforuserConsole.Read();}}//AbstractionclassAbstraction{protectedImplementorimplementor;//PropertypublicImplementorImplementor{set{implementor=value;}}publicvirtualvoidOperation(){implementor.Operation();}}//ImplementorabstractclassImplementor{publicabstractvoidOperation();}//RefinedAbstractionclassRefinedAbstraction:Abstraction{publicoverridevoidOperation(){implementor.Operation();}}//ConcreteImplementorAclassConcreteImplementorA:Implementor{publicoverridevoidOperation(){Console.WriteLine(ConcreteImplementorAOperation);}}//ConcreteImplementorBclassConcreteImplementorB:Implementor{publicoverridevoidOperation(){Console.WriteLine(ConcreteImplementorBOperation);}}}4.DoFactory.GangOfFour.Builder.StructuralBuilder:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。建造者模式:将产品的内部表象和产品的生成过程分割开来,从而使一个建造过程生成具有不同的内部表象的产品对象。建造模式使得产品内部表象可以独立的变化,客户不必知道产品内部组成的细节。建造模式可以强制实行一种分步骤进行的建造过程。usingSystem;usingSystem.Collections;namespaceDoFactory.GangOfFour.Builder.Structural{///summary///MainAppstartupclassforReal-World///BuilderDesignPattern.////summarypublicclassMainApp{///summary///Entrypointintoconsoleapplication.////summarypublicstaticvoidMain(){//CreatedirectorandbuildersDirectordirector=newDirector();Builderb1=newConcreteBuilder1();Builderb2=newConcreteBuilder2();//Constructtwoproductsdirector.Construct(b1);Productp1=b1.GetResult();p1.Show();director.Construct(
本文标题:23种设计模式_加示例代码
链接地址:https://www.777doc.com/doc-6126230 .html