views:

112

answers:

4

Following is the problem statement.

There are n numbers of match strings, If event A occurs and then in certain period of time event B occurs then I do not raise alarm. but if B do not occurs then i have to raise alarm. There can be multiple chain of events which defines whether to raise alarm or not.

A: 

use Observer Pattern

alt text

this. __curious_geek
at least explain what the pattern has to do with the problem statement
Pete Kirkham
+5  A: 

The State pattern. You will have something like this (from your perspective):

When A occurs, change the state to such, where the occurrence of B resets the state. If B occurs in the initial state, raise alarm.

Check the linked article to see how to implement this behaviour.

Bozho
Your explanation sounds more like a procedural state machine than the OO state pattern - setting an int variable to 1. (it also doesn't match the rules in the OP)
Pete Kirkham
"one" = "the state" in my explanation :) (or "to one" = "to such"). Edited for more clarity.
Bozho
The state object pattern is that the state (A has occurred) handles the transition. There is no 'resetting of the state' or 'if' condition that tests whether it is the initial state when B occurs.
Pete Kirkham
I guess I'd know this when suggesting the pattern ;)The behaviour I explained was from the "user perspective", in order to fit the OP expectations. How it is handled internally is shown in the linked page. And there is resetting of the state - you just set the initial state ;)
Bozho
A: 

Your problem statement is for something like the level of a software module. A module will typically contain many classes, and these classes will relate to each other in different ways. Patterns provide both views on these relationships, and sometimes suggestions for how behaviours can be achieved using different relationships between classes.

It is likely that some kind of state machine will be involved, though whether this is a simple state machine, or the state object pattern, or whether an interpreter for rules, or a procedural machine created by transforming the rules using a visitor; whether the states are triggered by polling data sources or using observers, and whether the system can be composed from simpler state machines, or some kind of scheduler is needed to run many machines at once all depends on more detail than what is given in your post.

Pete Kirkham
A: 

Observer Pattern:

var observer = new Observer();
var eventA = new EventA();
var eventB = new EventB();

eventA.register(observer, function() {
    // set off alarm in N amount of time units
});
eventB.register(observer, function() {
    // reset alarm
});

Then later on eventA and eventB will call notify() for all registered observers, which will trigger the callbacks for starting alarm countdown / reseting alarm.

Code is in pesudo-javascript. If you are using full out javascript, simply use setTimeout and clearTimeout in the callbacks.

I guess I should mention that a Pub/Sub pattern (which Observer is a subset of) is perfectly good as well.

jay_soo