Before you understand
how to use Macaroni framework, you probably
want to know
why you should use it.
This article answers why the Macaroni framework has appeared.
C Motivation
We do set 2 simple aims:
- Simplify things
- Reduce dependecies
The questions are: What we want to simplify? What dependencies we want to remove?
Say, you want a class to produce events and another class to
consume them... Have a look at the classic
observer
pattern (a.k.a.
Publish/subscribe
or pub/sub):
Let's define those abstractions in a simple code:
1
public interface Subject { public void attach(Observer aListener); }
2
To be able to distribute evens among any class implementing a specific interface, you have to have an abstract observer interface
public interface Observer { public void update(); }
3
Now, let's implements something..
If you want a class be able to produce events, you have to
maintain a collection of observers (event listeners).
Furthermore, you have to maintain the event distribution
process itself:
import java.util.Collection; public class ConcreteSubject implements Subject{ private Collection<Observer> observers; //..... public void attach(Observer observer) { observers.add(observer); } //..... public void notifyObservers(){ for(Observer observer: observers){ observer.update(); } } }
4
Off course, you need an observer implementation. Not a big deal!
public class ConcreteObserver implements Observer{ public void update() { // DO something really useful } }
5
Finally, you need something to assemble the things together and make it run:
public class Assembler { public void runIt(){ Subject subject=new ConcreteSubject(); Observer observer=new ConcreteObserver(); subject.attach(observer); } }
Now let's see what we have.. We do have the 5 classes coupled together.
- ConcreteSubject + Observer + Subject
- ConcreteObserver+ ConcreteObserver + Subject
- Observer + Subject
- Assemler + ConcreteSubject + ConcreteObserver
So, we have 5 classes with about 6 dependencies among them.
Macaroni framework provides little bit simplier model:
TODO
TODO
TODO