CSDN博客

img wangjiong

Classes(2)

发表于2004/9/20 23:19:00  524人阅读

分类: Excerpt of CC2

Constructors
1. Initialize all member data in all constructors, if possible.
2. Initialize data members in the order in which they're declared.
3. Enfore the singleton property by using a a private constructor(It seems like the Singleton pattern in GoF)
4. Enforce the singleton property by using all static member data and reference counting.
5. Prefer deep copies to shallow copies until proven otherwise.
A deep copy of an object is a member-wise copy of the object's member data. A shallow copy typically just points to or refer to a single reference copy.

Reason to Create a Class
1. Model real-world objects
2. Model abstract objects--an object that isn't a concrete, real-world object, but that privides an abstraction of other concrete objects. A good example is the classic Shape object.
3. Reduce complexity. 
Create a class to hide information so that you won't need to think about it.
4. Isolate complexity.
5. Hide implementation details.
6. Limit effects of chagnes.
Isolate areas that are likely to chagne so that the effects of changes are limited to the scope of a sinlge or, at most a few classes.
7. Hide global data.
8. Streamline parameter passing.
If you're passing a parameter among several routines, that might indicate a need to factor those routines into a class that share the parameter as class data.
9. Make central points of control.
It's a good idea to control each task in one place.
10. Facilitate reusable code.
11. Plan for a family of programs.
Seems just like the item 6.
12. Package related operations.
In case in which you can't hide information, share data, or plan for flexibility, you can still package sets of operations into sensible groups such as statistcal functions, string-manipulation routines and so on.
13. To accomplish a specific refactoring.

Classes to Avoid
1. Avoid creating god classes.
2. Eliminate irrelevant classes.
If a class consists only of data but no behavior, ask yourself whether it's really a class.
3. Avoid classes named after verbs.
A class that only behavior but no data is generally not really a class.

0 0

相关博文

我的热门文章

img
取 消
img