Classes (2)

xiaoxiao2021-03-06  51

Constructors1. 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 aa 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 reason.

Reason to Create a Class1 Model real-world objects2 Model abstract objects -.. An object that is not 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 will not 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 Infor mation, 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 Avoid1. 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.

转载请注明原文地址:https://www.9cbs.com/read-114916.html

New Post(0)