Saturday, June 16, 2007

Treat class design as type design


Class designer = type designer.

How should objects fo your new type be created and destroyed ? --this effects your constructors, destructors, memory allocation and deallocation( operator new, new[], delete , delete[]) etc. Shud people call a static fn / normal creation/singleton/ restricted access (??) of some kind , who is responsible for releasing allocated resources, clients or class is self managing, centralized resource release, reference counting or auto ptr semantics when things are copied ?

How should object initialization differ from object assignment ? -- this answers the differences that will be there between your constructor and your assignment operators. think reference counting, no assignment, deep copy etc ??

What does it mean for objects of your new type to be passed by value  -- remember pass by value is defined by the implementation of your copy constructor.

What are the restrictions on legal values for your new type : --- this determines the invariants that your class needs to maintain, invariants determine your error checking and exceptions your functions throw and exception specifications of your functions

Does your new type fit into an inheritance graph ---if you are inheriting than you are constrained by your parent classes ( their virtual,abstract and non virtual functions and destructors) and if other classes are going to inherit from you than do you want to have virtual/abstract/non virtual functions and destructors ?

What kind of type conversion are allowed for your new type --- do you want to define any implicit conversion operators. And explicit conversion functions

What operators and functions make sense for the new type --- what functions you want to declare, shud they be member /friend/outside/ in same name space/public functions/private functions/ etc. Does it make sense to nest your class inside another class

What standard functions should be disallowed -- declare these private, e.g. may copy in network socket connection management class etc)

What is the undeclared interface of your new type ---what guarantees it offers w.r.t performance, exception safety, resource usage ( locks , dynamic memory), these impose restraints on your implementation.

How general is your new type : are you defining a new type or a whole new family of types, if you defined a family of types than you want to define a new class template not just a class

Is a new type really what you need ---- if purpose of new class is just old class + more functionality…then you can maybe use some non member functions or templates to get the work done


Its hard to design smart classes..but worth it.

Things to remember:
Class design is type design. Before defining a new type , be sure to consider all the issues discussed in this item

I need to reread this item and get a hang on it





--
Regards,
Umesh Kumar

http://worthyarticles.blogspot.com/
http://learnbooks.blogspot.com/
http://cplusplusnotes.blogspot.com/

No comments: