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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!