// Common components
var
withSerializableTypeProxy = (function () {
function toString(type) {
return JSON.stringify(type);
}
function valueOf(type) {
return JSON.parse(JSON.stringify(type))
}
let defineProperty = Object.defineProperty;
return function trait (stateValue) {
const
compositeType = this;
defineProperty(compositeType, 'toString', {
value: (() => toString(stateValue))
});
defineProperty(compositeType, 'valueOf', {
value: (() => valueOf(stateValue))
});
};
}());
function withChangeableColorProxy(stateValue) {
const
compositeType = this;
compositeType.changeColor = ((colorValue) => stateValue.color = colorValue);
Object.defineProperty(compositeType, 'color', {
enumerable: true,
get: (() => stateValue.color)
});
stateValue.color = 'white'; // set default value.
}
class Switch {
constructor(stateValue) {
stateValue = ((stateValue != null) && (typeof stateValue == 'object') && stateValue) || {};
stateValue.isOpen = true;
const
switchType = this;
withSerializableTypeProxy.call(switchType, stateValue);
switchType.open = (() => stateValue.isOpen = true);
switchType.close = (() => stateValue.isOpen = false);
Object.defineProperty(this, 'isOpen', {
enumerable: true,
get: (() => stateValue.isOpen)
});
}
}
class LightSwitch extends Switch {
constructor(stateValue) {
stateValue = ((stateValue != null) && (typeof stateValue == 'object') && stateValue) || {};
super(stateValue);
withChangeableColorProxy.call(this, stateValue);
}
}
// Product A
var
withBlinkBehaviorProxy = (function () {
function setBlinkValue(stateValue, blinkValue) {
return stateValue.blink = blinkValue;
}
return function trait (stateValue) {
const
compositeType = this;
Object.defineProperty(compositeType, 'blink', {
enumerable: true,
get: (() => stateValue.blink),
set: ((blinkValue) => setBlinkValue(stateValue, blinkValue))
});
setBlinkValue(stateValue, false); // set default value.
};
}());
class LedSwitch extends LightSwitch {
constructor(stateValue) {
stateValue = ((stateValue != null) && (typeof stateValue == 'object') && stateValue) || {};
super(stateValue);
withBlinkBehaviorProxy.call(this, stateValue);
}
}
var
ledSwitch = (new LedSwitch);
console.log("ledSwitch : ", ledSwitch);
console.log("(ledSwitch instanceof LedSwitch) ? ", (ledSwitch instanceof LedSwitch));
console.log("(ledSwitch instanceof LightSwitch) ? ", (ledSwitch instanceof LightSwitch));
console.log("(ledSwitch instanceof Switch) ? ", (ledSwitch instanceof Switch));
console.log("ledSwitch.valueOf() : ", ledSwitch.valueOf());
console.log("Object.keys(ledSwitch) : ", Object.keys(ledSwitch));
console.log("(ledSwitch.blink = 'blink') : ", (ledSwitch.blink = 'blink'));
console.log("ledSwitch.blink : ", ledSwitch.blink);
console.log("(ledSwitch.blink = true) : ", (ledSwitch.blink = true));
console.log("ledSwitch.blink : ", ledSwitch.blink);
// Product B
// AlarmSwitch and ProductBLightSwitch both need a "timer"
function withPlayerBehaviorProxy(stateValue) {
const
compositeType = this;
compositeType.changeSound = ((soundValue) => stateValue.sound = soundValue);
Object.defineProperty(compositeType, 'sound', {
enumerable: true,
get: (() => stateValue.sound)
});
stateValue.sound = 'alarm.wav'; // set default value.
}
function withTimerBehaviorProxy(stateValue) {
const
compositeType = this;
compositeType.resetTimer = (() => stateValue.timer = 0);
Object.defineProperty(compositeType, 'timer', {
enumerable: true,
get: (() => stateValue.timer)
});
stateValue.timer = 5000; // set default value.
}
class AlarmSwitch extends Switch {
constructor(stateValue) {
stateValue = ((stateValue != null) && (typeof stateValue == 'object') && stateValue) || {};
super(stateValue);
withPlayerBehaviorProxy.call(this, stateValue);
withTimerBehaviorProxy.call(this, stateValue);
}
}
class ProductBLightSwitch extends LightSwitch {
constructor(stateValue) {
stateValue = ((stateValue != null) && (typeof stateValue == 'object') && stateValue) || {};
super(stateValue);
withTimerBehaviorProxy.call(this, stateValue);
}
}
var
lightSwitch_B = (new ProductBLightSwitch),
alarmSwitch = (new AlarmSwitch);
console.log("lightSwitch_B : ", lightSwitch_B);
console.log("alarmSwitch : ", alarmSwitch);
console.log("(lightSwitch_B instanceof ProductBLightSwitch) ? ", (lightSwitch_B instanceof ProductBLightSwitch));
console.log("(alarmSwitch instanceof AlarmSwitch) ? ", (alarmSwitch instanceof AlarmSwitch));
console.log("(lightSwitch_B instanceof LightSwitch) ? ", (lightSwitch_B instanceof LightSwitch));
console.log("(alarmSwitch instanceof LightSwitch) ? ", (alarmSwitch instanceof LightSwitch));
console.log("(lightSwitch_B instanceof Switch) ? ", (lightSwitch_B instanceof Switch));
console.log("(alarmSwitch instanceof Switch) ? ", (alarmSwitch instanceof Switch));
console.log("lightSwitch_B.valueOf() : ", lightSwitch_B.valueOf());
console.log("alarmSwitch.valueOf() : ", alarmSwitch.valueOf());
console.log("Object.keys(lightSwitch_B) : ", Object.keys(lightSwitch_B));
console.log("Object.keys(alarmSwitch) : ", Object.keys(alarmSwitch));
console.log("lightSwitch_B.resetTimer() : ", lightSwitch_B.resetTimer());
console.log("lightSwitch_B.timer : ", lightSwitch_B.timer);
console.log("alarmSwitch.changeSound('ringtone.wav') : ", alarmSwitch.changeSound('ringtone.wav'));
console.log("alarmSwitch.sound : ", alarmSwitch.sound);
.as-console-wrapper { max-height: 100%!important; top: 0; }