软件设计师23种设计模式总结.docx
《软件设计师23种设计模式总结.docx》由会员分享,可在线阅读,更多相关《软件设计师23种设计模式总结.docx(80页珍藏版)》请在冰豆网上搜索。
![软件设计师23种设计模式总结.docx](https://file1.bdocx.com/fileroot1/2023-7/11/bf550b37-70e7-4188-8024-4e485e6a3551/bf550b37-70e7-4188-8024-4e485e6a35511.gif)
软件设计师23种设计模式总结
创建型
结构型
行为型
类
FactoryMethod
Adapter
Interpreter
TemplateMethod
对象
AbstractFactory
Builder
Prototype
Singleton
Apapter(对象)
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
ChainofResponsibility
Command
Iterator
Mediator
Memento
Observer
State
Strategy
Visitor
1.AbstractFactory(抽象工厂)
提供一个创建一系列相关或互相依赖对象的接口,而无须制定它们具体的类。
AbstractFactory抽象工厂
classProgram
{
staticvoidMain(string[]args)
{
AbstractFactoryfactory1=newConcreteFactory1();
Clientc1=newClient(factory1);
c1.Run();
AbstractFactoryfactory2=newConcreteFactory2();
Clientc2=newClient(factory2);
c2.Run();
Console.Read();
}
}
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)
{
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;
//Constructor
publicClient(AbstractFactoryfactory)
{
AbstractProductB=factory.CreateProductB();
AbstractProductA=factory.CreateProductA();
}
publicvoidRun()
{
AbstractProductB.Interact(AbstractProductA);
}
}
2.Builder(生成器)
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
Builder生成器
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();
publicabstractvoidBuildPartB();
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
{
IListparts=newList();
publicvoidAdd(stringpart)
{
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();
LeiFengstudent2=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("买米");
}
}
//学雷锋的大学生
classUndergraduate:
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();
}
}
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);
//Waitforuser
Console.Read();
}
}
abstractclassPrototype
{
privatestringid;
//Constructor
publicPrototype(stringid)
{
this.id=id;
}
//Property
publicstringId
{
get{returnid;}
}
publicabstractPrototypeClone();
}
classConcretePrototype1:
Prototype
{
//Constructor
publicConcretePrototype1(stringid)
:
base(id)
{
}
publicoverridePrototypeClone()
{
//Shallowcopy
return(Prototype)this.MemberwiseClone();
}
}
classConcretePrototype2:
Prototype
{
//Constructor
publicConcretePrototype2(stringid)
:
base(id)
{
}
publicoverridePrototypeClone()
{
//Shallowcopy
return(Prototype)this.MemberwiseClone();
}
}
5.Singleton(单例)
保证一个类仅有一个实例,并提供一个访问它的全局访问点。
Singleton
classProgram
{
staticvoidMain(string[]args)
{
Singletons1=Singleton.GetInstance();
Singletons2=Singleton.GetInstance();
if(s1==s2)
{
Console.WriteLine("Objectsarethesameinstance");
}
Console.Read();
}
}
classSingleton
{
privatestaticSingletoninstance;
privatestaticreadonlyobjectsyncRoot=newobject();
privateSingleton()
{
}
publicstaticSingletonGetInstance()
{
if(instance==null)
{
lock(syncRoot)
{
if(instance==null)
{
instance=newSingleton();
}
}
}
returninstance;
}
}
1.Adapter(适配器)
将一个类的接口转换成客户希望的另一个接口。
Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
Adapter
classProgram
{
staticvoidMain(string[]args)
{
Targettarget=newAdapter();
target.Request();
Console.Read();
}
}
classTarget
{
publicvirtualvoidRequest()
{
Console.WriteLine("普通请求");
}
}
classAdaptee
{
publicvoidSpecificRequest()
{
Console.WriteLine("特殊请求");
}
}
classAdapter:
Target
{
privateAdapteeadaptee=newAdaptee();
publicoverridevoidRequest()
{
adaptee.SpecificRequest();
}
}
2.Bridge(桥接)
将抽象部分与其实现部分分离,使它们都可以独立的变化。
Bridge
classProgram
{
staticvoidMain(string[]args)
{
Abstractionab=newRefinedAbstraction();
ab.SetImplementor(newConcreteImplementorA());
ab.Operation();
ab.SetImplementor(newConcreteImplementorB());
ab.Operation();
Console.Read();
}
}
classAbstraction
{
protectedImplementorimplementor;
publicvoidSetImplementor(Implementorimplementor)
{
this.implementor=implementor;
}
publicvirtualvoidOperation()
{
implementor.Operation();
}
}
classRefinedAbstraction:
Abstraction
{
publicoverridevoidOperation()
{
implementor.Operation();
}
}
abstractclassImplementor
{
publicabstractvoidOperation();
}
classConcreteImplementorA:
Implementor
{
publicoverridevoidOperation()
{
Console.WriteLine("具体实现A的方法执行");
}
}
classConcreteImplementorB:
Implementor
{
publicoverridevoidOperation()
{
Console.WriteLine("具体实现B的方法执行");
}
}
3.Composite(组合)
将对象组合成树形结构以表示“部分----整体”的层次结构。
Composite使得用户对单个对象和组合对象的使用具有一致性。
Composite
classProgram
{
staticvoidMain(string[]args)
{
Compositeroot=newComposite("root");
root.Add(newLeaf("LeafA"));
root.Add(newLeaf("LeafB"));
Compositecomp=newComposite("CompositeX");
comp.Add(newLeaf("LeafXA"));
comp.Add(newLeaf("LeafXB"));
root.Add(comp);
Compositecomp2=newComposite("CompositeXY");
comp2.Add(newLeaf("LeafXYA"));
comp2.Add(newLeaf("LeafXYB"));
comp.Add(comp2);
root.Add(newLeaf("LeafC"));
Leafleaf=newLeaf("LeafD");
root.Add(leaf);
root.Remove(leaf);
root.Display
(1);
Console.Read();
}
}
abstractclassComponent
{
protectedstringname;
publicComponent(stringname)
{
this.name=name;
}
publicabstractvoidAdd(Componentc);
publicabstractvoidRemove(Componentc);
publicabstractvoidDisplay(intdepth);
}
classComposite:
Component
{
privateListchildren=newList();
publicComposite(stringn