软件设计师23种设计模式总结文档格式.docx

上传人:b****1 文档编号:456933 上传时间:2023-04-28 格式:DOCX 页数:68 大小:1.44MB
下载 相关 举报
软件设计师23种设计模式总结文档格式.docx_第1页
第1页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第2页
第2页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第3页
第3页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第4页
第4页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第5页
第5页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第6页
第6页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第7页
第7页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第8页
第8页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第9页
第9页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第10页
第10页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第11页
第11页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第12页
第12页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第13页
第13页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第14页
第14页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第15页
第15页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第16页
第16页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第17页
第17页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第18页
第18页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第19页
第19页 / 共68页
软件设计师23种设计模式总结文档格式.docx_第20页
第20页 / 共68页
亲,该文档总共68页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

软件设计师23种设计模式总结文档格式.docx

《软件设计师23种设计模式总结文档格式.docx》由会员分享,可在线阅读,更多相关《软件设计师23种设计模式总结文档格式.docx(68页珍藏版)》请在冰点文库上搜索。

软件设计师23种设计模式总结文档格式.docx

c2.Run();

Console・Read();

abstractclassAbstractFactory

publicabstractAbstractProductACreateProductA();

publicabstractAbstractProductB

CreateProductBO;

'

classConcreteFactoryl:

AbstractFactorv

publicoverrideAbstractProductACreatePtoductA()

returnnewProductAl();

}

publicoverrideAbstractProductBCreateProductB()

returnnewProductBl();

classConcreteFactory2:

AbstractFactory

publicoverrideAbstractProductACreateProductA()

returnnewProductA2();

returnnewProductB2();

abstractclassAbstractProductAabstractclassAbstractProductB

publicabstractvoidInteract(AbstractProductAa);

classProductAl:

AbstractPtoductAclassProductBl:

AbstractProductB

publicoverridevoidInteract(AbstractProductAa)

Console・WriteLine(this・GetType()・Name+

,zinteractswith"

+a.GetType().Name);

classProductA2:

AbstractProductA

classProductB2:

publicoverridevoidInteract(AbstractProductAa)(

Console・WriteLine(this・GetType().Name+〃interactswith"

+&

GetType().Name):

classClient

privateAbstractProductAAbstractProductA;

privateAbstractProductBAbstractPtoductB;

//Constructor

publicClient(AbstractFactoryfactory)

AbstractProductB二factory.CreateProductBO;

AbstractProductA二factory.CreateProductA();

publicvoidRun()

AbstractProductB.Interact(AbstractProductA);

2.Builder(生成器)将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

Directo

r

Construct!

TC

forallobjectsinstructure

buildcr^BuildPartO

builder

O'

 

图10-26生成器模式结构图

Builder生成器

Directordirector二newDirector。

Builderbl=newConcreteBuilderl();

Builderb2二newConereteBuilder2();

director.Construet(b2);

Productp2=b2.GetResult();

p2.Show():

Console.Read();

classDirector

publicvoidConstruct(Builderbuilder){builder.BuildPartA();

builder.BuildPartB();

}abstractclassBuilder

publicabstractvoidBuildPartA();

publicabstractvoidBu订dPartB();

publicabstractProductGetResult();

classConcreteBuilderl:

Builder

privateProductproduct二newProduct();

publicoverridevoidBuildPartA()

product.AddC'

部件A"

);

publicoverridevoidBuildPartB()

product.Add(z/部件B"

publicoverrideProductGetResult()

returnproduct;

classConcreteBuilder2:

publicoverridevoidBuildPartA()

product.Add(z/部件X"

部件Y"

classProduct

IList〈string>

parts二newList〈string>

();

publicvoidAdd(stringpart)

parts.Add(part);

publicvoidShow()

Console.WriteLineC\n产品仓U建“);

foreach(stringpart

inparts)

Console.WriteLine(part);

3.FactoryMethod(工厂方法)

定义一个用于创建对象的接口,让子类决定实例化哪一个类。

FactoryMethod使一个实例化延迟到其子类。

图10-27工厂方法模式结构图

FactoryMethodI]厂方法

//

//基本方式:

薛磊风代表大学生学习雷锋

LeiFengxueleifeng二newUndergraduate();

xueleifeng.BuyRiceO;

xueleifeng.Sweep();

xueleifeng.WashO;

LeiFengstudentl二newUndergraduate();

studentl・BuyRice();

LeiFengstudent2二newUndergraduate();

student2.Sweep();

LeiFengstudent3二newUndergraduate();

student3・Wash();

//简单工厂模式

LeiFengstudentA=SimpleFactory.CreateLeiFengC学雷锋的大学生"

);

studentA・BuyRice();

LeiFengstudentB=SimpleFactory.CreateLeiFengC学雷锋的大学生”);

studentB.Sweep();

LeiFengstudentC二SimpleFactory.CreateLeiFeng(学雷锋的大学生"

studentC.WashO;

//工厂方法模式

IFactoryfactory二newUndergraduateFactory();

LeiFengstudent二factory.CreateLeiFeng();

student.BuyRice();

student.Sweep();

student.WashO;

Console.Read();

//雷锋

classLeiFeng

publicvoidSweep()

Console.WriteLine(”扫地“);

publicvoidWashO

Console.WriteLine(”洗衣”);

publicvoidBuyRice()

Console.WriteLine(”买米”);

//学雷锋的大学生

classUndergraduate:

LeiFeng

{}//社区志愿者classVolunteer:

{}

//简单雷锋工厂

classSimpleFactory

publicstaticLeiFengCreateLeiFeng(stringtype)

LeiFengresuIt二null;

switch(type)

case〃学雷锋的大学生〃:

resuIt二newUndergraduate():

break;

case〃社区志愿者〃:

resuIt二newVolunteer():

returnresuIt;

//雷锋工厂

interfaceIFactory

LeiFengCreateLeiFengO;

//学雷锋的大学生工厂

classUndergraduateFactory:

IFactory

publicLeiFengCreateLeiFengO

returnnewUndergraduate();

//社区志愿者工厂

classVolunteerFactory:

returnnewVolunteer();

4.Prototype(原型)用原型实例制定创建对象的种类,并且通过复制这些原型创建新的对象。

5.

图10-28原型模式结构图

Prototype原型

ConeretePtototypelpl二newConeretePrototypel("

l"

ConeretePrototypelcl二(ConeretePrototypel)p1.CIone();

Console.WriteLine("

Cloned:

{0}"

cl.Id);

ConeretePtototype2p2二newConeretePrototype2(〃II〃);

ConcretePrototype2c2二(ConeretePrototype2)p2・Clone();

c2.Id);

//Waitforuser

Console・ReadO;

abstractclassPrototype{

privatestringid;

//Construetor

publicPrototype(stringid){

this.id二id;

}//PropertypublicstringId

get{:

returnid;

}

publicabstractPrototypeClone();

classConcretePrototypel:

Prototype

//Construetor

publicConcretePrototypel(stringid)

:

base(id)

publicoverridePrototypeClone()

//Shallowcopy

return(Prototype)this・MemberwiseClone();

classConcretePrototype2:

publicConcretePrototype2(stringid)

base(id)

6.Singleton(单例)保证一个类仅有一个实例,并提供一个访问它的全局访问点。

Singleton

returnuniqueinstance

staticInstance()0

SingletonOperationO

GetSingletonDataO

图10-29单例模式结构I

staticuniqueinstancesingletonData

Singleton

Singletonsi二Singleton.Getlnstanee():

Singletons2二Singleton.Getlnstanee();

if(si二二s2)

Console.WriteLine("

Objectsarethesameinstanee"

}classSingleton

privatestaticSingletoninstanee;

privatestaticreadonlyobjectsyncRoot二newobject();

privateSingleton()

}publicstaticSingletonGetlnstanee(){

if(instanee二二nuII)

lock(syncRoot){

if(instanee二二null)

instanee二newSingleton();

returninstanee;

将一个类的接口转换成客户希望的另一个接口。

接口不兼容而不能一起工作的那

1.Adapter(适配器)

Adapter模式使得原木由于

图10-30娄适配器结构图

Targettarget二newAdapter();

target・Request();

Console.ReadO;

classTarget

publicvirtualvoidRequest()

Console.WriteLineC普通请求〃);

classAdaptee

publicvoidSpecificRequest()

Console.WriteLine(z,特殊请求〃);

classAdapter:

Target

privateAdapteeadaptee二newAdaptee();

publicoverridevoidRequest()

adaptee.SpecificRequest();

2.Bridge(桥接)

将抽象部分与其实现部分分离,使它们都可以独立的变化。

图10-32桥接模式結构图

Bridge

staticvoidMain(stringL]args)

Abstractionab二newRefinedAbstraction();

ab.Setlmplementor(newConcretelmplementorA());

ab.Operation();

ab.Setlmplementor(newConcretelmplementorB());

ab.Operation();

classAbstraction

protectedImplementorimplementor;

publicvoidSetlmplementor(Implementorimplementor)this・implementor二implementor;

publicvirtualvoidOperation()

implementor.Operation();

classRefinedAbstraction:

Abstraction{

publicoverridevoidOperation()implementor.Operation();

abstractclassImplementor

publicabstractvoidOperation();

classConcretelmplementorA:

Implementor

publicoverridevoidOperation()

Console.WriteLineC'

具体实现A的方法执行“);

classConcretelmplementorB:

Console.WriteLineC具体实现B的方法执行“);

3.Composite(组合)

“部分----整体”的层次结构。

Composite使

图J0-33组合模式结构图

Composite

Composite:

root二newComposite("

root"

root.Add(newLeaf("

LeafA"

));

LeafB〃));

Compositecomp二newComposite("

CompositeX"

comp.Add(newLeafC'

LeafXA"

comp.Add(newLeaf("

LeafXB"

root.Add(comp);

Compositecomp2二newComposite("

CompositeXY"

comp2.Add(newLeaf("

LeafXYA"

LeafXYB"

comp.Add(comp2);

LeafC"

Leafleaf二newLeaf("

LeafD"

root.Add(leaf);

root.Remove(leaf);

root.Display

(1);

abstractclassComponent

protectedstringname;

publicComponent(stringname)

this.name二name;

publicabstractvoidAdd(Componentc):

publicabstractvoidRemove(Componentc);

publicabstractvoidDisplay(intdepth);

classComposite:

Component

privateList〈Component〉children二newList〈Component〉();

publicComposite(stringname)

base(name)

publicoverridevoidAdd(Componentc)children.Add(c);

publicoverridevoidRemove(Componentc)

'

children.Remove(c);

publicoverridevoidDisplay(intdepth)

Console.WriteLine(newString(J」,depth)+name);

foreach(Co

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2