南京大学-软件工程-15 面向对象信息隐藏
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
What should be hiding?
2.
How to hiding change?
Encapsulation (1)
Encapsulation allows the programmer to group data and the subroutines that operate on them together in one place
《计算与软件工程II 》
Ch15 “面向对象”的信息隐藏
丁二玉 南京大学软件学院
Information Hiding
Each module hides the implementation of an important design decision (secrets) so that only the constituents of that module know the details
Data (implementation) Structure (implementation) Other object (implementation) Type (derived classes) Change/vary (design details) …
Encapsulation Correctly ——ADT
The implementation decisions made by the designer
when implementing the module designed to hide the primary secret 变化;
3
Main Contents
1.
1.
Encapsulation
Design Secrets need to hide…
Primary Secret: Responsibility Change
Hidden information that was specified to the software designer From SRS
Secondary Secret: Implementation Change
ADT = Abstract Data Type
A concept, not an implementation A set of (homogeneous) objects together with a set of operations on those objects No mention of how the operations are implemented data abstraction: group data and operation Type: hiding implementation, make usage correctly
Responsibility
Encapsulation (2)
hide irrelevant details from the user a class can be divided into two parts, the user interface and the implementation. The interface is the visible surface of the capsule.
describes the essential characteristics of objects of the class which are visible to the exterior world The implementation hiding means that data can only be manipulated, that is updated, within the class, but it does not mean hiding interface data.
public void setSpeed(double newSpeed) { if (newSpeed < 0) { sendErrorMessage(...); newSpeed = Math.abs(newSpeed); } speed = newSpeed; }
Encapsulation = data abstraction + tyHale Waihona Puke Baidue
Why type?
A type may be viewed as a set of clothes (or a suit of armor) that protects an underlying untyped representation from arbitrary or unintended use. It provides a protective covering that hides the underlying representation and constrains the way objects may interact with other objects. In an untyped system untyped objects are naked in that the underlying representation is exposed for all to see.
The implementation is hidden in the capsule.
New View of Encapsulation
Old/Beginner/Implementation view of encapsulation: hiding data inside an object New view: hiding anything, including:
Encapsulate Data
If needed, use Accessors and Mutators, Not
Public Members Accessors and Mutators is meaningful behavior
Constraints, transformation, format…
2.
How to hiding change?
Encapsulation (1)
Encapsulation allows the programmer to group data and the subroutines that operate on them together in one place
《计算与软件工程II 》
Ch15 “面向对象”的信息隐藏
丁二玉 南京大学软件学院
Information Hiding
Each module hides the implementation of an important design decision (secrets) so that only the constituents of that module know the details
Data (implementation) Structure (implementation) Other object (implementation) Type (derived classes) Change/vary (design details) …
Encapsulation Correctly ——ADT
The implementation decisions made by the designer
when implementing the module designed to hide the primary secret 变化;
3
Main Contents
1.
1.
Encapsulation
Design Secrets need to hide…
Primary Secret: Responsibility Change
Hidden information that was specified to the software designer From SRS
Secondary Secret: Implementation Change
ADT = Abstract Data Type
A concept, not an implementation A set of (homogeneous) objects together with a set of operations on those objects No mention of how the operations are implemented data abstraction: group data and operation Type: hiding implementation, make usage correctly
Responsibility
Encapsulation (2)
hide irrelevant details from the user a class can be divided into two parts, the user interface and the implementation. The interface is the visible surface of the capsule.
describes the essential characteristics of objects of the class which are visible to the exterior world The implementation hiding means that data can only be manipulated, that is updated, within the class, but it does not mean hiding interface data.
public void setSpeed(double newSpeed) { if (newSpeed < 0) { sendErrorMessage(...); newSpeed = Math.abs(newSpeed); } speed = newSpeed; }
Encapsulation = data abstraction + tyHale Waihona Puke Baidue
Why type?
A type may be viewed as a set of clothes (or a suit of armor) that protects an underlying untyped representation from arbitrary or unintended use. It provides a protective covering that hides the underlying representation and constrains the way objects may interact with other objects. In an untyped system untyped objects are naked in that the underlying representation is exposed for all to see.
The implementation is hidden in the capsule.
New View of Encapsulation
Old/Beginner/Implementation view of encapsulation: hiding data inside an object New view: hiding anything, including:
Encapsulate Data
If needed, use Accessors and Mutators, Not
Public Members Accessors and Mutators is meaningful behavior
Constraints, transformation, format…