Avinash
Avinash

Reputation: 13257

Design pattern suggestion

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.

Upvotes: 1

Views: 614

Answers (4)

jay_soo
jay_soo

Reputation: 1328

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.

Upvotes: 0

Bozho
Bozho

Reputation: 597006

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.

Upvotes: 5

Pete Kirkham
Pete Kirkham

Reputation: 49311

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.

Upvotes: 0

this. __curious_geek
this. __curious_geek

Reputation: 43207

use Observer Pattern

alt text

Upvotes: -1

Related Questions