-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.js
32 lines (27 loc) · 982 Bytes
/
interpreter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const ExpressionOr = require('./expressionOr')
const ExpressionAnd = require('./expressionAnd')
const ExpressionTerminal = require('./expressionTerminal')
class Application {
// Rule: Cats and Dogs are home pets
_getHomePetExpression() {
const cats = new ExpressionTerminal('Cats')
const dogs = new ExpressionTerminal('Dogs')
return new ExpressionOr(cats, dogs)
}
// Rule: Cats like milk
_getCatsLikeExpression() {
const cats = new ExpressionTerminal('Cats')
const milk = new ExpressionTerminal('Milk')
return new ExpressionAnd(cats, milk)
}
run() {
const isHomePet = this._getHomePetExpression()
const likesMilk = this._getCatsLikeExpression()
console.log(`Are Dogs home pets? ${isHomePet.interpret('Dogs')}`) // true
console.log(`Are Cats like milk? ${likesMilk.interpret('Cats Milk')}`) // true
console.log(`Are Dogs like milk? ${likesMilk.interpret('Digs Milk')}`) // false
}
}
module.exports = {
Application
}