您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > 软件设计师23种设计模式总结
1/54创建型结构型行为型类FactoryMethodAdapterInterpreterTemplateMethod对象AbstractFactoryBuilderPrototypeSingletonApapter(对象)BridgeCompositeDecoratorFaçadeFlyweightProxyChainofResponsibilityCommandIteratorMediatorMementoObserverStateStrategyVisitor1.AbstractFactory(抽象工厂)提供一个创建一系列相关或互相依赖对象的接口,而无须制定它们具体的类。AbstractFactory抽象工厂classProgram{staticvoidMain(string[]args){AbstractFactoryfactory1=newConcreteFactory1();Clientc1=newClient(factory1);c1.Run();AbstractFactoryfactory2=newConcreteFactory2();Clientc2=newClient(factory2);c2.Run();Console.Read();2/54}}abstractclassAbstractFactory{publicabstractAbstractProductACreateProductA();publicabstractAbstractProductBCreateProductB();}classConcreteFactory1:AbstractFactory{publicoverrideAbstractProductACreateProductA(){returnnewProductA1();}publicoverrideAbstractProductBCreateProductB(){returnnewProductB1();}}classConcreteFactory2:AbstractFactory{publicoverrideAbstractProductACreateProductA(){returnnewProductA2();}publicoverrideAbstractProductBCreateProductB(){returnnewProductB2();}}abstractclassAbstractProductA{}abstractclassAbstractProductB{publicabstractvoidInteract(AbstractProductAa);}classProductA1:AbstractProductA{}classProductB1:AbstractProductB{publicoverridevoidInteract(AbstractProductAa)3/54{Console.WriteLine(this.GetType().Name+interactswith+a.GetType().Name);}}classProductA2:AbstractProductA{}classProductB2:AbstractProductB{publicoverridevoidInteract(AbstractProductAa){Console.WriteLine(this.GetType().Name+interactswith+a.GetType().Name);}}classClient{privateAbstractProductAAbstractProductA;privateAbstractProductBAbstractProductB;//ConstructorpublicClient(AbstractFactoryfactory){AbstractProductB=factory.CreateProductB();AbstractProductA=factory.CreateProductA();}publicvoidRun(){AbstractProductB.Interact(AbstractProductA);}}2.Builder(生成器)将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。4/54Builder生成器classProgram{staticvoidMain(string[]args){Directordirector=newDirector();Builderb1=newConcreteBuilder1();Builderb2=newConcreteBuilder2();director.Construct(b1);Productp1=b1.GetResult();p1.Show();director.Construct(b2);Productp2=b2.GetResult();p2.Show();Console.Read();}}classDirector{publicvoidConstruct(Builderbuilder){builder.BuildPartA();builder.BuildPartB();}}abstractclassBuilder{publicabstractvoidBuildPartA();5/54publicabstractvoidBuildPartB();publicabstractProductGetResult();}classConcreteBuilder1:Builder{privateProductproduct=newProduct();publicoverridevoidBuildPartA(){product.Add(部件A);}publicoverridevoidBuildPartB(){product.Add(部件B);}publicoverrideProductGetResult(){returnproduct;}}classConcreteBuilder2:Builder{privateProductproduct=newProduct();publicoverridevoidBuildPartA(){product.Add(部件X);}publicoverridevoidBuildPartB(){product.Add(部件Y);}publicoverrideProductGetResult(){returnproduct;}}classProduct{IListstringparts=newListstring();publicvoidAdd(stringpart)6/54{parts.Add(part);}publicvoidShow(){Console.WriteLine(\n产品创建----);foreach(stringpartinparts){Console.WriteLine(part);}}}3.FactoryMethod(工厂方法)定义一个用于创建对象的接口,让子类决定实例化哪一个类。FactoryMethod使一个实例化延迟到其子类。FactoryMethod工厂方法classProgram{staticvoidMain(string[]args){////基本方式:薛磊风代表大学生学习雷锋LeiFengxueleifeng=newUndergraduate();xueleifeng.BuyRice();xueleifeng.Sweep();xueleifeng.Wash();LeiFengstudent1=newUndergraduate();student1.BuyRice();7/54LeiFengstudent2=newUndergraduate();student2.Sweep();LeiFengstudent3=newUndergraduate();student3.Wash();//简单工厂模式LeiFengstudentA=SimpleFactory.CreateLeiFeng(学雷锋的大学生);studentA.BuyRice();LeiFengstudentB=SimpleFactory.CreateLeiFeng(学雷锋的大学生);studentB.Sweep();LeiFengstudentC=SimpleFactory.CreateLeiFeng(学雷锋的大学生);studentC.Wash();//工厂方法模式IFactoryfactory=newUndergraduateFactory();LeiFengstudent=factory.CreateLeiFeng();student.BuyRice();student.Sweep();student.Wash();Console.Read();}}//雷锋classLeiFeng{publicvoidSweep(){Console.WriteLine(扫地);}publicvoidWash(){Console.WriteLine(洗衣);}publicvoidBuyRice(){Console.WriteLine(买米);}}//学雷锋的大学生8/54classUndergraduate:LeiFeng{}//社区志愿者classVolunteer:LeiFeng{}//简单雷锋工厂classSimpleFactory{publicstaticLeiFengCreateLeiFeng(stringtype){LeiFengresult=null;switch(type){case学雷锋的大学生:result=newUndergraduate();break;case社区志愿者:result=newVolunteer();break;}returnresult;}}//雷锋工厂interfaceIFactory{LeiFengCreateLeiFeng();}//学雷锋的大学生工厂classUndergraduateFactory:IFactory{publicLeiFengCreateLeiFeng(){returnnewUndergraduate();}}//社区志愿者工厂classVolunteerFactory:IFactory{publicLeiFengCreateLeiFeng(){returnnewVolunteer();9/54}}4.Prototype(原型)用原型实例制定创建对象的种类,并且通过复制这些原型创建新的对象。Prototype原型classProgram{staticvoidMain(string[]args){ConcretePrototype1p1=newConcretePrototype1(I);ConcretePrototype1c1=(ConcretePrototype1)p1.Clone();Console.WriteLine(Cloned:{0},c1.Id);ConcretePrototype2p2=newConcretePrototype2(II);ConcretePrototype2c2=(ConcretePrototype2)p2.Clone();Console.WriteLine(Cloned:{0},c2.Id);//WaitforuserConsole.Read();}}abstractclassPrototype{privatestringid;//ConstructorpublicPrototype(stringid)10/54{this.id=id;}//PropertypublicstringId{get{returnid;}}publicabstractPrototypeClone();}classConcretePrototype1:Proto
本文标题:软件设计师23种设计模式总结
链接地址:https://www.777doc.com/doc-5044103 .html