Chapter 15 面向对象的信息隐藏

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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
《计算与软件工程II 》
Ch15 “面向对象”的信息隐藏
丁二玉 南京大学软件学院
Main Contents
1. Encapsulation VS Information Hiding 2. What should be hiding? 3. How to hiding change?
Information Hiding
See chapter 16 Iterator Pattern
References and Collection Data-Type !
Encapsulate other objects
Collaboration Design
Composition; delegation
Encapsulate type(subclass)
Each module hides the implementation of an important design decision so that only the constituents of that module know the details
公开外部表现 / 隐藏内部实现
Encapsulation
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
The implementation is hidden in the capsule.
The implementation hiding means that data can only be manipulated, that is updated, within the class, but it does not mean hiding interface data.
implemented
Encapsulate Data
If needed, use Accessors and Mutators, Not
Public Members Accessors and Mutators is meaningful behavior
Constraints, transformation, format…
LSP
pointers to superclasses or interfaces;
All derived classes must be substituteable
for their base class
Encapsulate Change (or vary)
Identify the aspects of your application that may change (or vary) and separate them from what stays the same.
Take the parts that change( vary ) and encapsulate them, so that later you can alter or extend the parts that vary without affecting the parts that don’t.
public void setSpeed(double newSpeed) { if (newSpeed < 0) { sendErrorMessage(...); newSpeed = Math.abs(newSpeed); } speed = newSpeed;
}
Encapsulate structures
New View of EncaБайду номын сангаасsulation
Old/Beginner/Implementation view of encapsulation: hiding data inside an object
New view: hiding anything, including:
implementation derived classes design details
Abstraction
An abstraction focuses on the outside view of an object and separates an object’s behavior from its implementation
Encapsulation
Classes should not expose their internal implementation details
See DIP and OCP Later
Encapsulate Implementation Detail
Data Structure Other object Type Change/vary …
Principle #1: Minimize The Accessibility of Classes and Members
Main Contents
1. Encapsulation VS Information Hiding 2. What should be hiding? 3. How to hiding change?
Encapsulation Correctly ——ADT
ADT = Abstract Data Type
相关文档
最新文档