Lab Experiment 2
Lab Experiment 2
Vijesh
ID NO.2200031659
LAB EXPERIMENT – 2
Creational Design Pattern II - Factory and Abstract Design Pattern
PRE-LAB:
1]
2]
IN-LAB:
A]
package Game;
interface Weapon {
void attack();
package Game;
package Game;
package Game;
interface PowerUp {
void activate();
package Game;
S.Vijesh
ID NO.2200031659
package Game;
package Game;
interface AbstractGameFactory {
Weapon createWeapon();
PowerUp createPowerUp();
package Game;
package Game;
S.Vijesh
ID NO.2200031659
package Game;
package Game;
void attack() {
package Game;
void attack() {
package Game;
interface EnemyFactory {
S.Vijesh
ID NO.2200031659
Enemy createEnemy();
package Game;
package Game;
package Game;
class GameManager {
private GameManager() {
currentLevel = 1;
if (instance == null) {
return instance;
if (difficulty.equalsIgnoreCase("easy")) {
} else if (difficulty.equalsIgnoreCase("hard")) {
weapon.attack();
powerUp.activate();
enemy.attack();
System.out.println("Game over!");
currentLevel++;
}
S.Vijesh
ID NO.2200031659
package Game;
gameManager.startGame();
gameManager.setDifficulty("hard");
gameManager.nextLevel();
gameManager.startGame();
OUTPUT:
Starting game at level 1 with EasyGameFactory
Game over!
Proceeding to level 2
Game over!
S.Vijesh
ID NO.2200031659
1] Increased code reusability Design patterns allow you to reuse code that
has already been written and tested. This can save you time and effort
when you are developing new code.
Improved code readability and maintainability Design patterns can make
your code more readable and maintainable by providing a common
vocabulary and set of conventions. This can make it easier for other
developers to understand your code and make changes to it in the future.
Reduced coupling. Design patterns can help to reduce coupling between
different parts of your code. This can make your code more flexible and
easier to change.
Improved testability. Design patterns can make your code more testable by
providing a well-defined interface for interacting with different parts of
your code. This can make it easier to write unit tests for your code.
Increased performance. Design patterns can sometimes lead to increased
performance by providing a more efficient way to implement certain
algorithms or data structures.
Here are some specific design patterns that can be used in Java:
Singleton pattern: The Singleton pattern ensures that there is only one
instance of a class. This can be useful for objects that need to be globally
accessible or that need to maintain a single state.
Factory pattern: The Factory pattern provides a way to create objects
without specifying their concrete class. This can be useful for creating
objects that depend on runtime parameters or that need to be created in a
specific way.
S.Vijesh
ID NO.2200031659
2]
Single Responsibility Principle (SRP): A class should only have one
responsibility. This means that a class should only do one thing and do
it well.
Open-Closed Principle (OCP): Classes should be open for extension but
closed for modification. This means that you should be able to add
new functionality to a class without having to modify the existing
code.
Liskov Substitution Principle (LSP): Subclasses should be substitutable for
their base classes. This means that you should be able to use a subclass in
place of its base class without any problems.
Interface Segregation Principle (ISP): Clients should not be forced to
depend on methods they do not use. This means that you should not force a
client to implement methods that it does not need.
Dependency Inversion Principle (DIP): Depend on abstractions, not on
concretions. This means that you should depend on interfaces or abstract
classes, not on concrete classes.
These principles can help you to write better design patterns in Java. By
following these principles, you can make your code more flexible,
maintainable, and extensible.
S.Vijesh
ID NO.2200031659
Design patterns and algorithms are both important tools for software
development. Design patterns can help you to solve specific problems,
while algorithms can help you to solve specific problems in an efficient
way. By understanding the differences between design patterns and
algorithms, you can use them together to create better software.
Design patterns are often implemented using algorithms. For example, the
Singleton pattern can be implemented using a lazy initialization algorithm.
Algorithms can be used to implement design patterns. For example, the
Factory pattern can be implemented using a simple algorithm that returns
a new instance of a class based on a given input.
S.Vijesh
ID NO.2200031659
POST-LAB:
A]
package Experiment_2;
interface Vehicle {
String ride();
package Experiment_2;
@Override
package Experiment_2;
@Override
package Experiment_2;
@Override
package Experiment_2;
interface VehicleFactory {
Vehicle createVehicle();
package Experiment_2;
@Override
package Experiment_2;
@Override
S.Vijesh
ID NO.2200031659
package Experiment_2;
@Override
package Experiment_2;
interface PaymentMethod {
package Experiment_2;
@Override
package Experiment_2;
@Override
package Experiment_2;
interface PaymentFactory {
PaymentMethod createPaymentMethod();
package Experiment_2;
@Override
package Experiment_2;
@Override
package Experiment_2;
class UserAuthentication {
private UserAuthentication() {
}
S.Vijesh
ID NO.2200031659
if (instance == null) {
return instance;
return true;
package Experiment_2;
import java.util.Scanner;
if (authenticatedUser) {
switch (vehicleType.toLowerCase()) {
case "car":
break;
case "bike":
break;
case "scooter":
break;
default:
System.exit(1);
System.out.println(vehicle.ride());
switch (paymentMethodType.toLowerCase()) {
break;
case "paypal":
S.Vijesh
ID NO.2200031659
break;
default:
System.exit(1);
System.out.println(paymentMethod.pay(amount));
} else {
scanner.close();
OUTPUT: