Bob Tarr: Some Object-Oriented Design Principles(1): Minimize The Accessibility of Classes and Members
来源:互联网 发布:sql工程师 编辑:程序博客网 时间:2024/06/07 16:12
The Meaning of Abstraction
- Tony Hoare: “Abstraction arises from a recognition of similarities between certain objects, situations, or processes in the real world, and the decision to concentrate upon those similarities and to ignore for the time being the differences.”
- Grady Booch: “An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.”
- Abstraction is one of the fundamental ways to deal with complexity
- An abstraction focuses on the outside view of an object and separates an object’s behavior from its implementation
Encapsulation
- Grady Booch: “Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation.”
- Craig Larman: “Encapsulation is a mechanism used to hide the data, internal structure, and implementation details of an object. All interaction with the object is through a public interface of operations.”
- Classes should be opaque
- Classes should not expose their internal implementation details
Information Hiding In Java
- Use private members and appropriate accessors and mutators wherever possible
- For example:
Replace
public double speed;
with
public double speed;
with
private double speed;
public double getSpeed() {
return(speed);
}
public void setSpeed(double newSpeed) {
speed = newSpeed;
}
Use Accessors and Mutators, Not Public Members
- You can put constraints on values
public void setSpeed(double newSpeed) {
if (newSpeed < 0) {
sendErrorMessage(...);
newSpeed = Math.abs(newSpeed);
}
speed = newSpeed;
}
if (newSpeed < 0) {
sendErrorMessage(...);
newSpeed = Math.abs(newSpeed);
}
speed = newSpeed;
}
- If users of your class accessed the fields directly, then they would each be responsible for checking constraints
- You can change your internal representation without changing the interface
// Now using metric units (kph, not mph)
public void setSpeedInMPH(double newSpeed) {
speedInKPH = convert(newSpeed);
}
public void setSpeedInKPH(double newSpeed) {
speedInKPH = newSpeed;
}
public void setSpeedInMPH(double newSpeed) {
speedInKPH = convert(newSpeed);
}
public void setSpeedInKPH(double newSpeed) {
speedInKPH = newSpeed;
}
- You can perform arbitrary side effects
public double setSpeed(double newSpeed) {
speed = newSpeed;
notifyObservers();
}
speed = newSpeed;
notifyObservers();
}
- If users of your class accessed the fields directly, then they would each be responsible for executing side effects
- Bob Tarr: Some Object-Oriented Design Principles(1): Minimize The Accessibility of Classes and Members
- Principles of Object-Oriented Design
- Principles Of Object Oriented Design
- [转]Object Oriented Design Principles
- Object-Oriented Design Knowledge: Principles, Heuristics And Best Practices
- Part1:A Brief Study of Classes and Object-oriented Programming.
- the design principles and code analysis of hibernate lazy loading
- The principles of program design
- The Principles of Adaptive Design
- Object Oriented Design Principles 面向对象设计原则
- The Essence of Object-Oriented Programming with Java and UML
- Data Structures and Algorithms with Object-Oriented Design Patterns (1)
- Object Oriented Design -- Data and Algorithm Separation (1)
- The Principles of Beautiful Web Design [ILLUSTRATED]
- The 13 Basic Principles of Gameplay Design
- The basic principles of design pattern
- Fundamentals of Object-Oriented Design in UML
- Object Oriented Analysis and Design Using UML
- ASSERT()和assert()的区别是什么?
- 数据库设计方法、规范与技巧
- 使用ASP.NET实现Friendly URL的最简单方法
- 正则表达式
- php读mysql中文乱码问题解决方法
- Bob Tarr: Some Object-Oriented Design Principles(1): Minimize The Accessibility of Classes and Members
- 中国外交部和中国驻英国使、领馆联系办法
- Principles of Object-Oriented Design
- http://tb.blog.csdn.net/TrackBack.aspx?PostId=1571046
- 我的MSDN Blog正式开张,欢迎大家访问 [ http://blogs.msdn.com/yizhang/ ]
- Windows常用运行命令
- 浅谈Asp.net 多层架构中的变量引用与传递
- VC中使用ListCtrl经验总结
- 用Asp隐藏文件路径实现防盗链