[GO] Observer pattern understood by Nyanko Part 2

Overview

Last time explained the structure of the Observer pattern using two domestic cats and stray cats. This time around the Observer.update function, the cat will begging for something else instead of being soldered. The explanation is based on the contents of Last time, so please refer to that first.

IMG_0756.JPG

Nyanko to appear

** Tama of domestic cat ** Tama lives on the 3rd floor of an apartment. The owner lives alone.

cat_matatabi.png

** Domestic cat peach ** Recently, I have come to live in the same house as Tama.

cat_scottish_fold.png

Tama who cannot be satisfied with soldering

It's not just soldering that Tama asks the owner. I want you to play and I'm worried about Actinidia polygama. There are roughly two ways to tell the owner which request you are making. There are ** Push type ** passed via arguments and ** Pull type ** passed without arguments (using other methods). The previous code has no arguments and is therefore classified as a pull type. For processes that do not need to receive arguments, it is better to use the Pull type. If it's a Pull type and you have the information you need, the Observer class collects it or uses something you know. On the other hand, in the Push type, a character string, a numerical value, an Event class (described later), etc. are passed as arguments.

Tell the owner a request

First of all, make a "request" into a class so that you can make various begging.

Request.java


public abstract class Request{//Event class
    public abstract void words();
}

Food.java


public class Food extends Request{//Event class
    @Overide
    public void words(){
        System.out.println("Nya (I'm hungry)");
    }
}

Playing.java


public class Playing extends Request{//Event class
    @Overide
    public void words(){
        System.out.println("Nya (Hima)");
    }
}

Matatabi.java


public class Matatabi extends Request{//Event class
    @Overide
    public void words(){
        System.out.println("Nya (I want Actinidia polygama!)");
    }
}

uml4.png

Pass the Event class

The following example is a Push type code with a Request class (= Event class) as an argument.

Cat.java


public class Cat{//Subject class
    private Human human;
    
    public void setHuman(Human human){// setObserver()
        this.human = human;
    }

    public void hungry(){// notifyObservers()
        this.human.called(new Food());
    }

    public void killTime(){// notifyObservers()
        this.human.called(new Playing());
    }

    public void wantMatatabi(){// notifyObservers()
        this.human.called(new Matatabi());
    }
}

Human.java


public class Human{//Observer class
    public void called(Request request){// notify() or update()
        request.words();

        String className = request.getClass().getName();
        if(className.equals(Food.class.getName())){
            System.out.println("Give crunchy");

        } else if(className.equals(killTime.class.getName())) {
            System.out.println("It can't be helped anymore (Nekojarashi Furi Furi)");

        } else if(className.equals(Matatabi.class.getName())) {
            System.out.println("Give Actinidia polygama");
        }
    }
}

Main.java


    public static void main(String[] args) {
        Cat tama = new Cat();
        Human master = new Human();
        tama.setHuman(master);

        tama.hungry();
        tama.killTime();
        tama.wantMatatabi();
    }
Nya (I'm hungry)
Give crunchy
Nya (Hima)
It can't be helped anymore (Nekojarashi Furi Furi)
Nya (I want Actinidia polygama!)
Give Actinidia polygama

Pass the Subject class itself (this)

A common value to pass is the calling Subject class (= Cat class) itself. When the Observer class (= Human class) is called from multiple Subject classes, it is convenient because the caller is known.

Cat.java


public class Cat{//Subject class
    private Human human;
    private Request request;
    public final String name;
    
    public Cat(String name){
        this.name = name;
    }

    public void setHuman(Human human){// setObserver()
        this.human = human;
    }

    public void hungry(){// notifyObservers()
        request = new Food();
        this.human.called(this);
    }

    public void killTime(){// notifyObservers()
        request = new Playing();
        this.human.called(this);
    }

    public void wantMatatabi(){// notifyObservers()
        request = new Matatabi();
        this.human.called(this);
    }

    public Request getRequest(){
        return request;
    }
}

Human.java


public class Human{//Observer class
    public void called(Cat cat){// notify() or update()
        cat.getRequest().words();

        String className = cat.getRequest().getClass().getName();
        if(className.equals(Food.class.getName())){
            System.out.println(cat.name + "Give crunchy");

        } else if(className.equals(killTime.class.getName())) {
            System.out.println("It can't be helped anymore (Nekojarashi Furi Furi)");

        } else if(className.equals(Matatabi.class.getName())) {
            System.out.println(cat.name + "Give Actinidia polygama to");
        }
    }
}

Main.java


    public static void main(String[] args) {
        Cat tama = new Cat("Tama");
        Cat momo = new Cat("peach");
        Human master = new Human();
        tama.setHuman(master);
        momo.setHuman(master);

        tama.hungry();
        tama.killTime();
        momo.hungry();
        tama.wantMatatabi();
    }
Nya (I'm hungry)
Give Tama a crunch
Nya (Hima)
It can't be helped anymore (Nekojarashi Furi Furi)
Nya (I'm hungry)
Give the peach crunchy
Nya (I want Actinidia polygama!)
Give Actinidia polygama to Tama

The Java Observer class accepts the calling Subject class (named the Observable class in Java) and the Object in general as arguments.

in conclusion

I'm still not sure whether to use the Push type or the Pull type (studying). Please take a look at the reference materials.

Last time was very happy to receive many likes. Thank you very much. I was quite surprised (I took a screenshot) when the previous article appeared in the qiita ranking that I usually see. I hope this article is also useful to you.

Reference material

[1][https://qiita.com/varmil/items/8cd8fe9da510e31d940a] [2][http://d.hatena.ne.jp/backpaper0/20111111/1321012118] [3][http://apsec99-www.se.cs.titech.ac.jp/local/material/design-patterns/resources/19.html]

Recommended Posts

Observer pattern understood by Nyanko Part 2
Observer pattern understood by cats Part 1
Design Pattern #Observer
Observer pattern in Java
Sample source of Observer pattern realized by Java, PHP, Python