Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function doWork(object x)
- {
- if (x is a type of Apple)
- {
- x.Eat();
- } else if (x is a type of Orange)
- {
- x.Peel();
- x.Eat();
- }
- }
- class FruitFactory
- {
- Fruit GetMeMoreFruit(typeOfFruit)
- {
- switch (typeOfFruit)
- ...
- ...
- }
- }
- class X {
- public:
- int get_type(); /* Or an enum return type or similar */
- ...
- };
- void eat(X& x) {
- switch (x.get_type()) {
- TYPE_A: eat_A(x); break;
- TYPE_B: eat_B(x); break;
- TYPE_C: eat_C(x); break;
- }
- }
- void drink(X& x) {
- switch (x.get_type()) {
- TYPE_A: drink_A(x); break;
- TYPE_B: drink_B(x); break;
- TYPE_C: drink_C(x); break;
- }
- }
- void be_merry(X& x) {
- switch (x.get_type()) {
- TYPE_A: be_merry_A(x); break;
- TYPE_B: be_merry_B(x); break;
- TYPE_C: be_merry_C(x); break;
- }
- }
- class Base {
- virtual void eat() = 0;
- virtual void drink() = 0;
- virtual void be_merry() = 0;
- ...
- };
- class A : public Base {
- public:
- virtual void eat() { /* Eat A-specific stuff */ }
- virtual void drink() { /* Drink A-specific stuff */ }
- virtual void be_merry() { /* Be merry in an A-specific way */ }
- };
- class B : public Base {
- public:
- virtual void eat() { /* Eat B-specific stuff */ }
- virtual void drink() { /* Drink B-specific stuff */ }
- virtual void be_merry() { /* Be merry in an B-specific way */ }
- };
- class C : public Base {
- public:
- virtual void eat() { /* Eat C-specific stuff */ }
- virtual void drink() { /* Drink C-specific stuff */ }
- virtual void be_merry() { /* Be merry in a C-specific way */ }
- };
- /* Code Block 1... */
- if(/* result of some condition or function call */)
- {
- /* Code Block 2... */
- if(/* result of some condition or function call */)
- {
- /* Code Block 3... */
- if(/* result of some condition or function call */)
- {
- /* Code Block 4... */
- }
- }
- }
- /* Code Block 1... */
- IsOk = /* result of some condition or function call */
- if(IsOK)
- {
- /* Code Block 2... */
- IsOk = /* result of some condition or function call */
- }
- if(IsOK)
- {
- /* Code Block 3...*/
- IsOk = /* result of some condition or function call */
- }
- if(IsOK)
- {
- /* Code Block 4...*/
- IsOk = /* result of some condition or function call */
- }
- /* And so on... */
Add Comment
Please, Sign In to add comment