Chapter 6 working classes (page 162-197)
6.1 class foundations: abstract data types (ADTs)
Class basics: abstract data type (ADTs)
ADT refers to a set of data and operations performed on the data.
6.2 ADTs and Classes
Good class interfaces
Implement each abstract data type with its own class.
Class also involves two additional concepts: inheritance and polymorphism. Therefore, another way to consider a class is to regard it as an abstract data type plus the concepts of inheritance and polymorphism.
Abstract and cohesion
An abstract class interface usually has a high cohesion. Classes with strong cohesion tend to be very abstract.
Good encapsulation good encapsulation
Encapsulation is a more abstract concept. Abstraction manages complexity by providing a model that allows you to ignore implementation details, while encapsulation forces you to see details.
Encapsulation principles:
1. Restrict the accessibility of classes and members as much as possible (Accessibility)
2. Avoid putting private implementation details into the class interface.
3. Pay attention to the close coupling relationship)
A. Restrict the accessibility of classes and members as much as possible.
B. Avoid peer classes because they are tightly coupled.
C. Declare the data in the class as private rather than protected to reduce the coupling between the Birth class and the base class.
D. Avoid exposing member data in the public interface of the class.
E. Be vigilant against semantic damages to encapsulation.
F. Detect the Demeter rule
6.3 design and implementation issues
Design and implementation issues
1. Include ("there is ......" ) Containment
Alert to classes with more than 7 data members
Research shows that the number of discrete projects that people can remember when doing other things
Is 7 +-2. If a class contains more than 7 data members, consider not to break it into several smaller classes.
2. Inheritance ("It's ......" ) Inheritance
The concept of inheritance is that one class is the specialization of another class ).
The purpose of inheritance is to combine the common elements in a base class, which helps avoid repeated elements in multiple places.Code And data.
A. either use inheritance and describe it in detail, or do not use it. Because Program It increases complexity, so it is a dangerous technology.
B. Follow the liskov replacement principle
Unless the derived class is a special base class, it should not be inherited from the base class.
In summary, the derived class must be used through the interface of the base class, and the user does not need to know the difference between the two.
C. Ensure that only the part to be inherited is inherited.
D. Do not overwrite a member function that cannot be overwritten.
Both C ++ and Java allow "Override" of member functions that cannot be overwritten. For example:
If a member function is private in the base class, its derived class can create a member function with the same name.
This function is confusing, because it seems to be a polymorphism, but in fact it is not, just the same name.
E. Place the shared interfaces, data, and operations to the highest possible position in the inheritance tree.
F. Only one instance class is questionable.
Only one instance is needed, which may indicate that the objects and classes are confused in the design.
G. It is doubtful that there is only one base class of the derived class.
Whenever I see a base class with only one derived class, I suspect that a programmer is "Designing in advance"-that is, trying to predict the future needs, and often do not really understand what the future needs. That is to say, do not create any inheritance structures that are not absolutely necessary.
H. If a subprogram is overwritten after the derivation, but no operation is performed in the subprogram, this situation is doubtful. This usually indicates that the design of the base class is incorrect.
I. Avoid putting the inheritance system too deep.
The object-oriented programming method provides a large number of techniques that can be used to manage complexity. However, every powerful tool has its own dangers, and even some object-oriented technologies have a tendency to increase complexity.
We recommend that you limit the hierarchy to six layers.
J. Use polymorphism whenever possible to avoid a large number of type checks.
K. Make all data private (rather than protected)
"Inheritance destroys encapsulation ."
Multi-inheritance multiple inheritance
Although some experts recommend that multiple inheritance be widely used, the purpose of multiple inheritance is to define
"Mixins" is a simple class that can add a set of attributes to an object.
Hybrid systems require multi-inheritance, but as long as they are completely independent, they do not cause typical diamond-inheritance issues.
Java and VB recognize the value of a mixture, because they allow multi-interface inheritance, but can only inherit the implementation of one class.
C ++ supports multiple inheritance of interfaces and implementations at the same time.
Before deciding to use multi-inheritance, programmers should carefully consider other alternatives and carefully assess the impact it may have on system complexity and comprehensibility.
Why are there so many rules for inheritance?
Why are there so many inheritance rules?
To sum up when inheritance can be used and when inheritance should be used:
1. if multiple classes share data rather than behavior, you should create shared objects that can be contained in these classes.
2. If multiple classes share behavior rather than data, let them inherit from the common base class and define the shared subroutine in the base class.
3. If multiple class machines share data, they should inherit from a common base class and define the shared data and subprograms in the base class.
4. Use inheritance when you want to control interfaces by the base class; use include when you want to control interfaces by yourself.
Member function and Data
Member functions and data members
Suggestions for effectively implementing member functions and data members:
1. Minimize the number of subprograms
2. It is forbidden to generate implicit member functions and operators you do not need.
3. Reduce the number of different subprograms called by the class
4. Indirect calls to other subprograms should be minimized
5. In general, efforts should be made to minimize the scope of mutual cooperation between classes.
Minimize: A. the type of the instantiated object. B. Number of different subprograms directly called on the instantiated object. C. Number of subprograms that call objects returned by other objects.
constructors
constructor
1. If possible, all data members should be initialized in all constructors.
initializing all data members in all constructors is a defense-style Programming Practice.
2. Use the private constructor to forcibly implement the single-piece attribute. (Singleton property)
If You Want To define a class and require that it only have one unique instance object, you can hide all constructors of this class, then a static getinstance ()
subroutine is provided to access the unique instance of the class. Example: The following Java code:
public class maxid
{< br> // constructors and Destructors
private maxid (){...}
// public routines
Public static maxid getinstance ()
{< br> return m_instance;
}< br> // Private Members
Private Static final maxid m_instance = new maxid ();
...
}< br> 3. deep copies is preferred; shallow copies is used unless the argument is feasible.
Reason to create a class
Reason for creating a class
The following lists some reasonable reasons for creating a class:
1. modeling objects in the real world
2. Create an abstract object.
3. reduce complexity
4. Isolation complexity
5. Hide Implementation Details
6. Restrict the impact scope of changes
7. Hide global data
8. Smooth parameter transmission.
9. Create a Central Control Point
10. Make code easier to reuse.
11. make plans for the program family
12. package related operations together
13. Implement a specific refactoring
Classes to avoid
Classes to be avoided
The following are some classes that should be avoided:
1. Avoid creating a omnipotent class (God class)
2. Eliminate irrelevant classes
3. avoid naming classes with verbs.
A class with only behavior but no data is often not a real class.
Consider changing a class such as databaseinitialization or stringbuilder into a subroutine of another class.
6.5 language-specific issue
And detailsProgramming LanguageRelated Questions
For example, how to override a member function in a derived class to realize polymorphism.
In Java, all methods can be overwritten by default, unless final is added.
In C ++, all methods cannot be overwritten by default, unless virtual is added.
6.6 beyond classes: packages
Beyond class: Package
Class is the best way for programmers to implement modularization.
Key Point Summary: Key Point
1. The class interface should provide consistent abstraction. Many problems are caused by violation of this principle.
2. The class interface should hide some information, such as a system interface, a design decision, or some implementation details.
3. Inclusion is often more desirable than inheritance-unless you need to model a "is a/is a" link.
4. inheritance is a useful tool, but it increases complexity, which violates the primary technical mission of software.
---- Management complexity.
5. Class is the preferred tool for managing complexity. This goal can be achieved only by paying enough attention to the design class.