《类图对象包图》PPT课件.ppt

上传人:牧羊曲112 文档编号:5565934 上传时间:2023-07-28 格式:PPT 页数:175 大小:2.70MB
返回 下载 相关 举报
《类图对象包图》PPT课件.ppt_第1页
第1页 / 共175页
《类图对象包图》PPT课件.ppt_第2页
第2页 / 共175页
《类图对象包图》PPT课件.ppt_第3页
第3页 / 共175页
《类图对象包图》PPT课件.ppt_第4页
第4页 / 共175页
《类图对象包图》PPT课件.ppt_第5页
第5页 / 共175页
点击查看更多>>
资源描述

《《类图对象包图》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《类图对象包图》PPT课件.ppt(175页珍藏版)》请在三一办公上搜索。

1、第五章 类图、对象图、包图,计算机科学学院 董兆安,Page 2,内容提要,类图由类以及类之间的关系组成。类图是所有面向对象建模方法的核心部分。类图描述了系统的静态结构。类可以表示程序设计中的一个类型。也可以表示现实世界的一类事物或概念。信息系统开发的各个阶段都要使用UML中的类图。在系统开发的不同阶段,类图表示了不同层次的抽象。概念层(Conceptual):在需求分析阶段,类图主要用于领域内的一些概念类的描述,形成概念模型;规格说明层:(Specification):在设计阶段,类图着重描述类与类之间的接口等外部特性,形成设计模型;实现层(Implementation):在实现阶段,类图主

2、要用于描述类的在软件系统中的内部实现。,Page 3,Classifiers(类目、分类器),A classifier is a mechanism that describes structural and behavioral features.Classifiers include classes,associations,interfaces,data types,signals,components,nodes,use cases,and subsystems.,Page 4,类(Classes),A class is a collection of things or concep

3、ts that have the same characteristics.Each of these things or concepts is called an object.类是具有相同特征(属性)和行为(方法)的对象的集合。a class describes a group of objects with:similar properties(attributes)common behavior(operation,method)common relationships to other objects common meaningclass diagrams show classe

4、s with their attributes and operations,together with the associations between classes,Page 5,describing a class,The level of detail you choose to show for your classes depends on who is reading the diagrams on which they appear.For example,a stakeholder whos focused on the big picture is probably in

5、terested only in the names of the classes,while a developer working at a more detailed level probably wants to see a full set of attributes and operations.,Page 6,简化表示(Icon Notation),Some models representations can be switched between Icon Notation and Class Notation.,Page 7,类的名称(Class names),UML中的名

6、称是标识某个模型元素的字符串。简单名(simple name):字母、数字和下划线的序列,通常以字母开始。复合名(path name):以分隔符分开的一个或者多个简单名的组合序列。例如:java:awt:Rectangle;java:util:Date 复合名称中的最后一个一般指某个实体,如:类或者用例。虽然UML具有非常宽松的命名规定,许多标点都可以使用。但是为了避免混乱,我们还是建议采用简单名称的命名约定,尽量使用字母、数字、下划线构成字符串的命名方式。在分析和设计的不同阶段,类的图符可以使用恰当的精简方式,省略一些细节,甚至属性、方法两个要素都可省略。但是类的名称是三个要素中,必须具备的

7、要素,不可以省略。,Page 8,属性 attribute,The full form of a UML attribute declaration is as follows:visibility name:type multiplicity=default-value property-string,visibilityNametypemultiplicitydefault-valueproperty-string,+(public),#(protected),-(private)e.g.CustomerName,DiscountRatee.g.Point,String,Date,etc.

8、e.g.0.1,2.*e.g.=(0,0),=nullchangeable(default),addOnly,frozen,6 examples of legal attribute declarationsoriginorigin:Point=(0,0)+originid:integer frozenorigin:Pointname:0.1:String,Page 9,可见性,visibility:The UML supports the following four levels of visibility:Package visibility(shown with a tilde)mea

9、ns that objects belonging to any class in the same package can see and use the given class it.Public visibility(+)means that objects belonging to any class can use the given attribute or operation.Protected visibility(#)means that only objects that belong to subclasses of the given class(at any leve

10、l below that class)can use the attribute or operation.(Subclasses are discussed in Chapter 2.)Private visibility(-)means that only objects belonging to the class itself can use the attribute or operation.,Page 10,visibility,When you specify the visibility of a classifiers features,you generally want

11、 to hide all its implementation details and expose only those features that are necessary to carry out the responsibilities of the abstraction.Thats the very basis of information hiding.,Page 11,属性的类型,type:Examples of built-in attribute data types include Double,Int(short for Integer),and String.An

12、attribute type can also be a user-defined type or the name of a class.,Page 12,多重性(Multiplicity),Multiplicity indicates how many of one thing can exist relative to another thing.A multiplicity value on an attribute indicates how many instances of that attribute are present for each instance of the c

13、lass.A multiplicity expression can take several forms,including the following:A fixed value(such as 1 or 3)An asterisk(*),which means manyA range of values,expressed as lower.upper(for instance,0.1 or 3.*)A set of values(for example,1,3,5,7),Page 13,an example,An object belonging to the Account clas

14、s can have from one to three email addresses.Each Account object has an ID that cant be deleted;you might think of this as being the equivalent of the key within a database table.And,theres an initial value for an Account objects password,which the Customer is likely to change but which is useful in

15、 case the Customer forgets to define a password when he or she sets up an Account.,Page 14,operation signatures 操作签名,The full form of a UML operation declaration is as follows:visibility name(parameter-list):return-typeThe parameters of an operation,which appear in a list separated by commas,represe

16、nt the data provided by the caller of the operation,the data that the operation returns to the caller,or both.,visibilitynameparameter-listreturn-type,+(public),#(protected),-(private)e.g.restart,addStudentsee next pagee.g.Integer,String,etc,Page 15,parameter declaration 参数声明,The full form of a para

17、meter declaration is as follows:direction name:type multiplicity=default-value A parameter can have one of the following three directions:in(The operation cant modify the parameter,so the caller doesnt need to see it again.)out(The operation sets or changes the value of the parameter and returns it

18、to the caller.)inout(The operation uses the value of the parameter and may change the value;the caller expects to see an inout parameter again.),directionnametypedefault-value,in,out,inoute.g.getId,calculateBonusBoolean,Money,etc.e.g.42,John,Page 16,an example,The checkAvailability operation receive

19、s a Book object and returns a value of the user-defined type OrderStatus.The isFulfilled operation is a query that returns True if everything the customer ordered is in place or False otherwise.,The UML defines a separate language,called the Object Constraint Language(OCL),that you can use to specif

20、y constraints.,Page 17,Note,对象约束语言简称OCL(Object Constraint Language),它是一种用于施加在指定的模型元素上约束的语言。Using the OCL enables you to reach a certain level of rigor in your modeling without side effects.In other words,the evaluation of OCL expressions cant alter the state of the executing system.对象约束语言是一种形式化语言,它主

21、要用于表示UML模型中施加于模型上的约束。OCL表达式以附加在模型元素上的条件和限制来表现对该对象的约束,其中包括附加在模型元素上的不变量或约束的表达式,附加在操作和方法上的前置条件和后置条件等。Two kinds of constraints are of particular interest in the context of operations:preconditions and postconditions.,Page 18,NOTE,Abstract:把一个类名写为斜体表示是抽象的;leaf,表示它是叶子操作,意味着该操作不是多态的,不可以被覆写(这类似于java中的final操

22、作)。除了leaf之外,还有:查询(query),操作不会改变系统的状态;顺序(sequential),调用者必须在对象外部进行协调;监护(guarded),通过将所有对象监护操作的所有调用顺序化;并发(concurrent),静态(static);,Page 19,responsibilities,职责指的是类所担任的任务,类的设计要完成什么样的功能,要存担的义务。一个类可以有多种职责,设计得好的类一般至少有一种职责,在定义类的时候,将类的职责分解成为类的属性和方法。类的职责其实只是一段或多段文本描述。通常在UML中在类图的最下方用单独的部分列出类的职责。Its often desirabl

23、e to define explicit responsibilities for a class.These represent the obligations that one class has with regard to other classes The idea of assigning responsibilities to classes is at the center of Class-Responsibility-Collaboration(CRC)cards.This idea is also central to Responsibility-Driven Desi

24、gn.,Page 20,UML object notation,The notation for an object takes the same basic form as that for a class.There are three differences between the notations,as follows:Within the top compartment of the class box,the name of the class to which the object belongs appears after a colon.The object may hav

25、e a name,which appears before the colon,or it may be anonymous,in which case nothing appears before the colon.The contents of the top compartment are underlined for an object.Each attribute defined for the given class has a specific value for each object that belongs to that class.,Page 21,classes a

26、nd subclasses,继承,指的是一个类(子类)继承另外的一个类(超类)的功能,并增加它自己的新功能的能力。考虑银行账户的类型:CheckingAccount 和 SavingsAccount 类从 BankAccount 类继承而来。,Page 22,继承和泛化,继承关系由每个超类的单独的线画出,这是在IBM Rational Rose和IBM Rational XDE中使用的方法。然而,有一种称为 树标记的备选方法可以画出继承关系。当存在两个或更多子类时,除了继承线象树枝一样混在一起外,你可以使用树形记号。,Page 23,抽象类(Abstract Classes),An abstr

27、act class is a class that cant have any instances.Abstract classes are generally designed to capture operations that subclasses inherit.The idea is that the operations defined for an abstract class are relatively general,and each class that inherits these operations refines and expands upon them.In

28、UML notation,the name of an abstract class appears in italics You can also use an abstract stereotype for readers of the model who might not realize the significance of the italics.前面的图中,父类的类名BankAccount和withdrawal操作使用斜体。这说明父类BankAccount 是一个抽象类,而withdrawal方法是抽象的操作。换句话说,BankAccount 类使用withdrawal规定抽象操

29、作,并且两个子类CheckingAccount 和 SavingsAccount 都分别地执行它们各自版本的操作。然而,超类(父类)不一定要是抽象类。标准类作为超类是正常的。,Page 24,主动类(Active Classes),An active class is a class that represents an independent flow of control,such as a process or a thread.The class box for an active class has thin vertical bars just inside the borders

30、,as shown below.The instances of an active class are called active objects,Page 25,Scope(作用域/有效范围/辖域),The owner scope of an attribute(respectively an operation)specifies whether the attribute(the operation)appears in each instance of the class or whether there is just a single instance of the featur

31、e for all instances of the class.In the UML,there are two kinds of owner scope:instance each instance of the class holds its own value(definition)for the attribute(operation)class there is just one value(definition)of the attribute(operation)for all instances of the class,Page 26,Scope,The class sco

32、pe is rendered by underlining the attribute(operation)Attribute(operation)at the class scopeClass(operation)attributeDo not apply to an instance,but to the class(e.g.,constructors),Page 27,Class Multiplicity,How many instances of a specific class will be created at runtime?Zero:for an abstract class

33、,or if you want the class to expose only class scope attributes and operationsOne:this is a singleton(单体)X(a specific number)Many(default)The multiplicity of a class is written in the upper-right corner of the class icon,NetworkController,consolePort 2.*:Port,1,ControlRod,3,Page 28,接口(Interfaces),An

34、 interface is a collection of operations that represent services offered by a class or a component.By definition,all of these operations have public visibility.The interface specifies something like a contract that a class must adhere to;the class realizes(or provides a realization for)one or more i

35、nterfaces.UML中的接口是一种实现继承的形式,但这种继承形式与Java中通过关键词extends实现的继承有所不同。一个类和一个接口不同:一个类可以有它形态的真实实例,然而一个接口必须至少有一个类来实现它。在 UML 2 中,接口长方形的顶部区域有版型interface,如图:,Page 29,接口的类型,The UML defines two kinds of interfaces:provided interfaces(供接口)and required interfaces(需接口)。,lollipop notation棒棒糖,Page 30,接口的类型,You can also

36、 show one class providing an interface that another class requires,using“ball and socket”notation,where the ball represents the provided interface and the socket indicates the required interface。The Inventory class provides the Retrieve Books interface that the Order class requires,Page 31,端口 port,A

37、 port specifies a distinct interaction point between a class and its environment.Ports group provided interfaces and/or required interfaces in two ways.They serve as focal points through which requests can be made to invoke the operations that the class makes available.They also serve as gateways fo

38、r calls that the class makes to operations offered by other classes.,An instance of the Order class receives a request to fulfill the actual order that the instance represents via the Perform Fulfillment interface.The Order instance uses the Retrieve Books interface in meeting this request.After the

39、 given order is shipped,the associated Customer may request tracking information via the Provide Tracking Info interface.The Order instance,in turn,uses the Retrieve Tracking Info interface to acquire the necessary information to return to the Customer.,端口作为互作用点并且在类封装的“壳”中提供开放的门,Page 32,Class Diagra

40、m:Stereotypes,Analysis class stereotypes differentiate the roles objects can play:Boundary objects model interaction between the system and actors Entity objects represent information and behaviour in the application domainControl objects co-ordinate and control other objects(Control flow of Use Cas

41、e scenario executions)Identified by a text label in the class name component shown between guillemets.,Page 33,Class Diagram:Stereotypes,User Interface:AddAdvertUI,Alternative notations for boundary class:,Page 34,Class Diagram:Stereotypes,Alternative notations for entity class:,Page 35,Class Diagra

42、m:Stereotypes,Alternative notations for control class:,Page 36,Class Diagrams:Defining relationships between classes,Page 37,Class Relationships,Classes,by themselves,arent particularly useful.Its the relationships among classes that provide the foundation for the structure of a new system.The follo

43、wing sections explore how you use the UML to illustrate various kinds of class relationships.,Page 38,The different relationships,The different relationships in a class diagramAssociation 关联aggregation 聚合;聚集composition 组合;合成Generalization 泛化Realization(interfaces)实现Dependency 依赖,Page 39,Associations

44、 关联,Association:a structural relationship that specifies that objects of one class are connected to objects of anotherAn association describes a group of links(semantic connection among objects)with common structure and semanticsAssociations represent:The possibility of a logical relationship or con

45、nection between objects of one class and objects of anotherIf two objects can be linked,their classes have an associationMathematically correspond to a set of tuples(Relation)Can be implemented in many ways,depending on the programming language.Graphically:a solid line connecting the same or differe

46、nt classes,Page 40,Associations,Refining the semantics of an association using:,Basic featuresA nameMultiplicities A roleA navigationAggregation and composition adornments,Advanced featuresVisibilities Qualifications 条件Interface specifierArity 元数,度,目Association classesConstraints,Page 41,Association

47、 name,The name of an association describes the nature of the relationshipYou can give a direction to a name by providing a direction triangle that points in the direction you intend to read the name,Dan Goldin,NASA,Bill Gates,Microsoft,John Smith,Rockwell,.,.,Works-for,Person,Organization,Works-for,

48、Employs,Page 42,Association multiplicity,Limit the number of instances of one class that may be related to an instance of anotherDefine the allowed number of links between objects,Class,Class,Class,Class,Class,many(zero or more),optional(zero or one),mandatory(one or more),numerically specified,1.*,

49、2.4,6.8,exactly one,0.*,0.1,1,Page 43,Meaning of Multiplicity Constraints,Course,1,teaches,Professor,*,Professor,Course,SWEN5232,CSCI3233,SWEN5233,Dr.Smith,Dr.White,Dr.Brown,Dr.Jones,CSCI4432,Dr.White,CSCI3233,Dr.Smith,CSCI4432,Dr.Brown,SWEN5232,teaches,Dr.Brown,SWEN5233,Page 44,Analyzing and valida

50、ting associations,Many-to-oneA company has many employees,An employee can only work for one company.This company will not store data about the moonlighting activities of employees!A company can have zero employeesE.g.a shell companyIt is not possible to be an employee unless you work for a company,*

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号