第10章UML与设计模式_第1页
第10章UML与设计模式_第2页
第10章UML与设计模式_第3页
第10章UML与设计模式_第4页
第10章UML与设计模式_第5页
已阅读5页,还剩75页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1UML与设计模式与设计模式2什么是设计模式什么是设计模式 定义:设计模式是从许多优秀的软件系统中总结出成功的可复用的设计方案. 简单地说:设计模式是对某特定环境下某类问题的解决方法。3说明: 设计模式是可重用的设计经验的总结,已在实际的系统中多次得到成功应用。 设计模式一般是针对用面向对象技术的。 设计模式不只是关于孤立的对象的设计,还强调对象之间的通信。 设计模式突出了OO设计中的封装、泛化、多态等概念,学习设计模式,必须清楚这些概念的使用。 设计模式通常都很小,只涉及几个类。4设计模式的历史设计模式的历史 设计模式设计模式的概念最早是由建筑师克里斯托夫亚历山大(Christopher A

2、lexander)提出来的(70年代后期)。 Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. 模式语言(A Pattern Langua

3、ge) Erich Gamma博士论文(1991)的部分工作 开始对设计模式进行分类整理5 E. Gamma, R. Helm, R. Johnson, and J. Vlissides 等四人合著的“Design Patterns: Elements of Object-Oriented Software” Addison-Wesley, 1995 The Gang of Four(GoF) patterns 书的中、英文电子版在ftp上 理解设计模式已成为任何面向对象软件开发人员所必需具备的重要素质之一。6 Design Patterns Elements of Reusable Obje

4、ct-Oriented Software 作者E.GammaR. Helm, R. JohnsonJ. Vlissides四位作者通常被称为四人组(Gang of Four, 或GoF) 7使用设计模式的好处使用设计模式的好处1. Simplify and speed-up design(简化和加快了设计) 无需从底层做起,从设计模式入手可以节省时间,提高开发质量。2. Facilitate communications between designers (方便了设计者之间的通信) 更准确地描述问题和它们的解决方案,使解决方案具有一致性,使代码更容易理解。 开发人员可以在更高的层次上来思考问

5、题和讨论方案。例:“建议用Visitor模式来解决这个问题”3. Reduce risk(降低风险)4. Patterns help ease the transition to object-oriented technology.(有助于转到OO技术)8设计模式的分类设计模式的分类 GoF中共有23个设计模式,这些模式可以按两个准则来分类:1. 按设计模式的按设计模式的purpose来分类来分类:分为创建型模式,结构型模式和行为型模式三种。2. 按设计模式的按设计模式的scope来划分来划分:即根据设计模式是作用于类还是作用于对象,把设计模式分为类设计模式和对象设计模式。9Purpose对

6、象创建型结构型行为型类ScopeFactory MethodAbstract FactoryBuilderPrototypeSingletonAdapterAdapterBridgeCompositeDecoratorFacadeFlyweightProxyInterpreterTemplate MethodChain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategyVisitor设计模式的分类表:设计模式的分类表:10创建型模式创建型模式 创建型模式创建型模式(Creational pattern):Cr

7、eational design patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented. 创建型模式创建型模式抽象了实例化过程。它们帮助一个系统独立于如何创建、组合和表示它的那些对象。 创建型模式包括: Factory Method(工厂方法) Abstract Factory(抽象工厂) Builder(生成器) Prototype(原型) Singleton(单件)11结

8、构型模式结构型模式 结构型模式结构型模式(Structural pattern):Structural patterns are concerned with how classes and objects are composed to form larger structures. 结构型模式结构型模式涉及到如何组合类和对象以获得更大的结构。 结构型模式包括: Adapter(适配器) Bridge(桥接) Composite(组成) Decorator(装饰) Facade(外观) Flyweight(享元) Proxy(代理)12行为型模式行为型模式 行为型模式行为型模式(behavi

9、oral pattern):Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. Behavioral patterns describe not just patterns of objects or classes but also the patterns of communication between them. 行为型模式行为型模式涉及到算法和对象间职责的分配。行为型模式行为型模式不仅描述对象或类的模式,还描述它们之间的通信模

10、式。13 行为型模式包括: Chain of Responsibility(职责链) Command(命令) Interpreter(解释器) Iterator(迭代器) Mediator(中介者) Memento(备忘录) Observer(观察者) State(状态) Strategy(策略) Template method(模板方法) Visitor(访问者)14记录一个设计模式需要有四个基本要素:1.名称 高度概括该模式的本质,有利于该行业统一术语、便于交流使用2.问题 描述应该在何时使用模式,解释设计问题和问题存在的前因后果3.方案 描述设计的组成部分,它们之间的相互关系及各自的职责

11、和协作方式4.效果 使用模式对系统的灵活性、扩充性和复用性的影响15例:中介者模式 名称 中介者 问题 用一个中介者来封闭一系列的对象 交互 方案 中介者接口、具体中介者、同事、具体同事 效果 减少了子类的生成,将各个同事解耦,简化了对象协议,控制集中化16设计模式中使用的两个设计模式中使用的两个OO设计原则设计原则1. Program to an interface and not to an implementation. (针对接口编程,而不是针对实现编程)2. Favor object composition over inheritance. (优先使用对象组合,而不是使用继承)17

12、主要内容主要内容 基本概念基本概念 设计模式实例分析设计模式实例分析 18Facade设计模式设计模式 Facade设计模式设计模式属于对象结构型设计模式。 intent:Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. 为子系统中的一组接口提供一个一致的界面, Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。19外观模式外观模式

13、 外观模式外观模式为系统中的一组接口提供一个一致的界面,为系统中的一组接口提供一个一致的界面,Faade模式模式定义了一个高层接口,这个接口使得这一子系统更加容定义了一个高层接口,这个接口使得这一子系统更加容易使用。易使用。Chain of Responsibility Pattern Provide a unified interface to a set of interfaces in a subsystem.Facade defines a higher-level interface that makes the subsystem easier to use.20子系统的客户端 使

14、用一个子系统的客户端往往只关注一些特定的功能,需要同时与子系统内部的许多对象打交道后才能达到目的。21医院的例子22医院的解决方案23概述概述 2022-6-27 外观模式是简化用户和子系统进行交外观模式是简化用户和子系统进行交互的成熟模式,外观模式的关键是为子互的成熟模式,外观模式的关键是为子系统提供一个称作外观的类,该外观类系统提供一个称作外观的类,该外观类的实例负责和子系统中类的实例打交道。的实例负责和子系统中类的实例打交道。当用户想要和子系统中的若干个类的实当用户想要和子系统中的若干个类的实例打交道时,可以代替地和子系统的外例打交道时,可以代替地和子系统的外观类的实例打交道。观类的实例

15、打交道。 24应用实例应用实例 2022-6-2724 邮政系统负责邮寄包裹的子系统包含邮政系统负责邮寄包裹的子系统包含CheckCheck、WeightWeight和和TransportTransport类。类。CheckCheck类类的实例负责对包裹进行安全检查,的实例负责对包裹进行安全检查,WeightWeight类的实例负责根据包裹的重量计类的实例负责根据包裹的重量计算邮资,算邮资,TransportTransport类的实例负责为包裹类的实例负责为包裹选择运输工具。一个要邮寄的包裹的用选择运输工具。一个要邮寄的包裹的用户如果直接和负责邮寄包裹的子系统的户如果直接和负责邮寄包裹的子系统

16、的类打交道就会非常不方便。类打交道就会非常不方便。 25 引入Facade对象后Client和子系统之间通信关系的变化:client classessubsystem classes26说明: Client通过调用Facade中的方法来和子系统通信。 Client很少直接存取子系统中的对象。 子系统中的对象不需要了解关于Client的知识。 子系统中对象不需要了解关于Facade对象的知识,即没有指向Facade的引用。 引入Facade对象可以降低Client和子系统之间相互依赖关系,降低系统的耦合度,增加了系统的灵活性。27 Facade设计模式中的参与者(Participant)为Fac

17、ade和 subsystem classes :1. Facade: 知道哪些子系统类负责处理请求 将客户请求代理给适当子系统对象 2. subsystem classes 实现子系统功能. 处理Faade对象指派的任务 子类中没有Faade的任何信息,即无对Faade对象的引用。28应用实例应用实例 2022-6-2728 报社的广告系统有三个类报社的广告系统有三个类CheckWordCheckWord、ChargeCharge和和TypeSettingTypeSetting类,各个类的职责类,各个类的职责如下:如下:CheckWordCheckWord类负责检查广告内容含类负责检查广告内容

18、含有的字符数量;有的字符数量;ChargeCharge类的实例负责计类的实例负责计算费用;算费用;TypeSettingTypeSetting的实例负责对广告的实例负责对广告进行排版。使用外观模式简化用户和上进行排版。使用外观模式简化用户和上述子系统所进行的交互。述子系统所进行的交互。292022-6-2729模式的结构的描述与使用模式的结构的描述与使用 1 1子系统(子系统(SubsystemSubsystem)_1_1 : : CheckWord.javaCheckWord.java public class CheckWordpublic class CheckWord public f

19、inal int basicAmount public final int basicAmount=85;=85; String advertisement; String advertisement; int int amount; amount; public CheckWord(String public CheckWord(String advertisement) advertisement) this.advertisement=advertisement; this.advertisement=advertisement; public void setChargeAmount

20、public void setChargeAmount() () amount=advertisement.length()+basicAmount amount=advertisement.length()+basicAmount; /; /计算出计费字符数目计算出计费字符数目 public int getAmount public int getAmount()() return amount; return amount; 302022-6-2730模式的结构的描述与使用模式的结构的描述与使用 1 1子系统(子系统(SubsystemSubsystem)_2_2 : : Charge.j

21、ava Charge.java public class Chargepublic class Charge public final int basicCharge public final int basicCharge=12;=12; CheckWord checkWord CheckWord checkWord; ; Charge(CheckWord checkWord Charge(CheckWord checkWord) this.checkWord=checkWord this.checkWord=checkWord; ; public void giveCharge publi

22、c void giveCharge()() int charge=checkWord.getAmount() int charge=checkWord.getAmount()* *basicChargebasicCharge; ; System.out.println System.out.println(广告费用广告费用:+charge+:+charge+元元);); 312022-6-2731模式的结构的描述与使用模式的结构的描述与使用 1 1子系统(子系统(SubsystemSubsystem)_3_3 : :TypeSeting.javaTypeSeting.java public c

23、lass TypeSetingpublic class TypeSeting String advertisement; String advertisement; public TypeSeting(String public TypeSeting(String advertisement) advertisement) this.advertisement=advertisement; this.advertisement=advertisement; public void typeSeting public void typeSeting() () System.out.println

24、 System.out.println(广告排版格式广告排版格式:);:); System.out.println System.out.println(* * * * * * * * *);); System.out.println(advertisement System.out.println(advertisement);); System.out.println System.out.println(* * * * * * * * *); ); 322022-6-2732模式的结构的描述与使用模式的结构的描述与使用 2 2外观(外观(FacadeFacade): : ClientSe

25、rverFacade.javaClientServerFacade.java public class ClientServerFacadepublic class ClientServerFacade private CheckWord checkWord private CheckWord checkWord; ; private Charge charge; private Charge charge; private TypeSeting typeSeting private TypeSeting typeSeting; ; String advertisement; String a

26、dvertisement; public ClientServerFacade(String public ClientServerFacade(String advertisement) advertisement) this.advertisement=advertisement; this.advertisement=advertisement; checkWord=new CheckWord(advertisement checkWord=new CheckWord(advertisement);); charge=new Charge(checkWord charge=new Cha

27、rge(checkWord);); typeSeting=new TypeSeting(advertisement typeSeting=new TypeSeting(advertisement); ); public void doAdvertisement public void doAdvertisement() () checkWord.setChargeAmount checkWord.setChargeAmount();(); charge.giveCharge charge.giveCharge();(); typeSeting.typeSeting typeSeting.typ

28、eSeting();(); 332022-6-2733模式的结构的描述与使用模式的结构的描述与使用 3 3应用应用 Application.javaApplication.java public class Applicationpublic class Application public static void main(String args public static void main(String args) ClientServerFacade clientFacade ClientServerFacade clientFacade; ; String clientAdverti

29、sement String clientAdvertisement=鹿花牌洗衣机,价格鹿花牌洗衣机,价格23562356元,联系电话:元,联系电话:1234567;1234567; clientFacade=new ClientServerFacade(clientAdvertisement clientFacade=new ClientServerFacade(clientAdvertisement);); clientFacade.doAdvertisement clientFacade.doAdvertisement();(); 34Facade设计模式的适用场合设计模式的适用场合 在下

30、列情况下,可考虑使用Facade设计模式: want to provide a simple interface to a complex subsystem. there are many dependencies between clients and the implementation classes of an abstraction. want to layer your subsystems. 35外观模式的优点外观模式的优点 2022-6-2735使客户和子系统中的类无耦合。使客户和子系统中的类无耦合。外观只是提供了一个更加简洁的界面,并不影外观只是提供了一个更加简洁的界面,并

31、不影响用户直接使用子系统中的类。响用户直接使用子系统中的类。子系统中任何类对其方法的内容进行修改,不子系统中任何类对其方法的内容进行修改,不影响外观的代码。影响外观的代码。36解析文件思考设计一个子系统,该子系统中有三个类: ReadFile类的实例可以读取文本文件 AnalyzeInformation类的实例可以从一个文本中删除用户不需要的内容 SaveFile类的实例能将一个文本保存到文本文件请为上述子系统设计一个外观,以便简化用户和上述子系统所进行的交互。37Abstract Factory设计模式设计模式 Abstract factory属于对象创建型设计模式。 提供一个创建一系列相关

32、或相互依赖对象的接口,而无需指定它们具体的类。38Abstract Factory的适用场合的适用场合 在下列情况下,可考虑使用Abstract Factory设计模式: A system should be independent of how its products are created, composed, and represented. A system should be configured with one of multiple families of products. A family of related product objects is designed to

33、 be used together, and you need to enforce this constraint. You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations. 3940Abstract Factory的一般结构的一般结构41说明: Abstract Factory设计模式中的参与者为 AbstractFactory ConcreteFactory AbstractProduct Concret

34、eProduct Client 客户类仅使用由AbstractFactory和AbstractProduct 声明的接口。42Abstract Factory设计模式的特点设计模式的特点1. Client只通过product接口操作product对象,product对象的具体名字不出现在client中。2. 在应用系统中改变ConcreteFactory的种类很容易。3. 可以保证应用系统在某一时刻只使用一个product系列。4. AbstractFactory接口中已确定了可以创建的product集合,如果要支持新的产品种类需要扩展AbstractFactory及其所有子类中的方法,比较困

35、难。43一一 、 一个实例一个实例 2022-6-2743 建立一个系统,该系统可以为用户提供西服套建立一个系统,该系统可以为用户提供西服套装(上衣裤子)和牛仔套装(上衣裤子)。装(上衣裤子)和牛仔套装(上衣裤子)。442022-6-2744模式的结构的描述与使用模式的结构的描述与使用 1 1抽象产品(抽象产品(ProductProduct) : :UpperClothes.javaUpperClothes.javapublic abstract class UpperClothespublic abstract class UpperClothes public abstract int g

36、etChestSize public abstract int getChestSize();(); public abstract int getHeight public abstract int getHeight();(); public abstract String getName public abstract String getName(); (); Trousers.javaTrousers.javapublic abstract class Trouserspublic abstract class Trousers public abstract int getWais

37、tSize public abstract int getWaistSize();(); public abstract int getHeight public abstract int getHeight();(); public abstract String getName public abstract String getName(); (); 452022-6-2745模式的结构的描述与使用模式的结构的描述与使用 2 2具体产品(具体产品(ConcreteProduct)_1: ConcreteProduct)_1: WesternUpperClothes.javaWestern

38、UpperClothes.java public class WesternUpperClothes extends UpperClothespublic class WesternUpperClothes extends UpperClothes private int chestSize private int chestSize; ; private int private int height; height; private String name; private String name; WesternUpperClothes(String name,int chestSize,

39、int WesternUpperClothes(String name,int chestSize,int height) height) =name; =name; this.chestSize=chestSize this.chestSize=chestSize; ; this.height=height; this.height=height; public int getChestSize public int getChestSize()() return chestSize return chestSize; ; public int getHe

40、ight public int getHeight()() return height; return height; public String getName public String getName()() return name; return name; 462022-6-2746模式的结构的描述与使用模式的结构的描述与使用 2 2具体产品(具体产品(ConcreteProduct)_2: ConcreteProduct)_2: CowboyUpperClothes.javaCowboyUpperClothes.java public class CowboyUpperClothe

41、s extends UpperClothespublic class CowboyUpperClothes extends UpperClothes private int chestSize private int chestSize; ; private int private int height; height; private String name; private String name; CowboyUpperClothes(String name,int chestSize,int CowboyUpperClothes(String name,int chestSize,in

42、t height) height) =name; =name; this.chestSize=chestSize this.chestSize=chestSize; ; this.height=height; this.height=height; public int getChestSize public int getChestSize()() return chestSize return chestSize; ; public int getHeight public int getHeight()() return height; return

43、height; public String getName public String getName()() return name; return name; 472022-6-2747模式的结构的描述与使用模式的结构的描述与使用 2 2具体产品(具体产品(ConcreteProduct)_3: ConcreteProduct)_3: WesternTrousers.javaWesternTrousers.java public class WesternTrouserspublic class WesternTrousers extends Trousers extends Trouse

44、rs private int waistSize private int waistSize; ; private int private int height; height; private String name; private String name; WesternTrousers(String name,int waistSize,int WesternTrousers(String name,int waistSize,int height) height) =name; =name; this.waistSize=waistSize thi

45、s.waistSize=waistSize; ; this.height=height; this.height=height; public int getWaistSize public int getWaistSize()() return waistSize return waistSize; ; public int getHeight public int getHeight()() return height; return height; public String getName public String getName()() return name; return na

46、me; 482022-6-2748模式的结构的描述与使用模式的结构的描述与使用 2 2具体产品(具体产品(ConcreteProduct)_4: ConcreteProduct)_4: CowboyTrousers.javaCowboyTrousers.java public class CowboyTrouserspublic class CowboyTrousers extends Trousers extends Trousers private int waistSize private int waistSize; ; private int private int height;

47、height; private String name; private String name; CowboyTrousers(String name,int waistSize,int CowboyTrousers(String name,int waistSize,int height) height) =name; =name; this.waistSize=waistSize this.waistSize=waistSize; ; this.height=height; this.height=height; public int getWaist

48、Size public int getWaistSize()() return waistSize return waistSize; ; public int getHeight public int getHeight()() return height; return height; public String getName public String getName()() return name; return name; 492022-6-2749模式的结构的描述与使用模式的结构的描述与使用 3 3抽象工厂(抽象工厂(AbstractFactoryAbstractFactory)

49、: :ClothesFactory.javaClothesFactory.java public abstract class ClothesFactorypublic abstract class ClothesFactory public abstract UpperClothes createUpperClothes(int chestSize,int public abstract UpperClothes createUpperClothes(int chestSize,int height); height); public abstract Trousers createTrou

50、sers(int waistSize,int public abstract Trousers createTrousers(int waistSize,int height); height); 502022-6-2750模式的结构的描述与使用模式的结构的描述与使用 4 4具体工厂(具体工厂(ConcreteFactoryConcreteFactory): :BeijingClothesFactory.javaBeijingClothesFactory.javapublic class BeijingClothesFactory extends ClothesFactorypublic cl

51、ass BeijingClothesFactory extends ClothesFactory public UpperClothes createUpperClothes(int chestSize,int public UpperClothes createUpperClothes(int chestSize,int height) height) return new WesternUpperClothes return new WesternUpperClothes(北京牌西服上衣北京牌西服上衣,chestSize,height,chestSize,height);); public

52、 Trousers createTrousers(int waistSize,int public Trousers createTrousers(int waistSize,int height) height) return new WesternTrousers return new WesternTrousers(北京牌西服裤子北京牌西服裤子,waistSize,height,waistSize,height);); ShanghaiClothesFactory.javaShanghaiClothesFactory.javapublic class ShanghaiClothesFac

53、tory extends ClothesFactorypublic class ShanghaiClothesFactory extends ClothesFactory public UpperClothes createUpperClothes(int chestSize,int public UpperClothes createUpperClothes(int chestSize,int height) height) return new WesternUpperClothes return new WesternUpperClothes(上海牌牛仔上衣上海牌牛仔上衣,chestSi

54、ze,height,chestSize,height);); public Trousers createTrousers(int waistSize,int public Trousers createTrousers(int waistSize,int height) height) return new WesternTrousers return new WesternTrousers(上海牌牛仔裤上海牌牛仔裤,waistSize,height,waistSize,height);); 512022-6-2751模式的结构的描述与使用模式的结构的描述与使用 5 5应用应用_1: _1:

55、 Shop.javaShop.java public class Shoppublic class Shop UpperClothes UpperClothes cloth; cloth; Trousers trouser; Trousers trouser; public void giveSuit(ClothesFactory factory,int chestSize,int waistSize,int public void giveSuit(ClothesFactory factory,int chestSize,int waistSize,int height) height) c

56、loth=factory.createUpperClothes(chestSize,height cloth=factory.createUpperClothes(chestSize,height);); trouser=factory.createTrousers(waistSize,height trouser=factory.createTrousers(waistSize,height);); showMess showMess();(); private void showMess private void showMess()() System.out.println System

57、.out.println();); System.out.println(cloth.getName System.out.println(cloth.getName()+:);()+:); System.out.print( System.out.print(胸围胸围:+cloth.getChestSize:+cloth.getChestSize();(); System.out.println System.out.println(身高身高:+cloth.getHeight:+cloth.getHeight();(); System.out.println(trouser.getName

58、System.out.println(trouser.getName()+:);()+:); System.out.print( System.out.print(腰围腰围:+trouser.getWaistSize:+trouser.getWaistSize();(); System.out.println System.out.println(身高身高:+trouser.getHeight:+trouser.getHeight();(); 522022-6-2752模式的结构的描述与使用模式的结构的描述与使用 5 5应用应用_2: _2: Application.javaApplicati

59、on.java public class Applicationpublic class Application public static void main(String args public static void main(String args) Shop shop=new Shop(); Shop shop=new Shop(); ClothesFactory factory=new BeijingClothesFactory ClothesFactory factory=new BeijingClothesFactory(); (); shop.giveSuit(factory

60、,110,82,170); shop.giveSuit(factory,110,82,170); factory=new ShanghaiClothesFactory factory=new ShanghaiClothesFactory(); (); shop.giveSuit(factory,120,88,180); shop.giveSuit(factory,120,88,180); 53观察者模式观察者模式 2022-6-2753观察者模式(别名:依赖,发布观察者模式(别名:依赖,发布-订阅)订阅) 定义对象间的一种一对多的依赖关系,当一个对象的定义对象间的一种一对多的依赖关系,当一个对

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论