UML类图:
image.png
代码模拟:
class Product {
constructor(name){
this.name = name
}
init() {
}
fn1() {
}
fn2(){
}
}
class Creator {
create(name) {
return new Product(name)
}
}
实例:
React.createElement
var profile = React.createElement("div", null,
React.createElement("img", {src: "avatar.png", className: "profile"}),
React.createElement("h3", null, [user.firstName, user.lastName].join(" "))
)
class Vnode(tag, attrs, children) {
// ...省略内部代码
}
React.createElement = function(tag, attrs, children){
return new Vnode(tag, attrs, children)
}
工厂模式设计原则:
- 构造函数和创建者分离
- 符合开放封闭原则
网友评论