Aoop Skill Week-2.1
Aoop Skill Week-2.1
2300032041
K.NEERAJA
1) In a Java application that requires license validation, explain how you can use
the Singleton Design Pattern to implement a License Manager. Describe the
benefits of using a Singleton for managing the license state and validation
process. Provide a sample implementation of a License Manager class that
ensures only one instance is responsible for checking and managing license
status throughout the application.
package week2
public class License {
private License() {
}
static License l = new License();
static License getL() {
return l;
}
static void validation(boolean x) {
if(x==true) {
System.out.println("Valid license");
} else {
System.out.println("Invalid License");
}
}
}
class LicenseManaging{ public
static void main(String args[]) {
License l1=License.getL();
License.validation(false);
}
}
package week2;
public class BankingSystem {
private BankingSystem() {
}
static BankingSystem b=new BankingSystem();
static BankingSystem getb() {
return b;
}
void managing() { int
total=10000; int
expenditure=2000; int
balance= total-expenditure; int
transactions= total-balance;
System.out.println("Savings : "+balance);
System.out.println("Money Spent : "+transactions);
}
}
class BankSystemMananging{
public static void main(String args[]){
BankingSystem b1=BankingSystem.getb();
b1.managing();
}
package skillweek2;
import java.util.Stack;
class OperationFactory {
public 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);
}
}
}
class PostfixEvaluator {
private OperationFactory operationFactory;
public PostfixEvaluator() {
this.operationFactory = new OperationFactory();
}
Engine createEngine();
Wheel createWheel();
Body createBody();
}
interface Engine {
String getType();
}
interface Wheel {
String getSize();
}
interface Body {
String getShape();
}
class CarEngine implements Engine {
public String getType() {
return "Car Engine";
}
}
class CarWheel implements Wheel {
public String getSize() { return "17
inch";
}
}
class CarBody implements Body {
public String getShape() {
return "Sedan";
}
}
class TruckEngine implements Engine { public
String getType() {
return "Truck Engine";
}
}
class TruckWheel implements Wheel { public
String getSize() {
return "22 inch";
}
}
class TruckBody implements Body { public
String getShape() {
return "Pickup";
}
}
class MotorcycleEngine implements Engine {
public String getType() {
return "Motorcycle Engine";
}
}
class MotorcycleWheel implements Wheel { public
String getSize() {
return "15 inch";
}
}
class MotorcycleBody implements Body {
public String getShape() { return
"Sport";
}
}
class CarFactory implements VehicleFactory {
public Engine createEngine() {
return new CarEngine();
}
public Wheel createWheel() {
return new CarWheel();
}
public Body createBody() {
return new CarBody();
}
}
class TruckFactory implements VehicleFactory { public
Engine createEngine() {
return new TruckEngine();
}
public Wheel createWheel() {
return new TruckWheel();
}
public Body createBody() {
return new TruckBody();
}
}
class MotorcycleFactory implements VehicleFactory {
public Engine createEngine() {
return new MotorcycleEngine();
}
public Wheel createWheel() {
return new MotorcycleWheel();
}
public Body createBody() {
return new MotorcycleBody();
}
}
class VehicleAssemblyLine {
private VehicleFactory vehicleFactory;
public VehicleAssemblyLine(VehicleFactory vehicleFactory) {
this.vehicleFactory = vehicleFactory;
}
Public void assembleVehicle() {
Engine engine = vehicleFactory.createEngine();
Wheel wheel = vehicleFactory.createWheel();
Body body = vehicleFactory.createBody();
System.out.println("Assembling a vehicle with:");
System.out.println("Engine: " + engine.getType());
System.out.println("Wheel Size: " + wheel.getSize());
System.out.println("Body Shape: " + body.getShape());
}
}
class VehicleManufacturingSystem { public
static void main(String[] args) { VehicleFactory
carFactory = new CarFactory();
VehicleAssemblyLine carAssemblyLine = new
VehicleAssemblyLine(carFactory);
carAssemblyLine.assembleVehicle();
System.out.println();
VehicleFactory truckFactory = new TruckFactory();
VehicleAssemblyLine truckAssemblyLine = new
VehicleAssemblyLine(truckFactory);
truckAssemblyLine.assembleVehicle();
System.out.println();
VehicleFactory motorcycleFactory = new MotorcycleFactory();
VehicleAssemblyLine motorcycleAssemblyLine = new
VehicleAssemblyLine(motorcycleFactory);
motorcycleAssemblyLine.assembleVehicle();
}
}
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
ConsoleLogger). d) Discuss how you would use this LoggerManager in a
realworld application to ensure efficient and consistent logging.
package skillweek2;
interface Logger { void
log(String message);
}
class LoggerManager {
private static LoggerManager instance;
private LoggerManager() {}