Professional Documents
Culture Documents
6.1 Solid - Dip.js
6.1 Solid - Dip.js
6.1 Solid - Dip.js
freeze({
parent: 0,
child: 1,
sibling: 2
});
class Person
{
constructor(name)
{
this.name = name;
}
}
// LOW-LEVEL (STORAGE)
class RelationshipBrowser
{
constructor()
{
if (this.constructor.name === 'RelationshipBrowser')
throw new Error('RelationshipBrowser is abstract!');
}
findAllChildrenOf(name) {}
}
addParentAndChild(parent, child)
{
this.data.push({
from: parent,
type: Relationship.parent,
to: child
});
this.data.push({
from: child,
type: Relationship.child,
to: parent
});
}
findAllChildrenOf(name) {
return this.data.filter(r =>
r.from.name === name &&
r.type === Relationship.parent
).map(r => r.to);
}
}
// HIGH-LEVEL (RESEARCH)
class Research
{
// constructor(relationships)
// {
// // problem: direct dependence ↓↓↓↓ on storage mechanic
// let relations = relationships.data;
// for (let rel of relations.filter(r =>
// r.from.name === 'John' &&
// r.type === Relationship.parent
// ))
// {
// console.log(`John has a child named ${rel.to.name}`);
// }
// }
constructor(browser)
{
for (let p of browser.findAllChildrenOf('John'))
{
console.log(`John has a child named ${p.name}`);
}
}
}
// low-level module
let rels = new Relationships();
rels.addParentAndChild(parent, child1);
rels.addParentAndChild(parent, child2);
new Research(rels);