View Single Post
  #8 (permalink)  
Old 09-22-2007, 03:18 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 293
vijayanand is on a distinguished road
Default Re: Differennt types of class

action class

The simplest and most obvious way to specify an action in C++ is to write a function. However, if the action has to be delayed, has to be transmitted 'elsewhere' before being performed, requires its own data, has to be combined with other actions, etc then it often becomes attractive to provide the action in the form of a class that can execute the desired action and provide other services as well. Manipulators used with iostreams is an obvious example.

Explanation:
A common form of action class is a simple class containing just one virtual function.
PHP Code:
 class Action
       
{
               public:
                    
virtual int do_it( int )=0;
                    
virtual ~Action( );
         } 
Given this, we can write code say a member that can store actions for later execution without using pointers to functions, without knowing anything about the objects involved, and without even knowing the name of the operation it invokes. For example:
PHP Code:
class write_file : public Action
     
{
              
Filef;
              public:
                  
int do_it(int)
                 {
                       return 
fwrite( ).suceed( );
                 }
      };
     class 
error_message: public Action
     
{
                
response_box db(message.cstr( ),"Continue","Cancel","Retry");
                switch (
db.getresponse( ))
                {
                        case 
0: return 0;
                        case 
1abort();
                        case 
2current_operation.redo( );return 1;
                 }
      }; 
A user of the Action class will be completely isolated from any knowledge of derived classes such as write_file and error_message.
__________________

J.Vijayanand
Reply With Quote