模型元素之间的关联(has-a use-a and have-a)

模型元素之间的连接关系有:关联Association、概化Generalization、依赖Dependency、实现Realization、聚合Aggregation、组合Combination。其中,聚合和组合是关联的一种特殊形式
(1)关联Association:用于描述模型元素之间的连接,只要两个模型元素之间存在相互通信的关系,它们之间就存在关联关系。
关联关系可以是单向的,但一般为双向的。
(2)概化Generalization:又称继承,指一个模型元素的所有信息能被另一个模型元素继承。
继承了其它模型元素的模型元素中不仅可以拥有属于自己的信息,而且还拥有了被继承模型元素中的信息。
(3)依赖Dependency:描述两个模型元素之间语义上的连接关系,其中一个模型元素是独立的。另一个是非独立的。
在这种联系中,改变独立元素将影响到非独立元素的语义。
(4)如果模型元素之间的关系具有“整体与部分”的特点,则这种关联称为聚合或组合。
聚合关系用Has a句型表示,组合关系用Be a part of句型表示。
(5)实现Realization:用于表示同一事务的两种描述之间的关系

  • 如果你确定两件对象之间是is-a的关系,那么此时你应该使用继承;比如菱形、圆形和方形都是形状的一种,那么他们都应该从形状类继承而不是聚合。

    如果你确定两件对象之间是has-a的关系,那么此时你应该使用聚合;比如电脑是由显示器、CPU、硬盘等组成的,那么你应该把显示器、CPU、硬盘这些类聚合成电脑类,而不是从电脑类继承。

    类间的关系

    网上关于此类的讨论非常多,发现对于该问题的理解各有各的说法,而各个说法中又相去甚远。通过浏览这些讨论以及对《O'Reilly - UML 2.0 In A Nutshell (2007)》的参考,发表一下自己的看法

    类间关系有很多种,在大的类别上可以分为两种:纵向关系、横向关系。

    纵向关系就是继承关系,它的概念非常明确,也成为OO的三个重要特征之一,这里不过多的讨论。

    横向关系较为微妙,按照UML的建议大体上可以分为四种:

    1. 依赖    (Dependency)
    2. 关联    (Association)
    3. 聚合    (Aggregation)
    4. 组合    (Composition)

    它们的强弱关系是没有异议的:依赖 < 关联 < 聚合 < 组合

    然而它们四个之间的差别却又不那么好拿捏,需要好好体会。

    1. 依赖 (Dependency)
      • UML表示法:虚线 + 箭头
      • 关系:"A uses a B"
      • 编程语言中体现为局部变量、方法的参数或者对静态方法的调用。
      • 此关系最为简单,也最好理解,所谓依赖就是某个对象的功能依赖于另外的某个对象,而被依赖的对象只是作为一种工具在使用,而并不持有对它的引用。
      • 典型的例子很多,比如:
        class Human
        {
            public void breath()
            {
                Air freshAir = new Air();
                freshAir.releasePower();
            }
            public static void main()
            {
                Human me = new Human();
                while(true)
                {
                    me.breath();
                }
            }
        }

        class Air
        {
            public void releasePower()
            {
                //do sth.
            }
        }
      • 释义:一个人自创生就需要不停的呼吸,而人的呼吸功能之所以能维持生命就在于吸进来的气体发挥了作用,所以说空气只不过是人类的一个工具,而人并不持有对它的引用。
    2. 关联 (Association ):
      • UML表示法:实线 + 箭头
      • 关系:" A has a B"
      • 所谓关联就是某个对象会长期的持有另一个对象的引用,而二者的关联往往也是相互的。关联的两个对象彼此间没有任何强制性的约束,只要二者同意,可以随时解除关系或是进行关联,它们在生命期问题上没有任何约定。被关联的对象还可以再被别的对象关联,所以关联是可以共享的。
      • 在编程语言中,关联关系是通过使用成员变量来实现的。
      • 典型的例子很多,比如:
        class Human
        {
            ArrayList friends = new ArrayList();
            public void makeFriend(Human human)
            {
                friends.add(human);
            }
            public static void main()
            {
                Human me = new Human();
                while(true)
                {
                    me.makeFriend(mySchool.getStudent());
                }
            }
        }
      • 释义:人从生至死都在不断的交朋友,然而没有理由认为朋友的生死与我的生死有必然的联系,故他们的生命期没有关联,我的朋友又可以是别人的朋友,所以朋友可以共享。
      • 关联的方向:双向关联(association,----------)通过A对象可以找到B对象,B对象同样可以找到A对象的关联为双向关联。 单向关联(direction-association,-------->)通过A对象可以找到B对象,但通过B对象不能找到A对象的关联为单向关联。
  • 聚合(Aggreation: 
    • UML表示法:空心菱形 + 实线 + 箭头
    • 关系:" A owns a B"
    • 聚合是强版本的关联。它暗含着一种所属关系以及生命期关系。被聚合的对象还可以再被别的对象关联,所以被聚合对象是可以共享的。虽然是共享的,聚合代表的是一种更亲密的关系。
    • 典型的例子很多,比如:
      class Human
      {
          Home myHome;
          public void goHome()
          {
              //在回家的路上
              myHome.openDoor();
              //看电视
          }
          public static void main()
          {
              Human me = new Human();
              while(true)
              {
                  //上学
                  //吃饭
                  me.goHome();
              }
          }
      }
    • 释义:我的家和我之间具有着一种强烈的所属关系,我的家是可以分享的,而这里的分享又可以有两种。其一是聚合间的分享,这正如你和你媳妇儿都对这个家有着同样的强烈关联;其二是聚合与关联的分享,如果你的朋友来家里吃个便饭,估计你不会给他配一把钥匙。
  • 组合(composition
    • UML表示法:实心菱形 + 实线 + 箭头
    • 关系:" A is the whole of  B"
    • 组合是关系当中的最强版本,它直接要求包含对象对被包含对象的拥有以及包含对象与被包含对象生命期的关系。被包含的对象还可以再被别的对象关联,所以被包含对象是可以共享的,然而绝不存在两个包含对象对同一个被包含对象的共享。
    • 典型的例子很多,比如:
      class Human
      {
          Heart myHeart = new Heart();
          public static void main()
          {
              Human me = new Human();
              while(true)
              {
                  myHeart.beat();
              }
          }
      }
    • 释义:组合关系就是整体与部分的关系,部分属于整体,整体不存在,部分一定不存在,然而部分不存在整体是可以存在的,说的更明确一些就是部分必须创生于整体创生之后,而销毁于整体销毁之前。部分在这个生命期内可以被其它对象关联甚至聚合,但有一点必须注意,一旦部分所属于的整体销毁了,那么与之关联的对象中的引用就会成为空引用,这一点可以利用程序来保障。心脏的生命期与人的生命期是一致的,如果换个部分就不那么一定,比如阑尾,很多人在创生后的某个时间对其厌倦便提前销毁了它,可它和人类的关系不可辩驳的属于组合。
      在UML中存在一种特例,就是允许被包含对象在包含对象销毁前转移给新的对象,这虽然不自然,但它给需要心脏移植的患者带来了福音。

     

    原文地址http://xuesky.cnblogs.com/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This Cortex-A Series Programmer’s Guide is protected by copyright and the practice or implementation of the information herein may be protected by one or more patents or pending applications. No part of this Cortex-A Series Programmer’s Guide may be reproduced in any form by any means without the express prior written permission of ARM. No license, express or implied, by estoppel or otherwise to any intellectual property rights is granted by this Cortex-A Series Programmer’s Guide. Your access to the information in this Cortex-A Series Programmer’s Guide is conditional upon your acceptance that you will not use or permit others to use the information for the purposes of determining whether implementations of the information herein infringe any third party patents. This Cortex-A Series Programmer’s Guide is provided “as is”. ARM makes no representations or warranties, either express or implied, included but not limited to, warranties of merchantability, fitness for a particular purpose, or non-infringement, that the content of this Cortex-A Series Programmer’s Guide is suitable for any particular purpose or that any practice or implementation of the contents of the Cortex-A Series Programmer’s Guide will not infringe any third party patents, copyrights, trade secrets, or other rights. This Cortex-A Series Programmer’s Guide may include technical inaccuracies or typographical errors. To the extent not prohibited by law, in no event will ARM be liable for any damages, including without limitation any direct loss, lost revenue, lost profits or data, special, indirect, consequential, incidental or punitive damages, however caused and regardless of the theory of liability, arising out of or related to any furnishing, practicing, modifying or any use of this Programmer’s Guide, even if ARM has been advised of the possibility of such damages. The information provided herein is subject to U.S. export control laws, including the U.S. Export Administration Act and its associated regulations, and may be subject to export or import regulations in other countries. You agree to comply fully with all laws and regulations of the United States and other countries (“Export Laws”) to assure that neither the information herein, nor any direct products thereof are; (i) exported, directly or indirectly, in violation of Export Laws, either to any countries that are subject to U.S. export restrictions or to any end user who has been prohibited from participating in the U.S. export transactions by any federal agency of the U.S. government; or (ii) intended to be used for any purpose prohibited by Export Laws, including, without limitation, nuclear, chemical, or biological weapons proliferation. Words and logos marked with ® or ™ are registered trademarks or trademarks of ARM Limited, except as otherwise stated below in this proprietary notice. Other brands and names mentioned herein may be the trademarks of their respective owners. Copyright © 2011, 2012 ARM Limited, 110 Fulbourn Road Cambridge, CB1 9NJ, England This document is Non-Confidential but any disclosure by you is subject to you providing notice to and the

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值