Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Animal
- {
- public:
- Noise warningNoise();
- Noise pleasureNoise();
- private:
- AnimalType type;
- };
- Noise Animal::warningNoise()
- {
- switch(type)
- {
- case Cat: return Hiss;
- case Dog: return Bark;
- }
- }
- Noise Animal::pleasureNoise()
- {
- switch(type)
- {
- case Cat: return Purr;
- case Dog: return Bark;
- }
- }
- class Animal
- {
- public:
- virtual Noise warningNoise() = 0;
- virtual Noise pleasureNoise() = 0;
- };
- class Cat: public Animal
- {
- // Compiler forces you to define both method.
- // Otherwise you can't have a Cat object
- // All code local to the cat belongs to the cat.
- };
- void MyProcedure(int p_iCommand, void *p_vParam)
- {
- // A LOT OF CODE ???
- // each case has a lot of code, with both similarities
- // and differences, and of course, casting p_vParam
- // into something, depending on hoping no one
- // did a mistake, associating the wrong command with
- // the wrong data type in p_vParam
- switch(p_iCommand)
- {
- case COMMAND_AAA: { /* A LOT OF CODE (see above) */ } break ;
- case COMMAND_BBB: { /* A LOT OF CODE (see above) */ } break ;
- // etc.
- case COMMAND_XXX: { /* A LOT OF CODE (see above) */ } break ;
- case COMMAND_ZZZ: { /* A LOT OF CODE (see above) */ } break ;
- default: { /* call default procedure */} break ;
- }
- }
- void MyProcedure(int p_iCommand, void *p_vParam)
- {
- switch(p_iCommand)
- {
- // Only one case. Isn't it cool?
- case COMMAND:
- {
- Command * c = static_cast<Command *>(p_vParam) ;
- c->process() ;
- }
- break ;
- default: { /* call default procedure */} break ;
- }
- }
- [+] Command
- |
- +--[+] CommandServer
- | |
- | +--[+] CommandServerInitialize
- | |
- | +--[+] CommandServerInsert
- | |
- | +--[+] CommandServerUpdate
- | |
- | +--[+] CommandServerDelete
- |
- +--[+] CommandAction
- | |
- | +--[+] CommandActionStart
- | |
- | +--[+] CommandActionPause
- | |
- | +--[+] CommandActionEnd
- |
- +--[+] CommandMessage
- class CommandAction : public Command
- {
- // etc.
- virtual void process() // overriding Command::process pure virtual method
- {
- this->processBefore() ;
- this->processWhile() ;
- this->processAfter() ;
- }
- virtual void processBefore() = 0 ; // To be overriden
- virtual void processWhile()
- {
- // Do something common for all CommandAction objects
- }
- virtual void processAfter() = 0 ; // To be overriden
- } ;
- class CommandActionStart : public CommandAction
- {
- // etc.
- virtual void processBefore()
- {
- // Do something common for all CommandActionStart objects
- }
- virtual void processAfter()
- {
- // Do something common for all CommandActionStart objects
- }
- } ;
- If (weapon is a rifle) then //Code to attack with rifle else
- If (weapon is a plasma gun) //Then code to attack with plasma gun
- weapon.attack()
- switch(obj.type): {
- case 1: cout << "Type 1" << obj.foo <<...; break;
- case 2: cout << "Type 2" << ...
- cout << object.toString();
- interface A{
- int foo();
- }
- final class B implements A{
- int foo(){ print("B"); }
- }
- final class C implements A{
- int foo(){ print("C"); }
- }
- class A{
- int foo(){print("A");}
- }
- class B extends A{
- int foo(){print("B");}
- }
- class C extends B{
- int foo(){print("C");}
- }
- ...
- class Z extends Y{
- int foo(){print("Z");
- }
- main(){
- F* f = new Z();
- A* a = f;
- a->foo();
- f->foo();
- }
- A* a = new Z;
- A a2 = *a;
- a->foo();
- a2.foo();
Add Comment
Please, Sign In to add comment