Last active
May 3, 2020 17:00
-
-
Save wintercn/c61f66b8ee067638ca5343fd2db62aba to your computer and use it in GitHub Desktop.
JS Factory demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const categories = [ | |
{id:'animals', parent:null}, | |
{id:'mammals', parent:'animals'}, | |
{id:'cats', parent:'mammals'}, | |
{id:'dogs', parent:'mammals'}, | |
{id:'chihuahua', parent:'dogs'}, | |
{id:'labrador', parent:'dogs'}, | |
{id:'persian', parent:'cats'}, | |
{id:'siamese', parent:'cats'}, | |
] | |
//经典Abstract Factory | |
class AbstractTreeNode { | |
constructor(data){ | |
this.id = data.id; | |
this.children = []; | |
this.map = new Map(); | |
this.map.set(this.id, this); | |
} | |
insert(factory, item){ | |
const node = factory.create(item); | |
if(this.map.get(item.parent)) | |
this.map.get(item.parent).append(node); | |
} | |
append(node){ | |
node.parent = this; | |
this.children.push(node); | |
var current = this; | |
while(current) { | |
current.map.set(node.id, node); | |
current = current.parent; | |
} | |
} | |
} | |
AbstractTreeNode.fromItems = function fromItems(factory, items) { | |
let root = null; | |
for(let item of items) { | |
if(item.parent != null) | |
root.insert(factory, item); | |
else | |
root = factory.create(item); | |
} | |
return root; | |
} | |
class TreeNode1 extends AbstractTreeNode { | |
} | |
class AbstractTreeNodeFactory { | |
} | |
class TreeNode1Factory extends AbstractTreeNodeFactory{ | |
create(item){ | |
return new TreeNode1(item); | |
} | |
} | |
AbstractTreeNode.fromItems(new TreeNode1Factory, categories) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const categories = [ | |
{id:'animals', parent:null}, | |
{id:'mammals', parent:'animals'}, | |
{id:'cats', parent:'mammals'}, | |
{id:'dogs', parent:'mammals'}, | |
{id:'chihuahua', parent:'dogs'}, | |
{id:'labrador', parent:'dogs'}, | |
{id:'persian', parent:'cats'}, | |
{id:'siamese', parent:'cats'}, | |
] | |
//经典Abstract Factory | |
const abstractTreeNodePrototype = { | |
init(data) { | |
this.id = data.id; | |
this.children = []; | |
this.map = new Map(); | |
this.map.set(this.id, this); | |
}, | |
insert(item){ | |
const node = Object.create(this); | |
node.init(item); | |
if(this.map.get(item.parent)) | |
this.map.get(item.parent).append(node); | |
}, | |
append(node){ | |
node.parent = this; | |
this.children.push(node); | |
var current = this; | |
while(current) { | |
current.map.set(node.id, node); | |
current = current.parent; | |
} | |
}, | |
fromItems(items) { | |
let root = null; | |
for(let item of items) { | |
if(item.parent != null) | |
root.insert(item); | |
else { | |
root = Object.create(this); | |
root.init(item); | |
} | |
} | |
return root; | |
} | |
}; | |
const tree1NodePrototype = Object.create(abstractTreeNodePrototype) | |
tree1NodePrototype.fromItems(categories) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment