5.1 Solid - Isp.js
5.1 Solid - Isp.js
mixins) => {
class base extends baseClass {
constructor (...args) {
super(...args);
mixins.forEach((mixin) => {
copyProps(this,(new mixin));
});
}
}
let copyProps = (target, source) => { // this function copies all properties and
symbols, filtering out some special ones
Object.getOwnPropertyNames(source)
.concat(Object.getOwnPropertySymbols(source))
.forEach((prop) => {
if (!prop.match(/^(?:constructor|prototype|arguments|caller|name|bind|call|
apply|toString|length)$/))
Object.defineProperty(target, prop,
Object.getOwnPropertyDescriptor(source, prop));
})
};
mixins.forEach((mixin) => {
// outside constructor() to allow aggregation(A,B,C).staticFunction() to be
called etc.
copyProps(base.prototype, mixin.prototype);
copyProps(base, mixin);
});
return base;
};
class Document
{
class Machine
{
constructor()
{
if (this.constructor.name === 'Machine')
throw new Error('Machine is abstract!');
}
print(doc) {}
fax(doc) {}
scan(doc) {}
}
fax(doc) {
//
}
scan(doc) {
//
}
}
// fax(doc) {
// // do nothing
// }
scan(doc) {
// throw new Error('not implemented!');
throw new NotImplementedError(
'OldFashionedPrinter.scan')
}
}
// solution
class Printer
{
constructor()
{
if (this.constructor.name === 'Printer')
throw new Error('Printer is abstract!');
}
print(){}
}
class Scanner
{
constructor()
{
if (this.constructor.name === 'Scanner')
throw new Error('Scanner is abstract!');
}
scan(){}
}
scan()
{
//
}
}