Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

DIGITAL ASSIGNMENT-II-F2 SLOT

Name: Thirukandiyur Sudarshan Karthik


Regno: 20MIS1110

TICPAT Company has hired you as a consultant. For a video game, they are
creating different kinds of animals. As a player moves through the game, as
various events happen, the application needs to create lions, tigers, dragons and
dinosaurs. Some of the characteristics of these animals differed by their killing
behavioural results which gives different points for the player.
Identify the suitable design pattern and illustrate the same. Propose a software
architecture for the given scenario and provide your justification for the same.

One suitable design pattern for this scenario is the Factory Method pattern. The Factory Method pattern allows us
to define an interface for creating objects, but let subclasses decide which classes to instantiate. In this case, we
can use the Factory Method pattern to create instances of different animal subclasses as needed in the game.

// AnimalFactory interface
interface AnimalFactory {
  public Animal createAnimal();
}

// LionFactory class
class LionFactory implements AnimalFactory {
  public Animal createAnimal() {
    return new Lion();
  }
}

// TigerFactory class
class TigerFactory implements AnimalFactory {
  public Animal createAnimal() {
    return new Tiger();
  }
}

// DragonFactory class
class DragonFactory implements AnimalFactory {
  public Animal createAnimal() {
    return new Dragon();
  }
}

// DinosaurFactory class
class DinosaurFactory implements AnimalFactory {
  public Animal createAnimal() {
    return new Dinosaur();
  }
}

// Game class
class Game {
  private List<Animal> animals;
  private List<AnimalFactory> animalFactories;

  public Game() {
    animals = new ArrayList<Animal>();
    animalFactories = new ArrayList<AnimalFactory>();
    animalFactories.add(new LionFactory());
    animalFactories.add(new TigerFactory());
    animalFactories.add(new DragonFactory());
    animalFactories.add(new DinosaurFactory());
  }

  public void spawnAnimal(int index) {


    AnimalFactory factory = animalFactories.get(index);
    Animal animal = factory.createAnimal();
    animals.add(animal);
  }
}

One suitable design pattern for this scenario is the Factory Method pattern. The Factory Method pattern allows us
to define an interface for creating objects, but let subclasses decide which classes to instantiate. In this case, we
can use the Factory Method pattern to create instances of different animal subclasses as needed in the game.

Here's an illustration of how the Factory Method pattern could be used in this scenario:

csharpCopy code

// AnimalFactory interface interface AnimalFactory { public Animal createAnimal(); } // LionFactory class class
LionFactory implements AnimalFactory { public Animal createAnimal() { return new Lion(); } } // TigerFactory class
class TigerFactory implements AnimalFactory { public Animal createAnimal() { return new Tiger(); } } //
DragonFactory class class DragonFactory implements AnimalFactory { public Animal createAnimal() { return new
Dragon(); } } // DinosaurFactory class class DinosaurFactory implements AnimalFactory { public Animal
createAnimal() { return new Dinosaur(); } } // Game class class Game { private List<Animal> animals; private
List<AnimalFactory> animalFactories; public Game() { animals = new ArrayList<Animal>(); animalFactories = new
ArrayList<AnimalFactory>(); animalFactories.add(new LionFactory()); animalFactories.add(new TigerFactory());
animalFactories.add(new DragonFactory()); animalFactories.add(new DinosaurFactory()); } public void
spawnAnimal(int index) { AnimalFactory factory = animalFactories.get(index); Animal animal =
factory.createAnimal(); animals.add(animal); } }

In this implementation, we define an AnimalFactory interface with a single method createAnimal() that returns an
instance of the Animal class. We then create four concrete factory classes, one for each type of animal, that
implement this interface and return instances of the appropriate animal subclass.

In the Game class, we create a list of AnimalFactory objects and add instances of each concrete factory class to the
list. When we want to spawn an animal in the game, we can simply call the spawnAnimal() method with the index
of the desired animal factory in the list. This method uses the selected factory to create a new instance of the
corresponding animal subclass and adds it to the list of animals in the game.

This software architecture provides several benefits. First, it allows us to easily add new animal types to the game
by creating new concrete factory classes that implement the AnimalFactory interface. Second, it separates the
process of creating animal instances from the rest of the game logic, making it easier to manage and maintain.
Finally, it provides a flexible and scalable system for creating different types of animals with unique properties and
behaviors in the video game.

The proposed software architecture based on the Factory Method pattern is suitable for the given scenario for
several reasons.

Firstly, it allows us to create instances of different animal subclasses without tightly coupling the creation process
to the game logic. This separation of concerns makes the code more modular, easier to understand, and easier to
maintain.

Secondly, the use of the Factory Method pattern allows us to easily add new animal types to the game by creating
new concrete factory classes that implement the AnimalFactory interface. This means that the code can be easily
extended to support new types of animals without modifying the existing code.

Thirdly, the use of the Factory Method pattern also provides a clear interface for creating animal objects, making
the code more readable and easier to understand. This also helps in unit testing and maintenance of the code.

Finally, the proposed software architecture also provides a flexible and scalable system for creating different types
of animals with unique properties and behaviors in the video game, which is essential for creating an engaging and
enjoyable player experience.

The Animal Factory and Animal subclasses are part of the Business Logic layer, which is responsible for
implementing the game's rules and logic. The Game Logic manages the game events and actions, including the
spawning of different types of animals and keeping track of the player's score.

The GUI is responsible for handling the user interface and interactions with the user, while the Data Storage layer
manages the game data and stores it persistently. The Data Access Layer interacts with the database to read and
write data related to the game state and progress.

This architecture provides several benefits. It separates the different concerns of the video game, making it easier
to maintain and extend the code. It also provides a clear separation of the different layers, which can be developed
and tested independently. Finally, it provides a flexible and scalable system for creating different types of animals
with unique properties and behaviors in the video game.
Assume that you are asked to develop an Educational management System for
VIT| International school for kids. Identify the creational and Structural design
patterns that can be applied for the above system and draw the UML diagrams
for both and discuss how they are working together.

For the Educational Management System for VIT| International School for Kids, we can use the following creational
and structural design patterns:

1. Creational Design Pattern: Singleton Pattern

2. Structural Design Pattern: Composite Pattern

Singleton Pattern:

The Singleton pattern ensures that only one instance of a class is created and provides global access to that
instance. This pattern is useful for situations where we need to ensure that only one instance of a class exists, such
as when we want to limit access to a shared resource or ensure that multiple instances of the class do not interfere
with each other.

In the context of the Educational Management System, we can use the Singleton pattern for the following classes:

 DatabaseConnectionManager: This class will be responsible for managing the database connections for the
system. Since we want to ensure that only one connection is used throughout the system, we can use the
Singleton pattern for this class.

 SchoolAdmin: This class will be responsible for managing the administrative tasks of the school. Since there
should only be one school administrator, we can use the Singleton pattern for this class.

For the Educational Management System for VIT| International School for Kids, we can use the following creational
and structural design patterns:

1. Creational Design Pattern: Singleton Pattern

2. Structural Design Pattern: Composite Pattern

Singleton Pattern:

The Singleton pattern ensures that only one instance of a class is created and provides global access to that
instance. This pattern is useful for situations where we need to ensure that only one instance of a class exists, such
as when we want to limit access to a shared resource or ensure that multiple instances of the class do not interfere
with each other.

In the context of the Educational Management System, we can use the Singleton pattern for the following classes:

 DatabaseConnectionManager: This class will be responsible for managing the database connections for the
system. Since we want to ensure that only one connection is used throughout the system, we can use the
Singleton pattern for this class.

 SchoolAdmin: This class will be responsible for managing the administrative tasks of the school. Since there
should only be one school administrator, we can use the Singleton pattern for this class.

The Singleton pattern and Composite pattern work together in the Educational Management System as follows:
 The Singleton pattern is used to ensure that only one instance of the DatabaseConnectionManager and
SchoolAdmin classes is created, which provides global access to these instances throughout the
 The Model represents the data and business logic of the system, and it interacts with the View and
Controller to update and retrieve data. The View represents the user interface, and it interacts with the
Model and Controller to display data and receive input. The Controller manages user input, communicates
with the Model and View to update data, and updates the View to reflect changes in the Model.

 The Singleton Pattern and MVC Pattern can work together in the educational management system. For
example, the Singleton Pattern can be used to create a database connection class that can be shared across
multiple components of the MVC architecture. The Model component can use the Singleton instance to
access and manipulate the database, while the View and Controller components can use the Model to
display and update data.

 Overall, the use of the Builder Pattern, Singleton Pattern, and MVC Pattern can help to create a robust and
maintainable educational management system for VIT| International School for kids.

You might also like