글
Part 2 - Chapter 6. Class Handling
Abstract Data Type
Once you understand ADT, you can make easy class to make in the beginning and to change in the future.
ADT is not int or float type which are defined ( or fixed ). It's a data type whice is made by developer such as struct or class.
The advantages :
-
Changing doesn't influence the whole program.
-
It's easy to advance a program.
-
Apparently It's easy to understand. and so on...
Better Class Interface
-
To make consistent interfaces.
Inappropriate Example
-
- class EmployeeCensus : public ListContainer{
-
public:
-
void AddEmployee( Employee employee );
-
void RemoveEmployee( Employee employee );
-
Employee NextItemInList();
-
Employee FirstItem();
Employee LastItem();
- }
In this case, 2 functions are about Employee. 3 functions are about List. It's not consistent abstract.
Appropriate Example
- class EmployeeCensus : public ListContainer{
-
public:
-
void AddEmployee( Employee employee );
-
void RemoveEmployee( Employee employee );
-
Employee NextEmployee();
-
Employee FirstEmployee();
Employee LastEmployee();
-
private:
-
ListContainer m_EmployeeList;
- }
The Abstraction is all about Employee.
2 To provide a pair of service which has an opposite function.
If there is a function which turn on the radio, we might need a function which turn off the radio.
But make sure that the opposite function is needed. So you should check it.
-
During maintenance, be careful to mess abstraction.
SqlQuery GetOueryToCreateNewEmployee() const; ~~~
Its' level of abstraction is lower then Employee Class's. It devastates an abstraction.
To persue the convenience of reading not the convenience of writing codes.
It takes more time to read codes than to write codes
If you make a program through interfaces, It's gonna break capsulation. then It's gonna break abstraction as well.
-> make a program through priavate functions.
To be careful the class including more than 7 data members.
If there is a class which has more than 7 data members, you consider whether u should devide the class.
To design for inheritance. If not, make it impossible to inherit. : C++ -> non-virtual, Java -> final
To be doubt if there is base class which has a derived class. Make it simple to a class.
If there is a class which has functhion doing nothing, u should change the origin.
ex) Cat Class and Scratch() <- ScratchlessCat and Scratch()- doing nothing. : It would be a problem.
Cat Class includes Claws Class. It's better.
To make data private instead of protected. - Joshua Bloch said "Inheritance break capsulation"
The case of using switch ~ case , U should use it when cases are about totally different classes or actions.
It's an example that seems to be changed to polymorphism.
-
switch( shape.type ){
-
case Shape_Circle:
-
shape_DrawCircle();
-
break;
-
case Shape_Square:
-
shape.DrawSquare();
-
break;
-
...
-
}
It's an example that seems unable to be changed to polymorphism.
-
switch( ui.Command() )
-
{
-
case Command_OpenFile:
-
OpenFile();
-
break;
-
case Command_Print:
-
print();
-
break;
-
....
-
}
instance => which has an object.
If there is a class without members and with only actions, u should avoid to make that class.
In JAVA, All rutines are possible to override and to prevent inheritance to use a final routine.
In C++, basically it's impossible to override. u should 선언 virtual for inheritance.
'책 정리 > Code Complete 2nd' 카테고리의 다른 글
[Code Complete 2nd/e] Words - 1 (0) | 2009.03.18 |
---|---|
Code Complete - Eng Version Doc (0) | 2009.03.18 |
Part 2 - Chapter 8 Defensive Programming (0) | 2009.03.09 |