# --------------------------------------------------
class Beverage
is abstract
get description as String
ensure
result.length > 0
body
return 'Unknown Beverage'
get cost as decimal is abstract
invariant
.cost > 0.0
# --------------------------------------------------
class CondimentDecorator
is abstract
inherits Beverage
var _beverage as Beverage
pro beverage as Beverage is protected
get
return _beverage
set
_beverage = value
get description as String is override, abstract
def init(beverage as Beverage)
.beverage = beverage
# --------------------------------------------------
class Espresso
inherits Beverage
get description as String is override
return 'Espresso'
get cost as decimal is override
return 1.99
test
b as Beverage = Espresso()
assert b.description == 'Espresso'
assert b.cost == 1.99
# --------------------------------------------------
class Mocha
inherits CondimentDecorator
get description as String is override
return '[.beverage.description], Mocha'
get cost as decimal is override
return 0.20 + .beverage.cost
def init(beverage as Beverage)
base.init(beverage)
test
b as Beverage = Espresso()
b = Mocha(b)
assert b.description == 'Espresso, Mocha'
assert b.cost == 1.99 + 0.20
The test in Mocha fails because when the constructor in the abstract class CondimentDecorator is called, at the end of that constructor the invariant from the parent class (Beverage) is not satisfied. Two questions arise:
1) Should the invariant be called after CondimentDecorator's constructor? It would seem that all invariant checking should be delayed until the end of the original constructor.
2) For me, using Cobra 0.8.0, this problem resulted in an unhandled null reference exception. If invariant handling should operate in this fashion, it would seem that this should be handled in a cleaner fashion.
Let me know and best regards as always,
Steve
--