mardi 24 septembre 2019

Chain of resposibility in Java

currently, I'm trying to understand the chain of responsibility, but there's some piece of code in it, that I can't understand at all. Let's start from begining.

For first, I have to create an interface:

public interface ChainLink {
void setNextChain(ChainLink link);
void process(Model model);
}

Next step is creating the pojo class:

public class Model {

private int number;

public Model(int number) {
    this.number = number;
}

public int getNumber() {
    return number;
 }
}

Now, I have to create three separated classes, for each process:

First is for negative number

public class NegativeNumber implements ChainLink {

ChainLink link;
@Override
public void setNextChain(ChainLink link) {
    this.link = link;
}
@Override
public void process(Model model) {
    if (model.getNumber() < 0){
        System.out.println("Given number is "+model.getNumber()+" and it's negative");
    }else {
        link.process(model);
    }
 }
}

Second one is for number equals to 0

public class ZeroNumber implements ChainLink {
ChainLink link;
@Override
public void setNextChain(ChainLink link) {
    this.link = link;
}

@Override
public void process(Model model) {
    if (model.getNumber() == 0){
        System.out.println("Given number is equal to 0");

    }else {
        link.process(model);
    }
 }
}

And the third one, for possitive numbers

public class PositiveNumber implements ChainLink{
private ChainLink link;
@Override
public void setNextChain(ChainLink link) {
    this.link = link;
}

@Override
public void process(Model model) {
    if (model.getNumber() > 0){
        System.out.println("Given number is "+model.getNumber()+" and it's positive");

    }else {
        link.process(model);
    }
 }
}

Now, I have to chain it up on Main class.

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    ChainLink negativeLink = new NegativeNumber();
    ChainLink zeroLink = new ZeroNumber();
    ChainLink positiveLink = new PositiveNumber();

    negativeLink.setNextChain(zeroLink);
    zeroLink.setNextChain(positiveLink);

    Scanner scanner = new Scanner(System.in);

    while (true){
        int number;
        System.out.println("Enter the number");
        number = scanner.nextInt();
        negativeLink.process(new Model(number));
    }
 }
}

So the thing that I'd like to make sure about, is how the process class are working. So in first, we're creating the instance of our interface, and then overriding methods in it. And now I'm not sure if I understand how "setNewChain" method works, is it just about taking the instance of our interface which is "link" (not this in method parameter) and overwriting it with another interface process? (in this case, if number is not less than 0, then we have to call the another chain which is "ZeroNumber" because this one, can't do much here, because number is >=0 and so on.

Aucun commentaire:

Enregistrer un commentaire