创建型

工厂模式

思想:用于批量创建相似对象 或 完成相似功能。

class A {}
function factory() {
  return new A();
}

建造者

思想:将复杂功能模块拆分,再组合起来。

class Editor {
  this.text = new Text();
  this.img = new Img();
  this.action = new Action();
}

class Text{}
class Img {}
class Action{}

单例模式

思想:全局有且只有一个实例。

class A {
  getInstance() {
    if (this.instance) {
      return this.instance;
    }
    return (this.instance = new A());
  }
}
posted @   梦渊同学  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示