要说起设计模式,真的是前人的智慧,今天刚学设计模式,理解里面的精髓后简直被震惊了一番,随后尝试着把工厂模式也融入到js里,经过努力终于成功,也正好符合java要求的开闭原则
创建型模式的概念分两种:
1.对外要隐藏里面复杂的实现逻辑跟处理过程,让调用者只关心执行结果
2.对工厂来说返回的结果一定要保持准确性跟真实性,返回的数据要符合规范
以下的每个代码快都是一个文件里的内容,没有用到任何框架,原生写的,也很基础,不难理解
1、
class interfaceImpl{
constructor(){
this.arr = new interfaces();
}
// 继承
getMenniu() {
return this.arr.getSanlu.call(this)
}
getYili() {
return this.arr.getYili.call(this)
}
}
2、
class interfaces{
constructor(){
}
getSanlu() {
return new Sanlu();
}
getYili() {
return getYili();
}
}
3、
class Sanlu{
getSanlu(){
return "三鹿";
}
getPrice() {
return 58;
}
}
4、页面调用
<script type="text/javascript">
const a = new interfaceImpl();
console.log(a.getMenniu().getPrice())
console.log(a.getYili())
</script>