Banking System

  1. We have Bank Account with 2 Fields – balance and Account Number
  2. We have Transaction class implementing Runnable
  3. We create object for account with some initial balance and try to pass as parameter to runnable Transaction Object

BankAccount.java

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class BankAccount {
    private Integer balance;
    private Integer accountNumber;

    private final Lock reLock = new ReentrantLock();

    public BankAccount(Integer balance, Integer accountNumber){
        this.balance = balance;
        this.accountNumber = accountNumber;
    }

    public void debitAmount(Integer amount){
        reLock.lock();

        try{
            balance -= amount;
        }finally {
            reLock.unlock();
        }

    }

    public void creditAmount(Integer amount){
        reLock.lock();

        try{
            balance += amount;
        }finally {
            reLock.unlock();
        }
    }

    public Integer getAccountNumber(){
        return this.accountNumber;
    }

    public Integer getBalance(){
        return this.balance;
    }

}

BankTransaction.java

public class BankTransaction implements Runnable{
    public Integer transAmount;
    public BankAccount bankAccount;

    public BankTransaction(Integer transAmount, BankAccount bankAccount){
        this.transAmount  = transAmount;
        this.bankAccount  = bankAccount;
    }


    @Override
    public void run() {
        if(transAmount >= 0){
            bankAccount.creditAmount(transAmount);
        }else{
            bankAccount.debitAmount(Math.abs(transAmount));
        }
    }
}

BankSystem.java

public class BankSystem {
    public static void main(String[] args) {
        BankAccount objAcc1 = new BankAccount(1000, 101);
        BankAccount objAcc2 = new BankAccount(2000, 102);

        Thread objThread1 = new Thread(new BankTransaction(50, objAcc1));
        Thread objThread2 = new Thread(new BankTransaction(-150, objAcc2));
        Thread objThread3 = new Thread(new BankTransaction(250, objAcc2));
        Thread objThread4 = new Thread(new BankTransaction(250, objAcc1));

        objThread1.start();
        objThread2.start();
        objThread3.start();
        objThread4.start();

        try{
            objThread1.join();
            objThread2.join();
            objThread3.join();
            objThread4.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        System.out.println("Final Balance in Account " + objAcc1.getAccountNumber() + " with balance " + objAcc1.getBalance());
        System.out.println("Final Balance in Account " + objAcc2.getAccountNumber() + " with balance " + objAcc2.getBalance());
    }
}

Output

Final Balance in Account 101 with balance 1300
Final Balance in Account 102 with balance 2100
  1. We use ReentrantLock for locking the Resource(totalSeats)
  2. Incase anything goes wrong (Exception being thrown etc.) you want to make sure the lock is released no matter what.
  3. Calling the reserveSeats method should be done inside separate threads

ReservationSystem.java

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReservationSystem {
    private Integer totalSeats;
    private final Lock lock = new ReentrantLock();

    public ReservationSystem(Integer totalSeats){
        this.totalSeats = totalSeats;
    }

    public Integer getTotalSeats(){
        return totalSeats;
    }

    public void reserveSeats(String userName, int numOfSeats){
        lock.lock();

        try{
            if(numOfSeats >0 && totalSeats>numOfSeats){
                totalSeats -= numOfSeats;
                System.out.println(userName + " has reserved "+ numOfSeats + " with " + totalSeats + " still available");
            }else{
                System.out.println("Seats not Available");
            }
        }finally {
            lock.unlock();
        }
    }
}

BookSeat.java

public class BookSeat {
    public static void main(String[] args) {
        ReservationSystem objResSys = new ReservationSystem(100);

        System.out.println("Total available Seats "+ objResSys.getTotalSeats());

        Thread objThread1 = new Thread(() -> {objResSys.reserveSeats("User1", 10);});
        Thread objThread2 = new Thread(() -> {objResSys.reserveSeats("User2", 20);});
        Thread objThread3 = new Thread(() -> {objResSys.reserveSeats("User3", 5);});


        objThread1.start();
        objThread2.start();
        objThread3.start();

        try {
            objThread1.join();
            objThread2.join();
            objThread3.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        System.out.println("Remaining available Seats "+ objResSys.getTotalSeats());
    }
}


Total available Seats 100
User2 has reserved 20 with 80 still available
User1 has reserved 10 with 70 still available
User3 has reserved 5 with 65 still available
Remaining available Seats 65
  1. NEW – a newly created thread that has not yet started the execution
  2. RUNNABLE – either running or ready for execution but it’s waiting for resource allocation
  3. BLOCKED – waiting to acquire a monitor lock to enter or re-enter a synchronized block/method
  4. WAITING – waiting for some other thread to perform a particular action without any time limit
  5. TIMED_WAITING – waiting for some other thread to perform a specific action for a specified period
  6. TERMINATED – has completed its execution

NEW Thread (or a Born Thread) is a thread that’s been created but not yet started.
It remains in this state until we start it using the start() method

NewState.java

public class NewState implements Runnable{
    public void run(){
        System.out.println("I am in new State");
    }
}

Main.java

public class Main {
    public static void main(String[] args) throws InterruptedException {
       Thread objThread = new Thread(new NewState());
       System.out.println(objThread.getState());
    }
}

Output

NEW

Runnable When we’ve created a new thread and called the start() method on that, it’s moved from NEW to RUNNABLE state. Threads in this state are either running or ready to run, but
they’re waiting for resource allocation from the system. In a multi-threaded environment, the Thread-Scheduler (which is part of JVM) allocates a fixed amount of time to each thread. So it runs for a particular amount of time, then leaves the control to other RUNNABLE threads.

RunnableState .java

public class RunnableState implements Runnable{
    public void run(){
        System.out.println("I would be in Runnable State");
    }
}

Main.java

public class Main {
    public static void main(String[] args) throws InterruptedException {
       Thread objRThread = new Thread(new RunnableState());
       objRThread.start();
       System.out.println(objRThread.getState());
    }
}

Output

RUNNABLE
I would be in Runnable State

This is the state of a dead thread. It’s in the TERMINATED state when it has either finished execution or was terminated abnormally.
TerminatedState.java

public class TerminatedState implements Runnable{
    public void run(){
        Thread objNewState = new Thread(new NewState());
        objNewState.start();
    }
}

Main.java

public class Main {
    public static void main(String[] args) throws InterruptedException {
       Thread objTState = new Thread(new TerminatedState());
       objTState.start();
       objTState.sleep(1000);
       System.out.println("T1 : "+ objTState.getState());
    }
}

Output

I am in new State
T1 : TERMINATED

A thread is in the BLOCKED state when it’s currently not eligible to run. It enters this state when it is waiting for a monitor lock and is trying to access a section of code that is locked by some other thread.
BlockedState.java

public class BlockedState implements Runnable{
    public void run(){
      blockedResource();
    }

    public static synchronized void blockedResource(){
        while(true){
            //Do Nothing
        }
    }
}

Main.java

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread objB1Thread = new Thread(new BlockedState());
        Thread objB2Thread = new Thread(new BlockedState());

        objB1Thread.start();
        objB2Thread.start();

        Thread.sleep(1000);

        System.out.println(objB1Thread.getState());
        System.out.println(objB2Thread.getState());
        System.exit(0);
    }
}

Output

RUNNABLE
BLOCKED

A thread is in WAITING state when it’s waiting for some other thread to perform a particular action. According to JavaDocs, any thread can enter this state by calling any one of the following
object.wait() (or) thread.join() (or) LockSupport.park()

WaitingState.java

public class WaitingState implements Runnable{
    public void run(){
        Thread objWaitState = new Thread(new SleepState());

        objWaitState.start();

        try {
            objWaitState.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

SleepState.java

public class SleepState implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

Main.java

public class Main {
    public static void main(String[] args) throws InterruptedException {
       Thread objWaitingThread = new Thread(new WaitingState());
       objWaitingThread.start();
       objWaitingThread.sleep(1000);
       System.out.println("T1 : "+ objWaitingThread.getState());
       System.out.println("Main : "+Thread.currentThread().getState());
    }
}

Output

T1 : WAITING
Main : RUNNABLE

A thread is in TIMED_WAITING state when it’s waiting for another thread to perform a particular action within a stipulated amount of time. According to JavaDocs, there are five ways to put a thread on TIMED_WAITING state:
thread.sleep(long millis) (or) wait(int timeout) (or) wait(int timeout, int nanos) thread.join(long millis) (or) LockSupport.parkNanos (or) LockSupport.parkUntil

TimedWaitState.java

public class TimedWaitState implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

Main.java

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread objTWState = new Thread(new TimedWaitState());
        objTWState.start();
        Thread.sleep(2000);
        System.out.println("T1 : "+ objTWState.getState());
    }
}

Output

T1 : TIMED_WAITING

Simple Program to print numbers using threads
NumberPrinter.java

public class NumberPrinter implements Runnable{
    int number;
    public NumberPrinter(int number){
        this.number = number;
    }

    public void run(){
        System.out.println("Printing Number from Thread "+ this.number);
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        for (int idx=1;idx<=5;idx++){
            Thread objthread = new Thread(new NumberPrinter(idx));
            objthread.start();
        }
    }
}

Output

Printing Number from Thread 5
Printing Number from Thread 1
Printing Number from Thread 4
Printing Number from Thread 3
Printing Number from Thread 2

How to Delete duplicate Rows from Table

In the below table studId 1,6 and 9 is repeated which should be deleted.

studId studentName age
1 Mugil 35
2 Vinu 36
3 Viju 42
4 Mani 35
5 Madhu 36
6 Mugil 35
7 Venu 37
8 Banu 34
9 Mugil 35

Below query wont work on MySQL but the format doesn’t change. When you take Max only last occurrence of row would be taken and others would be excluded.

DELETE FROM tblStudents TS
 WHERE TS.studId NOT IN (SELECT MAX(TSS.studId)
                            FROM tblStudents TSS
                        GROUP BY TSS.studentName, TSS.age)c

The same could be done using MIN function.

SELECT TS.* FROM tblStudents TS
 WHERE TS.studId NOT IN (SELECT MIN(TSS.studId)
                           FROM tblStudents TSS
                          GROUP BY TSS.studentName, TSS.age)                        

Output

studId studentName age
6 Mugil 35
9 Mugil 35

The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. The observer pattern is a behavioral design pattern that defines a one-to-many relationship between objects. It allows an object, called the subject, to notify other objects, called observers, about any changes in its state. The observers can then react to these changes according to their own logic.

Why Observer Design Pattern
The subject and the observers are loosely coupled, meaning that they do not depend on each other’s implementation details. The subject only knows that it has a list of observers, and the observers only know that they can update themselves when the subject changes.

When Observer Design Pattern
The observer pattern is especially useful in event-driven systems, where the system reacts to events that occur asynchronously. An event is a change in the state of the system or its components, such as a user input, a network request, or a timer. Use the pattern when some objects in your app must observe others, but only for a limited time or in specific cases. Use the Observer pattern when changes to the state of one object may require changing other objects, and the actual set of objects is unknown beforehand or changes dynamically.

How Observer Design Pattern

  1. Declare the subscriber interface(IObserver.java). At a bare minimum, it should declare a single update method.
  2. Declare the publisher interface(Observable.java) and describe a pair of methods for adding a subscriber(Observer) object to and removing it from the list. Remember that publishers must work with subscribers only via the subscriber interface.
  3. Create concrete publisher classes. Each time something important happens inside a publisher, it must notify all its subscribers.
  4. Implement the update notification methods in concrete subscriber classes. Most subscribers would need some context data about the event. It can be passed as an argument of the notification method.

IObserver.java

public interface IObserver {
    public void notify(String serviceName);
}

Observer.java

public class Observer implements IObserver {
    @Override
    public void notify(String serviceName) {
            System.out.println("Notification from " + serviceName);
    }
}

Observable.java

public class Observable {
    private List<IObserver> observers = new ArrayList<IObserver>();

    public void addObserver(IObserver observer){
        this.observers.add(observer);
    }

    public void execute(){
        for (IObserver observer : observers) {
            observer.notify("Email Service");
        }
    }
}

Client.java

public class Client {
    public static void main(String[] args) {
        Observable observable = new Observable();
        observable.addObserver(new Observer());
        observable.execute();
    }
}

IObserver.java

public interface IObserver {
    public void notify(String foodItem);
}

DeliveryOrderObserver.java

public class DeliveryOrderObserver implements IObserver {
    @Override
    public void notify(String foodItem) {
        System.out.println("Ready for Pickup - "+ foodItem);
    }
}

FoodOrderObserver.java

public class FoodOrderObserver implements IObserver {
    @Override
    public void notify(String foodItem) {
        System.out.println(" Order received - "+ foodItem);
    }
}

FoodPickUpObserver.java

public class FoodPickUpObserver implements IObserver {
    @Override
    public void notify(String foodItem) {
        System.out.println("Parcel - "+ foodItem);
    }
}

PrepareFoodObserver.java

public class PrepareFoodObserver implements IObserver {
    @Override
    public void notify(String foodItem) {
        System.out.println("Preparing - "+ foodItem);
    }
}

Observable.java

public class Observable {
    private List<IObserver> observers = new ArrayList<IObserver>();

    public void addObserver(IObserver observer){
        this.observers.add(observer);
    }

    public void execute(){
            for(IObserver observer: observers){
                observer.notify("Egg Biryani");
        }
    }
}

FoodAppClient.java

public class FoodAppClient {
    public static void main(String[] args) {
        Observable observable = new Observable();

        observable.addObserver(new FoodOrderObserver());
        observable.addObserver(new PrepareFoodObserver());
        observable.addObserver(new FoodPickUpObserver());
        observable.addObserver(new DeliveryOrderObserver());
        observable.execute();
    }
}
Order received - Egg Biryani
Preparing - Egg Biryani
Parcel - Egg Biryani
Ready for Pickup - Egg Biryani

Advantages of Observer Pattern

  1. The subject and the observers can be reused in different contexts, as long as they implement the common interface. The subject does not need to know the details of the observers, and the observers do not need to know the details of the subject. The subject and the observers can be added, removed, or changed at runtime, without affecting the rest of the system.
  2. The subject can notify the observers asynchronously, without waiting for their responses. The observers can process the events in parallel, without blocking the subject. This allows for the subject and the observers to adapt to changing requirements and scenarios.

Strategy Design Pattern provides a way to extract the behavior of an object into separate classes that can be swapped in and out at runtime

Why Strategy Design Pattern
This pattern enables an object to choose from multiple algorithms and behaviors at runtime, rather than statically choosing a single one.

When Strategy Design Pattern
Places where different algorithms(Strategy) is used to address a single solution I.E Different sorting algorithms can be encapsulated into separate strategies and passed to an object that needs sorting, Different formatting strategies can be encapsulated into separate strategies and passed to an object that needs formatting, Different payment methods can be encapsulated into separate strategies and passed to an object that needs to process payments.

How Strategy Design Pattern
The Strategy Design Pattern works by separating the behavior of an object from the object itself. The behavior is encapsulated into different strategies, each with its own implementation of the behavior.The context maintains a reference to a strategy object and interacts with it through a common interface. At runtime, the context can swap the current strategy with another one, effectively changing the object’s behavior.

  1. Identify the algorithm or behavior that needs to be encapsulated and made interchangeable.
  2. Define an interface that represents the behavior, with a single method signature that takes in any required parameters.
  3. Implement concrete classes that provide specific implementations of the behavior defined in the interface.
  4. Define a context class that holds a reference to the interface and calls its method when needed.
  5. Modify the context class to allow for the dynamic swapping of the concrete implementations at runtime.

PaymentProcessor.java

public class PaymentProcessor{
    private PaymentType paymentType;

    public void processPayment(String paymentAmount){
        if(paymentType.equals(PaymentType.CASH)){
            System.out.println("Cash Payment on Delivery");
        }else if(paymentType.equals(PaymentType.NET_BANKING)){
            System.out.println("Net Banking Payment ");
        }else if(paymentType.equals(PaymentType.UPI)){
            System.out.println("UPI Payment by GPay or Paytm ");
        }else if(paymentType.equals(PaymentType.DEBIT_CARD)){
            System.out.println("Debit Card Payment ");
        }else if(paymentType.equals(PaymentType.CREDIT_CARD)){
            System.out.println("Credit Card Payment ");
        }else{
            System.out.println("Gift Coupon");
        }

    }

    public void setPaymentType(PaymentType paymentType) {
        this.paymentType = paymentType;
    }
}

Problem with above code
In this code, the PaymentProcessorclass has a processPayment method that takes a payment amount and processes the payment. The payment type is set using the setPaymentType method, which sets the paymentType field. The processPayment method then checks the value of paymentType and processes the payment accordingly.

The problem with this code is that it violates the Open-Closed Principle, which states that classes should be open for extension but closed for modification. In this code, if you want to add a new payment type, you would have to modify the processPayment method, which violates the Open-Closed Principle.

PaymentStrategy.java

public interface PaymentStrategy {
    public void paymentMethod(String amount);
}

CashPayment.java

public class CashPayment implements PaymentStrategy {
    @Override
    public void paymentMethod(String amount) {
        System.out.println(amount + " paid in cash");
    }
}

CreditCardPayment.java

public class CreditCardPayment implements PaymentStrategy {
    @Override
    public void paymentMethod(String amount) {
        System.out.println(amount + " paid in credit card");
    }
}

CouponPayment.java

public class CouponPayment implements PaymentStrategy {
    @Override
    public void paymentMethod(String amount) {
        System.out.println(amount + " paid in Coupons");
    }
}

DebitCardPayment.java

public class DebitCardPayment implements PaymentStrategy {
    @Override
    public void paymentMethod(String amount) {
        System.out.println(amount + " paid in debit card");
    }
}

UPIPayment.java

public class UPIPayment implements PaymentStrategy {
    @Override
    public void paymentMethod(String amount) {
        System.out.println(amount + " paid in UPI");
    }
}

DecidePaymentStrategy.java

public class DecidePaymentStrategy {
    private PaymentStrategy paymentStrategy;

    public DecidePaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    public void processPayment(String paymentAmount){
        paymentStrategy.paymentMethod(paymentAmount);
    }
}

MakePaymentClient.java

public class MakePaymentClient {
    public static void main(String[] args) {
        PaymentStrategy objPaymentStrategy = new CashPayment();
        MakePayment objMakePayment = new MakePayment(objPaymentStrategy);
        objMakePayment.processPayment("35.54");
    }
}

PaymentType.java

public enum PaymentType {
    CREDIT_CARD("CREDIT_CARD"), DEBIT_CARD("DEBIT_CARD"), CASH("CASH"), NET_BANKING("NET_BANKING"),UPI("UPI");

    private String paymentMode;

    PaymentType(String paymentMode) {
        this.paymentMode = paymentMode;
    }
}
35.54 paid in cash

Note:
The same strategy design pattern could be implemented using Factory method by moving code to Factory class and deciding the strategy based on the static factory method call.

Implementation using Factory Method

PaymentFactory.java

public class PaymentFactory {
    private static CashPayment objCashPayment =  new CashPayment();
    private static DebitCardPayment objDebitCardPayment =  new DebitCardPayment();
    private static CreditCardPayment objCreditCardPayment =  new CreditCardPayment();
    private static UPIPayment objUPIPayment =  new UPIPayment();
    private static CouponPayment objCouponPayment =  new CouponPayment();

    public static PaymentStrategy  payementStrategyFactory(PaymentType paymentType){
        if(paymentType.equals(PaymentType.CASH)){
            return objCashPayment;
        }else if(paymentType.equals(PaymentType.UPI)){
            return objUPIPayment;
        }else if(paymentType.equals(PaymentType.DEBIT_CARD)){
            return objDebitCardPayment;
        }else if(paymentType.equals(PaymentType.CREDIT_CARD)){
            return objCreditCardPayment;
        }else{
            return objCouponPayment;
        }
    }
}

MakePaymentClient.java

public class MakePaymentClient {
    public static void main(String[] args) {
        PaymentStrategy objPaymentStrategy = PaymentFactory.payementStrategyFactory(PaymentType.CREDIT_CARD);
        objPaymentStrategy.paymentMethod("300");
    }
}  
300 paid in credit card

Reference to Factory Pattern

Decorator pattern dynamically changes the functionality of an object at run-time without creating new object rather by adding behavior and attribute to existing Object.

Why Decorator Pattern?

It allows adding new functionality to an existing object without altering its original class. This pattern involves wrapping the original object in a decorator class, which has the same interface as the object it decorates. The decorator class then adds its behavior to the object during runtime, allowing for increased flexibility and modularity in programming.

When Decorator Pattern?
If you want to add additional functionality to an existing object (i.e. already instantiated class at runtime), as opposed to object’s class and/or subclass then Decorator pattern should be used. It is easy to add functionality to an entire class of objects by subclassing an object’s class, but it is impossible to extend a single object this way. With the Decorator Pattern, you can add functionality to a single object and leave others like it unmodified.

How Decorator Pattern Implemented?

  1. Create a Interface with a concrete base class which implements Interface. This base class has only default constructor
  2. Create Concrete Decorator Classes which implements Interface
  3. The Decorator keeps appending the Logs and the log method would be invoked only once after all the appending is done

Logger.java

public interface Logger {
    void  log(String msg);
}

BasicHtmlLogger.java

public class BasicHtmlLogger implements  Logger {
    public BasicHtmlLogger() {
    }

    @Override
    public void log(String msg) {
        System.out.println("<html>" + msg + "</html>");
    }
}

HTMLLoggerDecoratorBold.java

public class HTMLLoggerDecoratorBold implements Logger {
    Logger logger;

    public HTMLLoggerDecoratorBold(Logger logger) {
        this.logger = logger;
    }

    @Override
    public void log(String msg) {
        logger.log("<bold>"+ msg+ "</bold>");
    }
}

HTMLLoggerDecoratorItalic.java

public class HTMLLoggerDecoratorItalic implements Logger {
    Logger logger;

    public HTMLLoggerDecoratorItalic(Logger logger) {
        this.logger = logger;
    }

    @Override
    public void log(String msg) {
        logger.log("<italic>"+ msg+ "</italic>");
    }
}

HTMLLoggerDecoratorUnderline.java

public class HTMLLoggerDecoratorUnderline implements Logger {
    Logger logger;

    public HTMLLoggerDecoratorUnderline(Logger logger) {
        this.logger = logger;
    }

    @Override
    public void log(String msg) {
        logger.log("<underline>"+ msg+ "</underline>");
    }
}

Client.java

public class Client {
    public static void main(String[] args) {
        Logger objBasicLogger = new BasicHtmlLogger();
        Logger objBoldLogger = new HTMLLoggerDecoratorBold(objBasicLogger);
        Logger objItalicLogger = new HTMLLoggerDecoratorItalic(objBasicLogger);
        Logger objUnderlineLogger = new HTMLLoggerDecoratorUnderline(objBasicLogger);

        Logger objBoldItalicLogger = new HTMLLoggerDecoratorItalic(objBoldLogger);

        objBasicLogger.log("Logging Msg without Formatting"); //Basic Logging
        objUnderlineLogger.log("Logging Msg with Underline"); //Basic Logging + Underline
        objBoldLogger.log("Logging Msg with Bold Formatting"); //Basic Logging + Bold
        objItalicLogger.log("Logging Msg with Italic Formatting"); //Basic Logging + Italic
        objBoldItalicLogger.log("Logging Msg with Bold, Italic Formatting"); //Basic Logging + Bold + Italic
    }
}

Output

<html>Logging Msg without Formatting</html>
<html><underline>Logging Msg with Underline</underline></html>
<html><bold>Logging Msg with Bold Formatting</bold></html>
<html><italic>Logging Msg with Italic Formatting</italic></html>
<html><bold><italic>Logging Msg with Bold, Italic Formatting</italic></bold></html>

Below we have one more example of Decorator Pattern

Gift.java

public interface Gift {
    public String addGift();
    public Integer addGiftCost();
}

BasicGift.java

public class BasicGift implements Gift {
    @Override
    public String addGift() {
        return "Gift Item";
    }

    @Override
    public Integer addGiftCost() {
        return 50;
    }
}

GiftFlowersDecorator.java

public class GiftFlowersDecorator implements Gift {
    Gift gift;

    public GiftFlowersDecorator(Gift gift) {
        this.gift = gift;
    }

    @Override
    public String addGift() {
        if(gift != null){
            return gift.addGift() + " + Flowers ";
        }

        return " Flowers to gift Item";
    }

    @Override
    public Integer addGiftCost() {

        if(gift != null){
            return gift.addGiftCost() + 10;
        }

        return 5;
    }
}

GiftGreetingsCardDecorator.java

public class GiftGreetingsCardDecorator implements Gift {
    Gift gift;

    public GiftGreetingsCardDecorator(Gift gift) {
        this.gift = gift;
    }

    @Override
    public String addGift() {
        if(gift != null){
            return gift.addGift() + " + Greetings Card";
        }

        return " Greetings Card";
    }

    @Override
    public Integer addGiftCost() {

        if(gift != null){
            return gift.addGiftCost() + 7;
        }

        return 5;
    }
}

GiftWrapDecorator.java

public class GiftWrapDecorator implements Gift {
    Gift gift;

    public GiftWrapDecorator(Gift gift) {
        this.gift = gift;
    }

    @Override
    public String addGift() {
        if(gift != null){
            return gift.addGift() + " + Wrapper ";
        }

        return " wrapping gift Item";
    }

    @Override
    public Integer addGiftCost() {

        if(gift != null){
            return gift.addGiftCost() + 5;
        }

        return 5;
    }
}

BuyGiftOnline.java

public class BuyGiftOnline {
    public static void main(String[] args) {
        Gift objGift = new BasicGift();
        System.out.println(objGift.addGift() + " = " + objGift.addGiftCost());

        Gift objGiftWithWrapper = new GiftWrapDecorator(objGift);
        System.out.println(objGiftWithWrapper.addGift() + " = " + objGiftWithWrapper.addGiftCost());

        Gift objGiftWithWrapperAndFlowers = new GiftFlowersDecorator(objGiftWithWrapper);
        System.out.println(objGiftWithWrapperAndFlowers.addGift() + "= " + objGiftWithWrapperAndFlowers.addGiftCost());

        Gift objGiftWithWrapperAndInvitation= new GiftGreetingsCardDecorator(objGiftWithWrapper);
        System.out.println(objGiftWithWrapperAndInvitation.addGift() + " = " + objGiftWithWrapperAndInvitation.addGiftCost());
    }
}

Output

Gift Item = 50
Gift Item + Wrapper  = 55
Gift Item + Wrapper  + Flowers = 65
Gift Item + Wrapper  + Greetings Card = 62

Facade design pattern simplifies the interface to a complex system; because it is usually composed of all the classes which make up the subsystems of the complex system

Why to use Facade Pattern?
Classes in all design patterns are just normal classes. What is important is how they are structured and how they work together to solve a given problem in the best possible way.

The Facade design pattern simplifies the interface to a complex system; because it is usually composed of all the classes which make up the subsystems of the complex system.

A Facade shields the user from the complex details of the system and provides them with a simplified view of it which is easy to use. It also decouples the code that uses the system from the details of the subsystems, making it easier to modify the system later.

When to use Facade Pattern?
To provide higher-level interface that makes the subsystem easier to use.

How to implement Facade Pattern?
Instead of exposing the complex code logic we wrap the complex code in a separate class facade class and call the method of Facade Class.

Code Implementation

  1. OnlineApp has many code logic which includes Creating Order, Inventory Updation, Payment Tracking, Seller Buyer Notification and Shipment
  2. Instead of writing all the code in onlineApp we can move the code to FacadeClass(OrderFacade.java) and call the method of OrderFacade

Code without Facade Pattern

OnlineApp.java

public class OnlineApp {
    public static void main(String[] args) {
        System.out.println("Step 1: Order Created");
        Order objOrder = new Order();
        objOrder.placeOrder();
        System.out.println("------------------------");

        System.out.println("Step 2: Inventory Updated");
        Inventory objInventory = new Inventory();
        objInventory.checkInventory("Iphone 13");
        System.out.println("------------------------");

        System.out.println("Step 3: Payment Succesful");
        Payment objPayment = new Payment();
        objPayment.checkPaymentStatus("873901");
        System.out.println("------------------------");

        System.out.println("Step 4: Seller and Buyer Notified");
        Notifications objNotification = new Notifications();
        objNotification.notifyBuyer("873901");
        objNotification.notifySeller("873901");
        System.out.println("------------------------");

        System.out.println("Step 5: Shipment Done");
        Shipment objShipment = new Shipment();
        objShipment.shipProduct("Road Name, Location");
        System.out.println("------------------------");

    }
}

Code with Facade Pattern

OrderFacade.java

public class OrderFacade {
    Order objOrder = new Order();
    Inventory objInventory = new Inventory();
    Payment objPayment = new Payment();
    Notifications objNotification = new Notifications();
    Shipment objShipment = new Shipment();

    public void placeOrder(String orderId) {
        System.out.println("Step 1: Order Created");
        objOrder.placeOrder();
        System.out.println("------------------------");

        System.out.println("Step 2: Inventory Updated");
        objInventory.checkInventory("Iphone 13");
        System.out.println("------------------------");

        System.out.println("Step 3: Payment Succesful");
        objPayment.checkPaymentStatus(orderId);
        System.out.println("------------------------");

        System.out.println("Step 4: Seller and Buyer Notified");
        objNotification.notifyBuyer(orderId);
        objNotification.notifySeller(orderId);
        System.out.println("------------------------");

        System.out.println("Step 5: Shipment Done");
        objShipment.shipProduct("Road Name, Location");
        System.out.println("------------------------");
    }
}

OnlineApp.java

public class OnlineApp {
    public static void main(String[] args) {
        OrderFacade objOrderFacade = new OrderFacade();
        objOrderFacade.placeOrder("873901");
    }
}

Output

Step 1: Order Created
Display Cart Order
------------------------
Step 2: Inventory Updated
------------------------
Step 3: Payment Succesful
------------------------
Step 4: Seller and Buyer Notified
Order placed by Buyer
Order Received by Seller
------------------------
Step 5: Shipment Done
Shipping toRoad Name, Location
------------------------