UML建模语言及工具PPT课件-第2章 对象技术与可视化建模基础.ppt_第1页
UML建模语言及工具PPT课件-第2章 对象技术与可视化建模基础.ppt_第2页
UML建模语言及工具PPT课件-第2章 对象技术与可视化建模基础.ppt_第3页
UML建模语言及工具PPT课件-第2章 对象技术与可视化建模基础.ppt_第4页
UML建模语言及工具PPT课件-第2章 对象技术与可视化建模基础.ppt_第5页
已阅读5页,还剩77页未读 继续免费阅读

下载本文档

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

文档简介

uml建模语言及工具,第 2 章 对象技术与可视化建模基础 an overview of object technology and visual modeling,-3-,review: an approach to the object-oriented,上升到面向对象 面向对象技术概述 定义、利益、研究内容 面向对象技术发展 历史(里程碑)、现状、未来 面向对象与结构化 扬弃而非否定 上升到面向对象 利用面向对象的观点认识客观问题,-4-,学习线路图,-5-,agenda 1,object and class four principles of oo generalization and polymorphism principles of visual modeling the uml process and visual modeling,-6-,object,informally, an object represents an entity, either physical, conceptual, or software physical entity conceptual entity software entity,化学过程,-7-,object-formal definition,an object is an entity with a well-defined boundary and identity that encapsulates state and behavior. state is represented by attributes and relationships. behavior is represented by operations, methods, and state machines.,-8-,对象的标识,每一个对象都有一个唯一的标识, 即使其状态有可能与其它对象一样,班机 747 on 11/10,班机 747 on 11/11,班机 747 on 11/12,-9-,对象的状态,一个对象的状态是它反映于现实世界的一系列属性: 参数的数值 (也就是,与对象有关系的数据) 它与其它对象的关系,可能的状态: 停泊 运营 着陆 终止,班机 747,-10-,对象的行为,定义当其它对象发出请求时,该对象如何反应 由为对象定义的一系列操作决定,-11-,sample-an object,-12-,representing objects in the uml,an object is represented as a rectangle with an underlined name.,named object,anonymous object,-13-,class,a class is a description of a set of objects that share the same attributes, operations, relationships, and semantics. an object is an instance of a class. a class is an abstraction in that it emphasizes relevant characteristics. suppresses other characteristics.,-14-,sample-a class,class employee,properties name address position salary start date end date,behavior hire fire promote increase salary retire,-15-,representing classes in the uml,a class is represented using a rectangle with compartments.,属性,操作,具有相同特征和操作的对象可以放到类中。 在下面这副图中您看到了什么类?,类,对象的类,您看到了几个类?,-18-,属性,属性是类的特征或特性 属性的值是某一特定对象的属性值 在类中属性名必须是唯一的 每一个类的实例都有为这个类定义的所有属性的值,银行帐户类属性 帐号 银行名称 拥有者 金额,mary的银行帐户属性值 12345678 first national bank mary smith $1024.48,-19-,属性取决于视点,从销售人员的角度 型号 价格 颜色 里程数,一辆汽车具有的属性:,从维修人员的角度 马达类型 传动类型 维修记录,-20-,操作,对象的行为是由为此对象定义的一系列操作决定的 操作访问或修改对象的属性值 一个类可能同时存在多个实例,也可能在某一时刻没有实例 一个类的所有实例都可以使用在这个类中定义的操作,-21-,类和对象,对象,实体,类,抽象数据类型,计算机世界,实 例 化,抽 象,映 射,映 射,现实世界,-22-,类是生成对象的模板,类是生成对象的模板 类的定义中包含有产生和删除对象的操作 一个类定义了使用哪种数据表示法来描述属性 每一个实例都采用有属性值的数据表示法 一个类通过一系列操作来定义行为 这种操作能在每个实例中激活,班机,-23-,类和对象的关系,每一个对象都是某一个类的实例 每一个类在某一时刻都有零个或更多的实例 类是静态的;它们的存在、语义和关系在执行前就已经定义好了 对象是动态的; 它们在程序执行时可以被创建和删除,-24-,sample-object and class,现实世界中的学生对象和对student类的建模,bob,john,kate,beverly,student 属性 方法,一些学生对象,student类,-25-,agenda-2,object and class four principles of oo generalization and polymorphism principles of visual modeling the uml process and visual modeling,-26-,basic principles of object orientation,-27-,抽象,抽象(abstraction)就是过滤掉对象的一部分特性和操作, 直到只剩下所需要的属性和操作。,一些权威认为抽象对于建模者来说是最重要的技术, 也就是说要搞清楚什么应该纳入模型中,什么应该舍去。,抽取共同性,例如,准备编制游戏软件,其中需要 实现不同人物使用魔法的功能。 每个人物具有不同的魔法,并具备使 用魔法的行为,抽象得到的人物类图如:,-28-,example: abstraction,一个项目的抽象依赖于定义抽象的上下文,-29-,封装性-encapsulation,hides implementation from clients. clients depend on interface.,-30-,封装性,封装(encapsulation)是软件模块化思想的体现,也是面向对象的根本之一 封装实现信息隐藏和数据抽象 信息隐藏的出发点:对象的私有数据不能被外界存取,只能以合法的手段访问 将数据抽象为一组行为,而不是内部的具体数据结构,把用户隔离在细节之外,从而使得软件各个部分依赖于抽象层,各模块获得自由,可以变化细节,可以替换,-31-,为什么要封装,结构化程序设计:程序 = 算法 + 数据结构,全局数据,算法,算法,算法,算法,一个大问题:如何保证数据的一致性?,-32-,范例:数据一致性,class shippingaddress public long citycode; public string address; ,城市代码 例如:北京为01 上海为02,邮政地址 “北京朝阳区静安里6号”,操作这个数据结构的程序员,必须严格遵守一系列业务逻辑规则,否则很容易破坏数据的一致性 结构化程序设计处理大项目时,多人协同开发时,本质上无法保证数据的一致性,class shippingaddress private long citycode; private string address; public long modifyaddress(string address) ,-33-,信息隐藏保证数据一致性,通过信息隐藏: 1. 外围代码无法随意访问底层数据,只能以合乎规范的方式操作数据 2. 保证数据一致性的逻辑只需正确开发一次,便可以供所有人反复使用 3. 业务逻辑知识被局限起来,协同开发难度降低 信息隐藏是面向对象更适应大型软件开发的根本原因之一,-34-,数据隐藏的实现:c,文件域内的数据隐藏,粒度太粗 /* 本文件域的私有数据 */ static int use_count = 0; void func(int arg) +use_count; c的结构体无数据隐藏机制: struct simpleobject_t int myinteger; void (*setinteger)(int); ;,-35-,数据隐藏的实现:c+,用private访问级别关键字定义私有数据,隐藏内部数据 struct simpleobject private: int myinteger; public: void setinteger(int i); simpleobject(int i = 0): myinteger(i) 使用: simpleobject so(8); so.myinteger = 16; / compile error so.setinteger(16); / ok c+对于对象私有数据的隐藏,完全是靠编译器保证,而由于c+过于强大的指针运算能力,编译器很容易被欺骗,所以,-36-,java中的数据隐藏,so,simpleobject对象实体,int myinteger,void setinteger(int i),虽然so本身也是指针(在java里叫做引用reference),但是java没有提供任何指针运算,因此无法骗过编译器,能够完全保证私有成员数据不被非法访问。,-37-,模块性-modularity,breaks up something complex into manageable pieces helps people understand complex systems cohesion(内聚), coupling(耦合) class, component, package,-38-,package,a general purpose mechanism for organizing elements into groups a model element that can contain other model elements. a package can be used: to organize the model under development. as a unit of configuration management.,-39-,a package can contain classes,the package, university artifacts, contains one package and five classes,-40-,层次性-hierarchy,any ranking or ordering of abstractions into a tree-like structure types of hierarchy:inheritance, aggregation,increasing abstraction,decreasing abstraction,elements at the same level of the hierarchy should be at the same level of abstraction,-41-,agenda-4,object and class four principles of oo generalization and polymorphism principles of visual modeling the uml process and visual modeling,-42-,generalization,a relationship among classes where one class shares the structure and/or behavior of one or more classes defines a hierarchy of abstractions in which a subclass inherits from one or more superclasses single inheritance multiple inheritance is an “is a kind of” relationship.,-43-,范例:单一继承,one class inherits from another,-44-,范例:多重继承,a class can inherit from several other classes,use multiple inheritance only when needed and always with caution!,-45-,what is inherited?,a subclass inherits its parents attributes, operations, and relationships a subclass may: add additional attributes, operations, relationships redefine inherited operations (use caution!) common attributes, operations, and/or relationships are shown at the highest applicable level in the hierarchy,-46-,范例:继承什么?,class student protected string name; public string getname() public account theaccount; class graduatestudent extends student 派生类(子类)从基类(超类、父类)中派生,继承了基类中全部的public和protected数据成员和方法。所以即使graduatestudent中没有定义getname(),也会从student中得到getname()方法的全部实现 派生类也会继承基类中的关系,因此graduatestudent与account也有聚合关系,-47-,继承的原始目的,在面向对象思想发展的初期,通过继承复用代码曾经被认为是面向对象最重要的目标之一 当时人们的想法是:从基类获得大部分成熟的代码,实现主要的功能,而修改必要的部分,实现特色功能 很遗憾,实践中人们发现无法达成事先预期的效果,-48-,继承并非代码复用的最佳机制,继承在代码复用方面不如其它方式灵活有效比如函数库 代码复用在今天的意义不象先前预期的那么大,原因是:代码只是细节,系统由接口定义。另外软件开发的人力资源越来越丰富,开发难度大幅降低 但是继承所提供的代码复用仍然是一项重要的“福利”,应当善加利用 今天继承的主要意义在于为实现多态铺平了道路,-49-,what is polymorphism?,the ability to hide many different implementations behind a single interface,-50-,范例:多态,abstract class shape public abstract void draw(); class rectangle extends shape / 覆盖(override)基类方法 public void draw() . /* 绘制矩形 */ class circle extends shape / 覆盖(override)基类方法 public void draw() /* 绘制圆形 */ ,-51-,虚函数多态,class shape public: virtual void draw (void) ; ; class circle : public shape public: virtual void draw (void) ; ; class sphere : public shape public: virtual void draw (void) ; ,void drawshape(shape *ashape) ashape-draw(); main() shape ashape; circle acircle; sphere asphere; drawshape( ,-52-,应用多态性,假设我们有一个数组sharr,里面放着一排shape,但是不知道哪些是rectangle,哪些是circle。利用多态性,我们可以: for (int i = 0; i sharr.length; +i) shape shape = (shape)sharri; shape.draw(); 遍历整个数组的过程中,各个shape自己知道应当如何在画布上绘制自己。shape.draw()这同一行代码在shape指向不同的对象时表现出不同的行为,这就是所谓多态性,-53-,agenda-6,object and class four principles of oo generalization and polymorphism principles of visual modeling the uml process and visual modeling,-54-,what is a model?,a model is a simplification(简化) of reality,-55-,why model?,modeling achieves four aims: helps you to visualize a system as you want it to be. permits you to specify the structure or behavior of a system. gives you a template that guides you in constructing a system. documents the decisions you have made. you build models of complex systems because you cannot comprehend such a system in its entirety you build models to better understand the system you are developing,-56-,建模的意义,模型是对现实的简化,建模是为了更好地理解系统。 模型帮助我们按照实际情况或需求对系统可视化;(掌握不了文字,画幅画代替) 模型允许我们详细说明系统的结构、行为; 模型给出了一个构造系统的模板; 模型对我们作出的决策进行文档化。(先有文档,再有代码),-57-,the importance of modeling,paper airplane,fighter jet,less important,more important,-58-,principle 1: the choice of model is important,the models you create profoundly influence how a problem is attacked and how a solution is shaped in software, the models you choose greatly affect your world view each world view leads to a different kind of system,-59-,principle 2: levels of precision may differ,every model may be expressed at different levels of precision(精度) the best kinds of models let you choose your degree of detail, depending on: who is viewing the model why they need to view it,-60-,principle 3: the best models are connected to reality,all models simplify reality a good model reflects potentially fatal characteristics the trick is to be sure that your simplifications dont mask any important details. a good model reveals any potentially fatal flaws in design,-61-,principle 4: no single model is sufficient,no single model is sufficient. every non-trivial system is best approached through a small set of nearly independent models. create models that can be built and studied separately, but are still interrelated,-62-,建模的原理,选择创建什么模型对如何动手解决问题和如何形成解决方案有意义深远的影响。 三只小猪盖房子 摩天大厦 每一种模型可以在不同的精度级别上表示。 最好的模型可以让你根据观察的角色及原因选择它的详细程度。 对每个系统最好用一组几乎独立的模型去处理。 飞机模型,-63-,agenda-7,object technology object and class four principles of oo generalization and polymorphism principles of visual modeling the uml process and visual modeling,-64-,the uml,umlyou must learn umlunified modeling language uml是一种标准的图形化建模语言,它是面向对象分析与设计的一种标准表示,它: 不是一种可视化的程序设计语言,而是一种可视化的建模语言 不是工具或知识库的规格说明,而是一种建模语言规格说明,是一种表示的标准 不是过程,也不是方法,但允许任何一种过程和方法使用它,-65-,what is the uml?,the uml is a language for visualizing specifying constructing documenting the artifacts of a software-intensive system,unified modeling language(统一建模语言)是对象管理组织(omg)制定的一个通用的、可视化的建模语言标准,可以用来可视化(visualize) 、描述(specify)、构造(construct)和文档化(document)软件密集型系统的各种工件(artifacts,又译制品),-66-,uml是标准的符号,1. 用uml画图很容易,摆脱符号烦恼,全心面对问题,2. uml仅仅是一种表达形式,用好uml首先需要掌握ooad的基本原则和方法,并在一定的软件开发过程(如统一过程up/usdp/rup、xp等)的指导下进行有取舍的运用,但知道要画什么是困难的!,-67-,选择uml-1,martin flower:如果你正在使用其它的旧技术,我强列建议您马上转用uml,因为它明显地将成为符号系统的统一标准。如果你正在考虑开始使用设计符号来工作,uml是一个好的选择,因为它已经统治业界了,-68-,选择uml-2,uml不是万能的,很多场合并不适合使用 1)传统的做法已完全适用,对ooad的要求也不高,项目非常成功,无任何改进的必要 2)开发的系统比较简单,直接用源码配上少量的文字就能解决问题,软件开发文档也无需添加图形来辅助说明 3)开发人员更习惯于直接阅读源码,用源码交流,这样做不影响工作效率和质量 4)开发的系统本身不属于oo方法、uml适用范围,-69-,选择uml-3,很多情况下,推荐使用uml: 1)oo方法是项目决定采用的方法论,是整个项目或产品成功的关键 2)开发人员感觉用源码说明不了真正的问题,希望利用可视化建模语言简化文档,提高交流效率,准确抓住问题本质 3)系统的规模和设计都比较复杂,需要用图形抽象地表达复杂的概念,增强设计的灵活性、可读性和可理解性,以便暴露深层次的设计问题,降

温馨提示

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

评论

0/150

提交评论