Modelling Classes and Relationships 课件
Target:
- 确立和设计 class 和 relationships
- 正确使用 UML 表达 class 和 relationships
- 开发和研究 Class Diagrams
第一步(写代码之前)
设计代码系统及其步骤
- 建立 class => 名词提取
- 建立 class relationships => 正确提取关键词
- has-a relationship
- is-a relationship
- can-do relationship
- 调整 class 和 relationship 使其适应整体代码布局
- 开发 Class Diagram => 合并 class 和 relationship
- Represent using an accepted notation => UML
- ==> Unified Modeling Language (UML):
- a graphical modelling language that can be used to represent object oriented analysis, design and implementation.
实例:
This game involves a number of characters, like the player, their allies, and their enemies. Some of these characters walk around the world, and some allow the player to interact with them.
There are also other objects in the game like keys, blocks, and power-ups, some of which can be interacted with, some that can be collected/picked up, and some that can be “used” after being picked up.
Some objects in the game have health and can “die”, while some are killed or removed instantly. Other objects can attack and defend other
characters.
提取名词: game characters player
allies enemies world
objects keys blocks
power-ups health-
设计 class:
- 建立 Class
- 建立 relationships
- 是否存在可作为 superclass 的 元素
- 调整 class 和 relationships
- Import into UML( Class and Attributes )
- class representation
- 关于 attributes 的详细说明(一个都不能少)
- Policy
- Method attributes
- Abstract
- 斜体字表示 abstract
- Import into UML( Relationships )
- 关系种类
- Association: A link indicating one class contains an attribute that is itself
a class.- has-a relationship
- delegate task (任务委托)
- 黑色实线表示
- 实例: GameObject 和 Position
- 虽然两者之间没有从属关系
- 但 GameObject 中直接创建了 Position 的 instance variable.
- Properties of Association
- Name of Association
- Role labels
- Directionality
- Unidirectional( hierarchical树状, ownership单向)
- Bidirectional( co-operation双向 )
- Multiplicity
- Type
- Plain association (正常 group )
- Aggregation( 聚集状态 ): where one class “has” another class, but both exist independently
- 如果一方被删除,另一方不会受到任何影响(不会被删除)
- 作为其他东西的数据得以保存( 另投他人 )
- Composition( 合体状态 ): indicating one class cannot exist without the other
- 不可单独存在: 即一方消失自己也不复存在
- 打比方说: 一个人死了, 那他的身体部位也一定死了
- Generalization ( Inheritance ): 不是常规意义的 Inheritance , superclass 必须是abstract
- Realization ( Interfaces ):
- Interface: Declares a set of constants and/or methods that define the behaviour of an object.
- Can only contain constants and abstract methods( final 和 abstract methods)
- Interfaces represent a “Can do” relationship
- Classes that implement an interface can do all the actions defined by the interface
- Interface names are generally called <...>able, and relate to an action
- Methods never have any code
- All methods are implied to be abstract
- All attributes are implied to be static final
- All methods and attributes are implied to be public
- !! Extending Interfaces
- Forms the same “Is a” relationship ( Inheritance )
- Used to add additional, specific behaviour
- Dependency: Dependency represents a weak relationship between classes
- 其实表达的是影响的传递: 如 when a method in a class has another class as one of the input parameters, this results in a dependency relationship.
- 与 Assocation 的区别:
- 不需要成为对方的 attribute
- 只需要作为 input 参与构造
- Association: A link indicating one class contains an attribute that is itself
- 关系种类
- UML Drawing Tools