Part 2 - Chapter 6. Class Handling

책 정리/Code Complete 2nd 2009. 3. 8. 17:06

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 :

  1. Changing doesn't influence the whole program.

  2. It's easy to advance a program.

  3. Apparently It's easy to understand. and so on...

 

Better Class Interface

  1.  To make consistent interfaces.

    Inappropriate Example

    1. class EmployeeCensus : public ListContainer{
    2. public:

    3. void AddEmployee( Employee employee );

    4. void RemoveEmployee( Employee employee );

    5. Employee NextItemInList();

    6. Employee FirstItem();

      Employee LastItem();

    7. }

In this case, 2 functions are about Employee. 3 functions are about List. It's not consistent abstract.

 

  Appropriate Example

  1. class EmployeeCensus : public ListContainer{
  2. public:

  3. void AddEmployee( Employee employee );

  4. void RemoveEmployee( Employee employee );

  5. Employee NextEmployee();

  6. Employee FirstEmployee();

    Employee LastEmployee();

  7. private:

  8. ListContainer m_EmployeeList;

  9. }

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.

 

  1. 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.

  1. switch( shape.type ){

  2. case Shape_Circle:

  3. shape_DrawCircle();

  4. break;

  5. case Shape_Square:

  6. shape.DrawSquare();

  7. break;

  8. ...

  9. }

 

 It's an example that seems unable to be changed to polymorphism.

  1. switch( ui.Command() )

  2. {

  3. case Command_OpenFile:

  4. OpenFile();

  5. break;

  6. case Command_Print:

  7. print();

  8. break;

  9. ....

  10. }

 

 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

설정

트랙백

댓글