各种设计模式的类图和使用规则.docx

上传人:b****8 文档编号:9498133 上传时间:2023-05-19 格式:DOCX 页数:17 大小:267.61KB
下载 相关 举报
各种设计模式的类图和使用规则.docx_第1页
第1页 / 共17页
各种设计模式的类图和使用规则.docx_第2页
第2页 / 共17页
各种设计模式的类图和使用规则.docx_第3页
第3页 / 共17页
各种设计模式的类图和使用规则.docx_第4页
第4页 / 共17页
各种设计模式的类图和使用规则.docx_第5页
第5页 / 共17页
各种设计模式的类图和使用规则.docx_第6页
第6页 / 共17页
各种设计模式的类图和使用规则.docx_第7页
第7页 / 共17页
各种设计模式的类图和使用规则.docx_第8页
第8页 / 共17页
各种设计模式的类图和使用规则.docx_第9页
第9页 / 共17页
各种设计模式的类图和使用规则.docx_第10页
第10页 / 共17页
各种设计模式的类图和使用规则.docx_第11页
第11页 / 共17页
各种设计模式的类图和使用规则.docx_第12页
第12页 / 共17页
各种设计模式的类图和使用规则.docx_第13页
第13页 / 共17页
各种设计模式的类图和使用规则.docx_第14页
第14页 / 共17页
各种设计模式的类图和使用规则.docx_第15页
第15页 / 共17页
各种设计模式的类图和使用规则.docx_第16页
第16页 / 共17页
各种设计模式的类图和使用规则.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

各种设计模式的类图和使用规则.docx

《各种设计模式的类图和使用规则.docx》由会员分享,可在线阅读,更多相关《各种设计模式的类图和使用规则.docx(17页珍藏版)》请在冰点文库上搜索。

各种设计模式的类图和使用规则.docx

各种设计模式的类图和使用规则

简单工厂模式

类图:

publicclassCreator

{

publicProductfactory()

{

returnnewConcreteProduct();

}

}

publicinterfaceProduct

{

}

publicclassConcreteProductimplementsProduct

{

publicConcreteProduct(){}

}

工厂方法模式:

简单工厂模式和工厂方法模式的区别:

工厂方法模式的核心是一个抽象工厂类,而不像简单工厂模式,把核心放在一个具体类上。

工厂方法模式可以允许很多具体的工厂类从抽象工厂类继承下来,从而可以在实际上成为多个简单工厂模式的综合,从而推广了简单工厂模式。

反过来讲,简单工厂模式是由工厂方法模式退化而来。

设想如果我们非常确定一个系统只需要一个具体的工厂类,那么就不妨把抽象工厂类合并到具体的工厂类中去。

而这样一来,我们就退化到简单工厂模式了。

工厂方法模式应该在什么情况下使用:

一般来说,如果你的系统不能事先确定那一个产品类在哪一个时刻被实例化,从而需要将实例化的细节局域化,并封装起来以分割实例化及使用实例的责任时,你就需要考虑使用某一种形式的工厂模式。

在我们的小花果园系统里,我们必须假设水果的种类随时都有可能变化。

我们必须能够在引入新的水果品种时,能够很少改动程序,就可以适应变化以后的情况。

因此,我们显然需要某一种形式的工厂模式。

如果在发现系统只用一个产品类层次(hierarchy)就可以描述所有已有的产品类,以及可预见的未来可能引进的产品类时,简单工厂模式是很好的解决方案。

因为一个单一产品类等级只需要一个单一的实的工厂类。

然而,当发现系统只用一个产品类层次不足以描述所有的产品类,包括以后可能要添加的新的产品类时,就应当考虑采用工厂方法模式。

由于工厂方法模式可以容许多个具体的工厂类,以每一个工厂类负责每一个产品类等级,因此这种模式可以容纳所有的产品等级。

在我们的小花果园系统里,不只有水果种类的植物,而且有蔬菜种类的植物。

换言之,存在不止一个产品类层次。

而且产品类层次的数目也随时都有可能变化。

因此,简单工厂模式不能满足需要,为解决向题,我们显然需要工厂方法模式。

抽象工厂模式:

在什么情形下应当使用抽象工厂模式

首先,一个系统应当不依赖于产品类实例被创立,组成,和表示的细节。

这对于所有形态的工厂模式都是重要的。

其次,这个系统的产品有多于一个的产品族。

第三,同属于同一个产品族的产品是设计成在一起使用的。

这一约束必须得在系统的设计中体现出来。

最后,不同的产品以一系列的接口的面貌出现,从而使系统不依赖于接口实现的细节。

单例模式

饿汉模式

packagecom.javapatterns.singleton.demos;

publicclassEagerSingleton{

privatestaticfinalEagerSingletonm_instance=newEagerSingleton();

privateEagerSingleton(){}

publicstaticEagerSingletongetInstance(){

returnm_instance;

}

}

懒汉式单态模式:

ADAPTER模式

在下列情况中使用Adapter模式:

(1)如果想使用一个已经存在的类,而它的接口不符合要求。

(2)如果想创建一个可以复用得的类,该类可以与其它不相关的类或不可预见的类协同工作。

(3)对于对象适配器,如果想使用一些已经存在的子类,但是不能对每个都进行子类化以匹配他们的接口。

对象适配器可以适配它的超类接口。

classAdaptee{

publicvoidSpecificRequest(){}

}

interfaceTarget{

publicvoidRequest();

}

classAdapterimplementsTarget{

Adapteeadaptee;

publicAdapter(Adapteeee){

adaptee=ee;

}

publicvoidRequest(){

//Implementbehaviorusing

//methodsinAdaptee:

adaptee.SpecificRequest();

}

}

classWhatIUse{

publicvoidop(Targettarget){

target.Request();

}

}

publicclassUseAdapter{

WhatIUsewhatIUse=newWhatIUse();

Adapteeadaptee=newAdaptee();

Adapteradapt=newAdapter(adaptee);

publicvoidtest(){

whatIUse.op(adapt);

}

publicstaticvoidmain(Stringargs[]){

newUseAdapter().test();

}

}///:

~

Decorator模式

PROXY模式

interfaceSubject{

voidf();

voidg();

voidh();

}

classProxyimplementsSubject{

privateSubjectrealSubject;

publicProxy(){

realSubject=newRealSubject();

}

//PassmethodcallstotherealSubject:

publicvoidf(){realSubject.f();}

publicvoidg(){realSubject.g();}

publicvoidh(){realSubject.h();}

}

classRealSubjectimplementsSubject{

publicvoidf(){

System.out.println("RealSubject.f()");

}

publicvoidg(){

System.out.println("RealSubject.g()");

}

publicvoidh(){

System.out.println("RealSubject.h()");

}

}

publicclassProxyDemo{

Proxyp=newProxy();

publicvoidtest(){

//Thisjustmakessureitwillcomplete

//withoutthrowinganexception.

p.f();

p.g();

p.h();

}

publicstaticvoidmain(Stringargs[]){

newProxyDemo().test();

}

}///:

~

COMMAND模式

mportjava.util.*;

importcom.bruceeckel.test.*;

interfaceCommand{

voidexecute();

}

classHelloimplementsCommand{

publicvoidexecute(){

System.out.print("Hello");

}

}

classWorldimplementsCommand{

publicvoidexecute(){

System.out.print("World!

");

}

}

classIAmimplementsCommand{

publicvoidexecute(){

System.out.print("I'mthecommandpattern!

");

}

}

//ACommandobjectthatholdscommands:

classMacro{

privateArrayListcommands=newArrayList();

publicvoidadd(Commandc){commands.add(c);}

publicvoidrun(){

Iteratorit=commands.iterator();

while(it.hasNext())

((Command)it.next()).execute();

}

}

publicclassCommandPatternextendsUnitTest{

Macromacro=newMacro();

publicvoidtest(){

macro.add(newHello());

macro.add(newWorld());

macro.add(newIAm());

macro.run();

}

publicstaticvoidmain(Stringargs[]){

newCommandPattern().test();

}

}///:

~

OBSERVER模式

ClassObserver{

ProtectedSubjectsubject;

Abstractvoidupdate();

Voidsetsubject(Subjectpsubject){subject=psubject;}

}

classSubject{

Listobservers=newArraylist();

Public:

Voidattach(Observerpobserver){observers.add(pobserver);}

Voiddetach(Observerpobserver){observers.remove(pobserver);}

Voidnotify(){

Iteratorit=observers.iterator();

While(it.hasnext())

{

it.update();

it.next();

}

}

classClockTimerextendsSubject{

intgetHour();

intgetMiniute();

intgetsecond();

voidtick(){notify();}

}

classDigitalClockextendsObserver{

ClockTimersubject;

Public:

DigitalClock(ClockTimerct){

Subject=ct;

Subject.attach(this);

}

voidupdate(){

System.out.println(subject.getHour());

System.out.println(subject.getMinite());

System.out.println(subject.getSecond()):

}

}

STRATEGY模式

importcom.bruceeckel.util.*;//Arrays2.print()

importcom.bruceeckel.test.*;

//Thestrategyinterface:

interfaceFindMinima{

//Lineisasequenceofpoints:

double[]algorithm(double[]line);

}

//Thevariousstrategies:

classLeastSquaresimplementsFindMinima{

publicdouble[]algorithm(double[]line){

returnnewdouble[]{1.1,2.2};//Dummy

}

}

classPerturbationimplementsFindMinima{

publicdouble[]algorithm(double[]line){

returnnewdouble[]{3.3,4.4};//Dummy

}

}

classBisectionimplementsFindMinima{

publicdouble[]algorithm(double[]line){

returnnewdouble[]{5.5,6.6};//Dummy

}

}

//The"Context"controlsthestrategy:

classMinimaSolver{

privateFindMinimastrategy;

publicMinimaSolver(FindMinimastrat){

strategy=strat;

}

double[]minima(double[]line){

returnstrategy.algorithm(line);

}

voidchangeAlgorithm(FindMinimanewAlgorithm){

strategy=newAlgorithm;

}

}

publicclassStrategyPattern{

MinimaSolversolver=

newMinimaSolver(newLeastSquares());

double[]line={

1.0,2.0,1.0,2.0,-1.0,

3.0,4.0,5.0,4.0};

publicvoidtest(){

double[]arrays;

arrays=solver.minima(line);

solver.changeAlgorithm(newBisection());

Arrays=solver.minima(line);

}

publicstaticvoidmain(Stringargs[]){

newStrategyPattern().test();

}

}///:

~

 

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

当前位置:首页 > 总结汇报 > 学习总结

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

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