Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions

Question

1. Which position would you take?

Answered: 1 week ago

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago