Question
use class diagram to generate the corresponding design patterns for these codes given below: Abstract Factory pattern class ContinentFactory { public virtual Herbivore CreateHerbivore ();
use class diagram to generate the corresponding design patterns for these codes given below:
Abstract Factory pattern
class ContinentFactory
{
public virtual Herbivore CreateHerbivore();
public virtual Carnivore CreateCarnivore();
}
class AfricaFactory : public ContinentFactory
{
public Herbivore CreateHerbivore()
{
return new Wildebeest();
}
public Carnivore CreateCarnivore()
{
return new Lion();
}
}
class AmericaFactory : public ContinentFactory
{
public Herbivore CreateHerbivore()
{
return new Bison();
}
public Carnivore CreateCarnivore()
{
return new Wolf();
}
}
class Herbivore
{
}
class Carnivore
{
public virtual void Eat(Herbivore h);
}
class Wildebeest : public Herbivore
{
}
class Lion : public Carnivore
{
public void Eat(Herbivore h)
{
// Eat Wildebeest
cout << Lion eats << typeid(h).name() << endl;
}
}
class Bison : Herbivore
{
}
class Wolf : Carnivore
{
public override void Eat(Herbivore h)
{
// Eat Bison
cout << Wolf eats << typeid(h).name() << endl;
}
}
class AnimalWorld
{
private Herbivore _herbivore;
private Carnivore _carnivore;
// Constructor
public AnimalWorld(ContinentFactory factory)
{
_carnivore = factory.CreateCarnivore();
_herbivore = factory.CreateHerbivore();
}
public void RunFoodChain()
{
_carnivore.Eat(_herbivore);
}
}
}
void main(int argc, char* argv[])
{
// Create and run the African animal world
ContinentFactory africa = new AfricaFactory();
AnimalWorld world = new AnimalWorld(africa);
world.RunFoodChain();
// Create and run the American animal world
ContinentFactory america = new AmericaFactory();
world = new AnimalWorld(america);
world.RunFoodChain();
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started