mardi 17 juillet 2018

Factory method pattern uses inheritance while the abstract factory pattern uses composition how?

I am going through the difference between Abstract Factory Pattern vs Factory Method Pattern. I understood that Factory Method is used to create one product only but Abstract Factory is about creating families of related or dependent products. But how Factory method pattern uses inheritance while the abstract factory pattern uses composition is unclear for me.

I know that this is been asked couple of times, Could any one please explain with the below code how inheritance and composition is happening?

Factory Method code

class IRose
{
public:
virtual string Color(void)=0;
};

class RedRose: public IRose
{
public:
string Color(void)
{
    return "Red";
}
};

class YellowRose: public IRose
{
public:
string Color(void)
{
    return "Yellow";
}
};

class IFactory
{
public:
virtual IRose* Create(string type)=0; 
//The factory create method in 90% of cases will take a parameter which 
//determines what kind of the object the factory will return.   
};

class Factory: public IFactory
{
public:
IRose* Create(string type)
{
    if ("Red" == type)
        return new RedRose();

    if ("Yellow" == type)
        return new YellowRose();

    return NULL;
}
};

int main()
{
IRose* p = NULL;
IFactory* f = NULL;

f = new Factory();  //You have to create an INSTANCE of the factory

p = f->Create("Red");
cout<<"\nColor is: "<<p->Color()<<"\n";
delete p;
p = f->Create("Yellow");
cout<<"\nColor is: "<<p->Color()<<"\n";
delete p;
return 1;
}

Abstract factory code.

class IFridge
{
public:
virtual string Run(void) = 0;
};

class FridgeSamsung : public IFridge
{
public:
string Run(void)
{
    return "You are now running Samsung Fridge\n";
}
};

class FridgeWhirlpool : public IFridge
{
public:
string Run(void)
{
    return "You are now running Whirlpool Fridge\n";
}
};

class IWashingMachine
{
public:
virtual string Run(void) = 0;
};

class WashingMachineSamsung : public IWashingMachine
{
public:
string Run(void)
{
    return "You are now running Samsung Washing Machine\n";
}
};

class WashingMachineWhirlpool : public IWashingMachine
{
public:
string Run(void)
{
    return "You are now running Whirlpool Washing Machine\n";
}
};

class IFactory
{
public:
virtual IFridge* GetFridge(void) = 0;
virtual IWashingMachine* GetWashingMachine(void) = 0;
};

class FactorySamsung : public IFactory
{
IFridge* GetFridge(void)
{
    return new FridgeSamsung();
}

IWashingMachine* GetWashingMachine(void)
{
    return new WashingMachineSamsung();
}
};

class FactoryWhirlpool : public IFactory
{
IFridge* GetFridge(void)
{
    return new FridgeWhirlpool();
}

IWashingMachine* GetWashingMachine(void)
{
    return new WashingMachineWhirlpool();
}
};

int main()
{
IFridge* fridge;    //Client just knows about fridge and washingMachine.
IWashingMachine* washingMachine; //and factory. He will write operations which
IFactory* factory; //work on fridges and washingMachines.

factory = new FactorySamsung; 
//This is the only place where the client
//has to make a choice. 

//The rest of the code below will remain same, even 
//if the factory is changed. He can change the factory and the same range
//of products but from a different factory will be returned. No need to 
//change any code.

fridge = factory->GetFridge();
cout << fridge->Run();
washingMachine = factory->GetWashingMachine();
cout << washingMachine->Run();
cout << "\n";

delete factory;
factory = new FactoryWhirlpool;

//See same client code.
fridge = factory->GetFridge();
cout << fridge->Run();
washingMachine = factory->GetWashingMachine();
cout << washingMachine->Run();
cout << "\n";
delete factory;
return 1;
}

Aucun commentaire:

Enregistrer un commentaire