Pages

Wednesday, December 30, 2015

Experimenting with humans and elfs

Everybody its on holiday, just arrived and It seems like I am the only muppet at the office today.
Oh, cool some body left a designs patterns book on the desk. Hehehe...



I guess it was that communist elf again.  See? that little man even forgot his hat!  


Wait, hold on a sec... ARGGGHHH...! WednesdayThursdayFriday!! My banana case its empty?????? The elf ate my banana!!!

Ok let's calm down its just a banana, the poor guy had to go back to the north pole so probably toke it for the way back.

Anyway, what I do to day?  Let's see:

- Pile of tech debt waiting for a miracle. 
    Uhm... I wonder if Jesus would be able to unblock me if I get started with any of this today.

- Second pile of tech debt. 
     Mayyybe, but wait! there are no story numbers there, better I don't mess up with the process ;)

- Swarm with the tester that just arrived. 
     Regression testing you said?? Yes, yes... sure, all the suffering will end one day, yep... Ok lets continue this conversation next week.

- Pick new story
     Opsy, board is empty! No BA's around, Well, I better mind my own business then.



All right! let's have a look at this book the elf left for me. 

Strategy, Template, Builder... Wow so many patterns which one I read about?

Oh screw the reading!, lets just do some coding. 

Ok, what I code about? Its xmas, let's think about some xmas related code...
...

Oh screw that too, lets just do some human experimentation or something. 

Yes, thats it! Let's experiment with humans! No, no better, let's experiment with elfs hahaha.... 
Im so evil sometimes!

So, what about this one?: Imagine I catch that elf and I torture him in a machine that plays christmas carols infinitely and I log all his reactions, like if it was a proper scientific research.

Mmmm... sounds like an events driven system to me. 

Ok! 

So let's refresh my mind, how did I do events in Java some time ago? 

Let's spike that a little bit for now, this book may be handy I will keep it close.


I said I would create a person, and I would put it in a machine and see how it reacts to the sounds the machine plays. In order to react, to sounds, I better make sure that person has ears(the listener). 


public class Main {
    public static void main(String[] args) {

        SomeListener ears = new Ears();

        Person person = new Person();
        person.addListener(ears);

        SoundsMachine soundsMachine = new SoundsMachine(person);
        soundsMachine.generateSound(new RainSound());
        soundsMachine.generateSound(new CreepyDoorKnobSound());

    }

}


Ok, cool. I remember now. 

So the important thing here its the person because for it to react, it will need to have different types of listeners. But those listeners, may react to specific events only. Well, the events could also be generic something like an interface(function pointer).

Yes, let's keep it simple. I think that is better since the specifics of each reaction could be programmed separately in each different listener...


public class Person {

    private List<SomeListener> listeners = new ArrayList<SomeListener>();

    public void addListener(SomeListener someListener) {
        listeners.add(someListener);
    }

    public void noticeWhatItsGoingOn(SoundEvent soundEvent) {
        for (SomeListener someListener: listeners) {
            someListener.perceive(soundEvent);
        }
    }
}




Fine, so let's then look closer at the ears listener Im using in this example.


public class Ears implements SomeListener {

    public void perceive(SoundEvent soundEvent) {
        System.out.print("Uhm, I hear something: ");
        soundEvent.produceSound();
        System.out.println("I think its " + determineSound(soundEvent) + "!");
        lineSeparator();
    }

    private String determineSound(SoundEvent soundEvent) {
        if (soundEvent instanceof RainSound)
            return "rain";
        else if(soundEvent instanceof CreepyDoorKnobSound) {
            return "a door opening. Hopefully I will be out of here soon";
        }
        return "I can't recognize that sound";
    }
}



So that implements the Listener interface which is basically a functional interface.


public interface SomeListener {
    public void perceive(SoundEvent soundEvent);
}



And that's it then the only thing its just to have a look at the implementation of the events that are sent.


public interface SoundEvent {
    public void produceSound();
}

public class RainSound implements SoundEvent {

    public void produceSound() {
        System.out.println("plop, plop, plop....");
    }
}

public class CreepyDoorKnobSound implements SoundEvent {
    public void produceSound() {
        System.out.println("KnJjjeeeeee...");
    }
}



I will sketch this to summarise:





The listening object(Person) is used by the application code which is what uses it and also where the action of registering the listeners its done(Main and SoundMachine). 

Also that listening object holds all the listeners in a list and acts as a gateway, to where the events are sent (noticeWhatItsGoingOn() method).

This was Javing's last post of 2015

Merry Xmas and prosperous new Year!



                              

When it’s the 5th one who tells me ‘see you next year’ when leaving the office




No comments:

Post a Comment

Share with your friends