jeudi 19 décembre 2019

What exactly is the use of Chain of responsibility Design pattern? [duplicate]

This question already has an answer here:

What exactly is the use of Chain of responsibility Design pattern? Please explain with a scenario in which you have used this design pattern.Please provide a practical scenario where you might have used chain of responsibility?

abstract class RupeesHandler
{
    RupeesHandler rupeesHandler;

    public void nextHandler( RupeesHandler rupeesHandler)
    {
        this.rupeesHandler= rupeesHandler;
    }

    public abstract void fetchRupees( long givenAmount );

}

class FiveHundredRupeesHandler extends RupeesHandler
{

    public void fetchRupees(long givenAmount)
    {
        long numberofNotesToBeFetched = givenAmount / 500;
        if (numberofNotesToBeFetched > 0)
        {
            if(numberofNotesToBeFetched >1)
            {
                System.out.println(numberofNotesToBeFetched + " ,Five Hundred Rupees notes are Fetched by FiveHundredRupeesHandler \n");
            }
            else
            {
                System.out.println(numberofNotesToBeFetched + " ,Five Hundred Rupees note is Fetched by FiveHundredRupeesHandler \n");
            }
        }

        long pendingRupeesToBeProcessed = givenAmount % 500;

        if (pendingRupeesToBeProcessed > 0)
        {
            rupeesHandler.fetchRupees(pendingRupeesToBeProcessed);

        }

    }

}

    class HundredRupeesHandler extends RupeesHandler
     {

    public void fetchRupees(long givenAmount)
    {
        long numberofNotesToBeFetched = givenAmount / 100;
        if (numberofNotesToBeFetched > 0)
        {
            if(numberofNotesToBeFetched >1)
            {
                System.out.println(numberofNotesToBeFetched + " ,Hundred Rupees notes are Fetched by HundredRupeesHandler \n");
            }
            else
            {
                System.out.println(numberofNotesToBeFetched + " ,Hundred Rupees note is Fetched by HundredRupeesHandler \n");

            }
        }

        long pendingRupeesToBeProcessed = givenAmount % 100;

        if (pendingRupeesToBeProcessed > 0)
        {
            rupeesHandler.fetchRupees(pendingRupeesToBeProcessed);
        }
    }
}


class FiftyRupeesHandler extends RupeesHandler
{

    public void fetchRupees(long givenAmount)
    {
        long numberofNotesToBeFetched = givenAmount / 50;
        if (numberofNotesToBeFetched > 0)
        {
            if(numberofNotesToBeFetched >1)
            {
                System.out.println(numberofNotesToBeFetched + " ,Fifty Rupees notes are Fetched by FiftyRupeesHandler \n");
            }
            else
            {
                System.out.println(numberofNotesToBeFetched + " ,Fifty Rupees note is Fetched by FiftyRupeesHandler \n");
            }
        }

        long pendingRupeesToBeProcessed = givenAmount % 50;

        if (pendingRupeesToBeProcessed > 0)
        {
            rupeesHandler.fetchRupees(pendingRupeesToBeProcessed);
        }
    }
}

class Kiosk
{
    private static FiveHundredRupeesHandler fivehundredRupeesHandler = new FiveHundredRupeesHandler();
    private static HundredRupeesHandler hundredRupeesHandler = new HundredRupeesHandler();

    static
    {
        // Construct the chain of Rupees Handlers
        fivehundredRupeesHandler.nextHandler(hundredRupeesHandler);
        hundredRupeesHandler.nextHandler(fiftyRupeesHandler);
    }

    public void payment( long givenAmount )
    {
        fivehundredRupeesHandler.fetchRupees(givenAmount);
    }
}

public class ChainOfResponsibility {
    public static void main( String[] args )
    {
        Kiosk kiosk = new Kiosk();

        System.out.println("\n ----------------Paid Amount 1755-----------------------\n");

        kiosk.payment(1755);

        System.out.println("\n ----------------Paid Amount 2275-----------------------\n");

        kiosk.payment(2275);
    }
}

I am creating multiple handlers . I could have done everything in a single class creating multile methods. For Eg -

void fivehundred(int givenAmount){
    long numberofNotesToBeFetched = givenAmount / 500;

    if (pendingRupeesToBeProcessed > 0)
    {
        getHundredRupees(pendingRupeesToBeProcessed);
    }
}  

getHundredRupees(int pendingRupees){
     // and further methods
}

What is the issue with this way? Please provide a practical scenario where you might have used chain of responsibility?

Aucun commentaire:

Enregistrer un commentaire