Aoop Skill Week-2
Aoop Skill Week-2
CODE:
private LicenseManager() {
this.isLicenseValid = false;
if (instance == null) {
return instance;
if ("VALID_LICENSE_KEY".equals(licenseKey)) {
isLicenseValid = true;
} else {
isLicenseValid = false;
return isLicenseValid;
isLicenseValid = false;
licenseManager.validateLicense("VALID_LICENSE_KEY");
licenseManager.resetLicense();
}
2. In a Java application for handling banking transactions, how can the
Singleton Design Pattern be used to create a BankingSystemManager?
Explain how this pattern helps ensure there is only one instance
managing banking operations like account balance and transactions.
Provide a clear and simple example of a BankingSystemManager class
that uses Singleton to guarantee a single instance and handle operations
safely in a multi-threaded environment.
CODE:
import java.util.concurrent.ConcurrentHashMap;
private BankingSystemManager() {
accountBalances = new ConcurrentHashMap<>();
}
manager.createAccount("12345", 1000.0);
manager.createAccount("67890", 500.0);
manager.deposit("12345", 200.0);
manager.withdraw("12345", 300.0);
manager.withdraw("11111", 100.0);
}
}
SAMPLE OUTPUT:
3. In a reporting application, users need to generate reports in different
formats such as PDF, Excel, and HTML. Each report format requires
a different implementation for generating the report. a) Design a
ReportFactory class that uses the Factory Design Pattern to create
instances of the appropriate Report type based on user input. b)
Provide the code implementation for the Report, PDFReport,
ExcelReport, and HTMLReport classes. c) Explain how the Factory
Design Pattern helps in managing the creation of different report types
and how it enhances the flexibility and scalability of the reporting
system.
a) Design of ReportFactory
b)
interface Report {
void generateReport();
}
class ReportFactory {
public static Report createReport(String reportType) {
switch (reportType.toLowerCase()) {
case "pdf":
return new PDFReport();
case "excel":
return new ExcelReport();
case "html":
return new HTMLReport();
default:
throw new IllegalArgumentException("Invalid report type: " + reportType);
}
}
}
report.generateReport();
}
}
SAMPLE OUTPUT:
4. You are tasked with implementing a calculator that evaluates postfix
expressions (Reverse Polish Notation). The postfix expression will be
provided as input, and the output should be the result of the
expression. a) Define an abstract Operation class that represents a
generic operation (e.g., addition, subtraction, multiplication, division)
for evaluating postfix expressions. Implement concrete classes for
each specific operation (e.g., Addition, Subtraction, Multiplication,
Division). b) Create a OperationFactory class that uses the Factory
Design Pattern to instantiate the appropriate Operation objects based
on the operator encountered in the postfix expression. c) Implement a
PostfixEvaluator class that utilizes the OperationFactory to evaluate a
given postfix expression. The class should handle the input string
(e.g., "7 3 - 2 1 + *") and produce the correct output (e.g., 12). d)
Explain how the Factory Design Pattern is utilized in your
implementation to handle different operations dynamically and how it
contributes to the design's flexibility and maintainability.
b) OperationFactory Class
class OperationFactory {
public static Operation getOperation(String operator) {
switch (operator) {
case "+":
return new Addition();
case "-":
return new Subtraction();
case "*":
return new Multiplication();
case "/":
return new Division();
default:
throw new IllegalArgumentException("Invalid operator: " + operator);
}
}
}
c) PostfixEvaluator Class
import java.util.Stack;
return stack.pop();
}
SAMPLE OUTPUT:
5. In the context of developing a sophisticated vehicle manufacturing
system that produces different types of vehicles such as cars, trucks,
and motorcycles, each with its own distinct set of components and
assembly processes, how can the Abstract Factory design pattern be
utilized to manage the creation of these components? a) Design an
abstract factory interface VehicleFactory for this system. What
methods should this interface include to facilitate the creation of
components specific to each type of vehicle? b) Implement concrete
factories for each type of vehicle: CarFactory, TruckFactory, and
MotorcycleFactory. Describe the specific components each factory
should produce and how these components differ from one another.
c) Discuss how the Abstract Factory pattern helps in achieving
separation of concerns in the vehicle manufacturing system. What
advantages does it offer in terms of scalability and flexibility when
adding new vehicle types or components? d) Provide a code example
interface and its concrete implementations to assemble a vehicle.
Demonstrate how the client can work with different factories to
assemble different types of vehicles without changing the core logic.
b) Concrete Factories
Each concrete factory implements the VehicleFactory interface to produce components
specific to a vehicle type.
CarFactory
public class CarFactory implements VehicleFactory {
public Engine createEngine() {
return new CarEngine();
}
interface Wheels {
void assemble();
}
interface Chassis {
void assemble();
}
class CarEngine implements Engine {
public void assemble() {
System.out.println("Assembling car engine.");
}
}
SAMPLEOUPUT:
6. Design a Logger Management System in Java that utilizes both the
Singleton and Factory design patterns. In this system: a) Singleton
Pattern: Ensure that there is only one instance of the LoggerManager
class throughout the application. This class will manage the logging
system and provide access to various types of loggers. b) Factory
Pattern: Implement a factory method within the LoggerManager that
creates and returns different types of loggers (e.g., FileLogger,
ConsoleLogger) based on the specified type. In your design: a) Describe
the role of the LoggerManager class and how the Singleton pattern is
applied to it. b) Explain how the Factory pattern is used within the
LoggerManager to create different types of loggers. c) Provide a code
implementation for the LoggerManager class, the Logger interface, and
at least two concrete implementations of loggers (e.g., FileLogger and
c)
Logger Interface and Concrete Loggers
public interface Logger {
void log(String message);
}
private LoggerManager() {}