9设计模型创建设计类图1方案_第1页
9设计模型创建设计类图1方案_第2页
9设计模型创建设计类图1方案_第3页
9设计模型创建设计类图1方案_第4页
9设计模型创建设计类图1方案_第5页
已阅读5页,还剩63页未读 继续免费阅读

下载本文档

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

文档简介

1、9 设计模型:设计模型:创建设计类图(创建设计类图(dcd)目标:创建设计类图(design class diagrams)识别dcd中的类、方法和关联.设计类图设计类图uml用类图表示类、接口及其关联静态对象建模常用类图表示法常用类图表示法java.awt:fontorjava.awt.fontplain : int = 0 readonly bold : int = 1 readonly name : stringstyle : int = 0.getfont(name : string) : fontgetname() : serfacerunnablerun()- e

2、llipsis “” means there may be elements, but not shown- a blank compartment officially means “unknown” but as a convention will be used to mean “no members”subclassfoo.run().superclassfooorsuperclassfoo abstract - classorstaticattribute : int+ publicattribute : string- privateattributeassumedprivatea

3、ttributeisinitializedattribute : bool = trueacollection : veggieburger * attributemaylegallybenull : string 0.1 finalconstantattribute : int = 5 readonly /derivedattribute + classorstaticmethod()+ publicmethod()assumedpublicmethod()- privatemethod()# protectedmethod() packagevisiblemethod()construct

4、or superclassfoo( long )methodwithparms(parm1 : string, parm2 : float)methodreturnssomething() : veggieburgermethodthrowsexception() exception ioexceptionabstractmethod()abstractmethod2() abstract / alternatefinalmethod() leaf / no override in subclasssynchronizedmethod() guarded 3 common compartmen

5、ts1. classifier name2. attributes3. operationsinterface implementation andsubclassingfruit.purchaseorder.1association with multiplicitiesdependency officially in uml, the top format is used to distinguish the package name from the class nameunofficially, the second alternative is commonorderan inter

6、face shown with a keyword uml的正式表示法,最上面的格式用来区分包名和类名;第二种非正式格式三个常见的分栏:1.类元名称2.属性3.操作用关键字interfce表示接口interfcerunnblerun()接口的实现和子类化依赖具有多重性的关联省略号表示这里具有元素,但是没有显示空分栏的正式含义是“未知”,但也用于表示“没有”设计类图样例设计类图样例postenteritem() sledteiscomplete:boolentimemkelineitem()11cptures导航类的定义的三个区域框方法,有参数,但没有制定类型信息 领域模型和设计模型中的类比较领

7、域模型和设计模型中的类比较与领域模型不同的是,设计类图显示出了软件实体的定义而不是真实世界中的概念。register.endsale()enteritem(.)makepayment(.)saletimeiscomplete : boolean/totalmakelineitem(.)register.saletimeiscomplete : boolean/totalcaptures111domain modelconceptual perspectivedesign modeldcd; software perspectivecurrentsale领域模型概念透视图设计模型dcd;软件透视

8、图类元类元l类元:描述行为和结构特性的模型元素l它们是对众多uml元素的泛化l最常用的两个类元是常规的类和接口表示表示uml属性的方式属性的方式l属性文本,如currentsale: salel关联线l两者兼有表示表示uml属性的方式属性的方式register.sale.1registercurrentsale : sale.sale.using the attribute text notation to indicate register has a reference to one sale instanceusing the association notation to indica

9、te register has a reference to one sale instanceobserve: this style visually emphasizes the connection between these classescurrentsaleregistercurrentsale : sale.sale.1thorough and unambiguous, but some people dislike the possible redundancycurrentsale使用属性文本表示法使用关联:这种风格从视觉上强调了类之间的关联指名register具有对一个sl

10、e实例的引用完整和明确的风格,两者兼有,但略嫌冗余表示表示uml属性的方式属性的方式准则:如果没有给出可见性,则通常假设属性为私有l导航线箭头:由源对象指向目标对象,表示register的一个属性是sale对象l多重性放置在目标一端(在没有明确指定时为1)l角色名只放置在目标一端,用以表示属性名称register.sale.1registercurrentsale : sale.sale.using the attribute text notation to indicate register has a reference to one sale instanceusing the ass

11、ociation notation to indicate register has a reference to one sale instanceobserve: this style visually emphasizes the connection between these classescurrentsaleregistercurrentsale : sale.sale.1thorough and unambiguous, but some people dislike the possible redundancycurrentsale导航线导航线(注)(注)l在两个类的导航关

12、系中,如果导航关系的一端有箭头,则箭头代表了导航的方向。l然而,如果有一个”x”出现在导航箭头的起始点上则表明这种导航关系可以不发生。如果既没有箭头,也没有“x”,则表明导航是双向的。register.sale.1registercurrentsale : sale.sale.using the attribute text notation to indicate register has a reference to one sale instanceusing the association notation to indicate register has a reference to

13、 one sale instanceobserve: this style visually emphasizes the connection between these classescurrentsaleregistercurrentsale : sale.sale.1thorough and unambiguous, but some people dislike the possible redundancycurrentsaleuml支持的可见性类型的标志支持的可见性类型的标志 标志+#-可见性类型publicprotectedprivatepackage“/”代表可导出(deri

14、ved)特征何时使用属性文本(关联线)何时使用属性文本(关联线)准则:对数据类型对象使用属性文本表示法,对其他对象使用关联线registerid: int.saletime: datetime.1applying the guideline to show attributes as attribute text versus as association linesstoreaddress: addressphone: phonenumber.1register has three attributes:1. id2. currentsale3. locationcurrentsaleloc

15、ation应用准则将属性表示为属性文本和关联线register具有三个属性:1.id2.currentsle3.loction关联端点的关联端点的uml表示法表示法l关联端点可以附加导航性箭头,也可以包含可选的角色名(关联端点名)来表示属性名称,也可以附加多重性,如*或0.1l还可以使用特性字符串(是由若干字符串来表述的属性特征)l如: ordered 表示集合中的元素是有序的lunique表示一组唯一元素l或ordered, list,其中list是用户定义的关键字特性字符串特性字符串lreadonly :只读属性; lunion :表明可导出属性是由其他属性联合的结果; lsubsets

16、:表明属性是一个继承得到的属性的子集; lredefines :改变一个继承得到的属性的名字; lordered:某类型的一个有序集合,集合中的元素不能重复; lbag:集合中的元素可以重复; lseq 或 sequence:集合元素可以重复,并且有序; lcomposite:组合属性;关联端点的关联端点的uml表示法表示法notice that an association end can optionally also have a property string such as ordered, listsaletime: datetime.saleslineitem.1.*lineit

17、emsordered, listsaletime: datetimelineitems : saleslineitem 1.* orlineitems : saleslineitem 1.* ordered.saleslineitem.two ways to show a collection attribute表示集合属性的两种方式注意在关联的末端也可以有特性字符串,例如ordered,list注释符号:注解、注释、约束和注释符号:注解、注释、约束和方法体方法体l注解符号:褶角矩形,并使用虚线连接到要注解的元素上l注解符号可以表示:luml注解或注释luml约束:l方法体:uml操作的实现例

18、:操作和方法操作和方法语法:visibility name (parameter-list) property-string还可以加返回类型或特性字符串,包括异常、操作是否抽象等如:+getplayer(name:string):playerexception ioexceptionpublic player getplayer(name:string) throws ioexception如何在类图中表示方法如何在类图中表示方法luml方法是操作的实现,如果定义了约束,则方法必须满足这些约束l在交互图中,通过消息的细节和顺序来表示l在类图中,使用构造型为method的uml注解符号regis

19、ter.endsale()enteritem(id, qty)makenewsale()makepayment(cashtendered)method/ pseudo-code or a specific language is okpublic void enteritem( id, qty ) productdescription desc = catalog.getproductdescription(id); sale.makelineitem(desc, qty);methoddcd中的操作问题中的操作问题lcreate操作,交互图中对其的解释为:在java和c#等语言中,对new操

20、作符和 构造器的调用l访问属性的操作,即提取或设置属性的操作,如getprice和setprice,通常不包含在类图中关键字关键字luml关键字是对模型元素分类的文本修饰关键字actorinterfaceabstractordered含义类元为参与者类元为接口抽象元素;具有强制顺序的一组对象用法示例置于类元名称之上置于类元名称之上置于类元名称或操作名称之后置于关联的端点或简化为或的形式构造型,简档和标记构造型,简档和标记l构造型也使用符号表示,例如authorshipl构造型表示对现有建模概念的精华,并且定义在uml简档中l简档是一组相关构造型、标记和约束的集合,其目的是使用uml专用于特定领

21、域或平台l标记:如果用构造型标记元素,则所有标记都适用于该元素,并且能够对其赋值构造型,简档和标记构造型,简档和标记stereotypeauthorshipauthor: stringstatus : stringuml extension relationship to a basic uml metamodel term elementauthorshipauthor = “craig”status = “tested”metaclasselement.authorshipsquare.using the stereotypea tool will probably allow a pop

22、up to fill in the tag values, once an element has been stereotyped with authorshipdeclaring the stereotype声明构造型uml扩展:关系于基本的uml元模型术语elementuml扩展:关系于基本的uml元模型术语element使用构造型一旦元素用uthorship创建构造型后,工具便可能允许弹出(菜单或窗口)以填写标记的值metclsselementstereotypeuthorshiputhorshipsqureuthorshiputhor=“crig”sttus=“tested”uml特

23、性和特性字符串特性和特性字符串l特性:表示元素特征的已命名的值。特性具有语义影响l特性字符串l如:abstract,visibility=publicl有些特性没有值,例如abstract,这通常表示布尔类型的特性,是abstract=true的简写进行类图建模的基本技术和思想进行类图建模的基本技术和思想l继承技术l关联和依赖技术l聚合和组合技术l为方法建模l为属性建模l在模型中引入接口类建模继承类建模继承1、继承技术要注意实现继承即接口继承应该能用一个子类的实例去替代父类的实例(liskov替换原理);留心多重继承;父类应该对子类一无所知;要留心仅以公共数据属性为基础的继承;类建模继承(续)

24、l在类层次结构中,要尽量把分解出的公共部分放在最高层;l子类应该继承所有内容(纯粹继承),因此也就继承了父类的所有关系。l纯粹继承使得我们不需要关心子类没有继承什么l纯粹继承的优点在于:在纵深的类层次中如果你仅需要了解每个类添加了什么,而不用知道其删除了什么,这会使你轻松得多泛化、抽象类、抽象操作表示泛化、抽象类、抽象操作表示l泛化:用由子类到超类的实线和空心三角箭头表示l抽象类和操作即可以采用abstract标记表示,也可以采用斜体名称来表示l终止类和不能够被子类复写的操作以leaf标记表示类建模关联和依赖l类之间的关联帮助我们定义对象如何和其他对象交互,使对象之间的协作成为可能。l当两个对

25、象之间不存在持久关联,但是他们又要进行协作时,两者之间可以建立依赖关系;l多重性一定要显示出来;l关联和依赖能够被继承(关联和依赖是通过属性和方法来实现的);l关联的以上性质同时适用于组成和聚合;依赖依赖l依赖用从客户到提供者的虚线箭头表示l依赖类型:l拥有提供者类型的属性l向提供者发送消息。对提供者的可见性可能是:属性、参数变量、局部变量、全局变量或类的可见性l接受提供者类型的参数l提供者是超类或接口l如果已经存在关联线,则不必添加第二条有虚线箭头的依赖线依赖依赖saleslineitem.productdescription.1.*lineitemssale.updatepricefor(

26、 productdescription ).the sale has parameter visibility to a productdescription, and thus some kind of dependencysle具有对productdescription的参数可见性,因此对其也具有某种依赖依赖依赖l如下代码:public class foopublic void dox()system.runfinalization();system.runfinalization().foo.dox().the dox method invokes the runfinalization

27、 static method, and thus has a dependency on the system classdox方法具有对system类的依赖依赖标签依赖标签l为表示依赖的类型,或者为代码生成工具提供帮助,可以给依赖线附加关键字或构造型callwindowa dependency on calling on operations of the operations of a clockclockgettime().createaa dependency that a objects create b objectsb.依赖于对clock中的操作的调用cllcrete对象创建b对

28、象而产生依赖类建模接口类建模接口l在模型中引入接口l接口是一个或多个操作特征标记,以及零个或多个属性的定义。在理想状态下,接口定义了一组内聚的行为;l接口由类或组件实现。要实现某个接口,类或组件必须包括一些方法;l使用接口有助于增加设计过程的灵活性、可扩展性和可插拔性(多态,代码只和接口打交道);l接口是多重继承的一种替代方案;类建模接口(续)类建模接口(续)interfacetimergettime()clock1.gettime().lollipop notation indicates clock3 implements and provides the timer interface

29、to clientstimer is a provided interfacetimerclock3.gettime().window2window3dependency line notationwindow2 has a dependency on the timer interface when it collaborates with a clock2 objectsocket line notationwindow3 has a dependency on the timer interface when it collaborates with a clock3 objectwin

30、dow1timersocket line notationwindow1 uses the timer interface it has a required interfaceclock2.gettime().clock1 implements and provides the timer interfacetimer插座线表示法window1使用timer接口它持有必要的接口window1timerinterfcetimerclock1实现并提供timer接口依赖线表示法window2在于clock2对象协作时,依赖于timer接口clock1clock2棒棒糖表示法指明clock3为客户

31、实现并提供了timer接口clock3timerwindow3window2插座线表示法window3在于clock3对象协作时,依赖于timer接口类建模聚合和组合l能用单句规则来验证其合理性:“一个对象是另一个对象的一部分”或者“一个对象被另一个对象所包含”;l交互的大部分是从整体到部分的;l在适当的时候要使用组合,但是可以忽略聚合l在某一时刻,部分的实例只属于一个组成实例l部分必须总是属于组成l组成要负责创建和删除其部分(如果组成被销毁,其部分也必须被销毁,或者依附于其他组成)组合表示finger0.7handcomposition1square40board1saleslineitem

32、1.*sale1composition means -a part instance (square) can only be part of one composite (board) at a time -the composite has sole responsibility for management of its parts, especially creation and deletion组合意味着:在某一时刻,部分实例(squre)只能是一个组合的一部分(bord)组合必须管理其部分的职责,尤其是创建和删除在uml中,用带有实心菱形箭头的关联线来表示组合,箭头指向其组成类约束

33、约束luml约束是对uml元素的限制或条件。用之间的文本表示stacksize : integer size = 0 push( element )pop() : objectthree ways to show uml constraints post condition: new size = old size + 1 post condition: new size = old size 1 显示uml约束的三种方式限定关联限定关联l限定关联具有限定符,限定符用于从规模较大的限定关联具有限定符,限定符用于从规模较大的相关对象集合中,依据限定符的键选择一个或多相关对象集合中,依据限定符的键

34、选择一个或多个对象(暗示基于键对事物进行查找)个对象(暗示基于键对事物进行查找)l要注意多重性的变化(多变要注意多重性的变化(多变1)productcatalogproductdescriptionitemidcontainsproductcatalogproductdescriptioncontains1.*multiplicity reduced to 1(a)(b)qualifier111单实例类单实例类 1servicesfactoryinstance : servicesfactoryaccountingadapter : iaccountingadapterinventoryada

35、pter : iinventoryadaptertaxcalculatoradapter : itaxcalculatoradapter getinstance() : servicesfactorygetaccountingadapter() : iaccountingadaptergetinventoryadapter() : iinventoryadaptergettaxcalculatoradapter() : itaxcalculatoradapter.uml notation: in a class box, an underlined attribute or method in

36、dicates a static (class level) member, rather than an instance memberuml notation: this 1 can optionally be used to indicate that only one instance will be created (a singleton) 模版类和接口模版类和接口l模版化类型,也称为模版、参数化类型和泛型l如代码:public class boardprivate list squares=new arraylist();模版类和接口模版类和接口interfacelistclea

37、r().kparameterized or template interfaces and classes k is a template parameter anonymous class with template binding completeboardsquares : list orsquares : list.arraylistclear().the attribute type may be expressed in official uml, with the template binding syntax requiring an arrow orin another la

38、nguage, such as javaarraylistelements : t*.clear().tfor example, the elements attribute is an array of type t, parameterized and bound before actual use.there is a chance the uml 2 “arrow” symbol will eventually be replaced with something else e.g., =参数化或模版接口和类k是模版参数listrrylistbordrrylistsqure属性类型可以

39、用正式的uml表示,例如使用箭头的模版绑定语法例如:elements属性是类型为t的数组,在实际使用前要进行参数化和绑定这里存在某种可能性,即uml2的“箭头”符号可能最终被其它符号代替,例如“=”用户自定义的分栏用户自定义的分栏dataaccessobjectid : int.dox().exceptions throwndatabaseexceptionioexceptionresponsibilitiesserialize and write objectsread and deserialize objects.主动类主动类l主动对象运行于自己控制的执行线程之上 in t e r f

40、a c e r u n n a b ler u n ( )c lo c k. . .r u n ( ). . .a c t iv e c la s sinterfcerunbleclock主动类类建模为属性建模为属性建模l把private可见性赋给所有属性;l仅通过set方法更新属性;l仅通过get方法直接访问属性;l始终调用属性的set方法对其进行更新,即使在该属性被定义的类中也是如此;l在属性的set方法中,实现简单的验证有效性的逻辑;l在分离出来的方法中,实现复杂的验证有效性的逻辑;l但是在类图中不描述这些简单的存取方法类建模为方法建模为方法建模l指明方法的可见性、名称、参数、返回值及构

41、造型;l为了减少类之间的耦合,要尽可能地限制方法可见性。l方法命名方式采用动宾结构: printmailinglabell静态方法有下划线,实例方法没有;l对于初始化方法(创建和构造函数),在dcd中可以忽略类建模为方法建模(续)为方法建模(续)l为方法写标题文档l方法做了什么;所有的错误条件和方法抛出的异常;方法适用的前提和后置条件;l为方法写内部文档l控制结构;代码完成的功能、以及为什么要完成这些功能;局部变量说明;比较难或者比较复杂的代码;处理顺序。注:方法的内部文档是类的详细设计设计类图包含的内容设计类图包含的内容1、设计类图:定义了软件类的规格说明和应用程序接口。1)类、属性和方法2

42、)接口3)继承4)类之间的关联和导航5)类、接口等元素之间的依赖关系交互图和类图之间的关系交互图和类图之间的关系l当我们绘制交互图时,在此动态对象建模的创造性设计过程中会产生一组类及其方法: register: salemakepayment(cashtendered)makepayment(cashtendered) register.makepayment().sale.makepayment().1currentsalemessages in interaction diagrams indicate operations in the class diagramsclasses ide

43、ntified in the interaction diagrams are declared in the class diagrams交互图中的消息表示类图中的操作交互图中的确定的类要在类图中加以声明如何建立设计类图如何建立设计类图建立设计类图的输入l交互图:识别软件类和类中的方法l概念模型:在类的定义中添加细节(如属性)如何建立设计类图(续)如何建立设计类图(续)(1)、通过分析交互图,识别出所有参与软件解决方案的类;(2)、将他们在一个类图中绘出;(3)、复制概念模型中的相关概念的属性到类图的类中,并为属性建模;(4)、通过分析交互图来为类图中的类添加方法;(5)、为属性和方法添加类型信息;(6)、在类图中添加关联,以支持必要的类之间的可见性;(7)、在关联上添加导航箭头,来指明属性可见性的方向;(8)、添加依赖关系连线,来指明非属性的可见性。1-3步步带属性的类图带属性的类图添加方法添加方法l每一个类的方法都

温馨提示

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

评论

0/150

提交评论