当前位置: 首页 > wzjs >正文

ppt那个网站做的好建设网站总结

ppt那个网站做的好,建设网站总结,wordpress getterms,wordpress新建页面连接无法访问一、模式定义与核心价值 单例模式(Singleton Pattern)是一种创建型设计模式,保证一个类仅有一个实例,并提供全局访问点。其核心价值在于: ​​资源控制​​:避免重复创建消耗性资源(如数据库连…

一、模式定义与核心价值

单例模式(Singleton Pattern)是一种创建型设计模式,保证一个类仅有一个实例,并提供全局访问点。其核心价值在于:

  1. ​资源控制​​:避免重复创建消耗性资源(如数据库连接)
  2. ​状态共享​​:维护全局唯一状态(如应用配置)
  3. ​访问管控​​:集中管理共享资源访问(如日志系统)

二、经典实现方案对比

1. 闭包实现(ES5)

const Singleton = (() => {let instance = null;function createInstance() {// 私有方法和属性const privateMethod = () => console.log('Private method');let privateVar = 'Initial value';return {// 暴露的公共接口publicMethod: () => {privateMethod();console.log('Public method called');},getVar: () => privateVar,setVar: (value) => { privateVar = value }};}return {getInstance: () => {if (!instance) {instance = createInstance();}return instance;}};
})();// 使用示例
const instance1 = Singleton.getInstance();
const instance2 = Singleton.getInstance();
console.log(instance1 === instance2); // true

2. 类静态属性(ES6+)

class DatabaseConnection {static instance = null;connectionCount = 0;constructor() {if (DatabaseConnection.instance) {return DatabaseConnection.instance;}// 模拟耗时的连接初始化this.connectionCount = 0;DatabaseConnection.instance = this;}connect() {this.connectionCount++;console.log(`Active connections: ${this.connectionCount}`);}disconnect() {this.connectionCount = Math.max(0, this.connectionCount - 1);}
}// 使用示例
const db1 = new DatabaseConnection();
const db2 = new DatabaseConnection();
db1.connect(); // Active connections: 1
db2.connect(); // Active connections: 2
console.log(db1 === db2); // true

3. 模块模式(现代ES Module)

// config.js
let configInstance = null;export default class AppConfig {constructor() {if (!configInstance) {this.env = process.env.NODE_ENV || 'development';this.apiBase = this.env === 'production' ? 'https://api.example.com' : 'http://localhost:3000';configInstance = this;}return configInstance;}// 添加配置冻结防止修改freeze() {Object.freeze(this);}
}// 初始化并冻结配置
const config = new AppConfig();
config.freeze();

三、高级应用场景

1. 带生命周期的单例

class SessionManager {static instance = null;static getInstance() {if (!this.instance) {this.instance = new SessionManager();// 注册页面卸载清理window.addEventListener('beforeunload', () => {this.instance.cleanup();});}return this.instance;}constructor() {this.sessions = new Map();this.timeouts = new Map();}createSession(userId, ttl = 3600) {const sessionId = crypto.randomUUID();this.sessions.set(sessionId, { userId, created: Date.now() });// 自动过期处理this.timeouts.set(sessionId, setTimeout(() => {this.destroySession(sessionId);}, ttl * 1000));return sessionId;}destroySession(sessionId) {clearTimeout(this.timeouts.get(sessionId));this.sessions.delete(sessionId);this.timeouts.delete(sessionId);}cleanup() {this.timeouts.forEach(clearTimeout);this.sessions.clear();this.timeouts.clear();}
}// 使用示例
const sessionManager = SessionManager.getInstance();
const sessionId = sessionManager.createSession('user123');

四、实践建议与注意事项

1. 合理使用场景

✅ 适用场景:

  • 全局状态管理(Redux/Vuex Store)
  • 浏览器环境唯一对象(如全屏加载器)
  • 共享资源访问(IndexedDB连接池)

❌ 避免滥用:

  • 普通工具类(应使用纯函数)
  • 短期使用的上下文对象(如表单数据)
  • 需要多实例的场景(如弹窗工厂)

2. 性能优化技巧

class OptimizedSingleton {static #instance; // 私有字段static #initialized = false;constructor() {if (!OptimizedSingleton.#initialized) {throw new Error('Use getInstance() method');}// 初始化逻辑...}static getInstance() {if (!this.#instance) {this.#initialized = true;this.#instance = new OptimizedSingleton();this.#initialized = false;}return this.#instance;}
}

3. 测试友好方案

// 可重置的单例模式
class TestableService {static instance;static reset() {this.instance = null;}constructor() {if (TestableService.instance) {return TestableService.instance;}// 初始化逻辑...TestableService.instance = this;}
}// 测试用例示例
describe('Service Test', () => {afterEach(() => {TestableService.reset();});test('instance equality', () => {const a = new TestableService();const b = new TestableService();expect(a).toBe(b);});
});

五、常见陷阱与解决方案

  1. ​模块热替换问题​
// 热模块替换兼容方案
if (module.hot) {module.hot.dispose(() => {Singleton.cleanup();});module.hot.accept();
}
  1. ​多窗口场景处理​
// 使用BroadcastChannel实现跨窗口单例
class CrossTabSingleton {static instance;static EVENT_KEY = 'singleton-update';constructor() {this.channel = new BroadcastChannel(CrossTabSingleton.EVENT_KEY);this.channel.onmessage = (event) => {if (event.data === 'instance-created') {// 处理其他页面实例化的情况}};}static getInstance() {if (!this.instance) {this.instance = new CrossTabSingleton();this.instance.channel.postMessage('instance-created');}return this.instance;}
}
  1. ​内存泄漏预防​
class LeakSafeSingleton {static #weakRef;static getInstance() {let instance = this.#weakRef?.deref();if (!instance) {instance = new LeakSafeSingleton();this.#weakRef = new WeakRef(instance);// 注册清理回调this.#registerFinalizer(instance);}return instance;}static #registerFinalizer(instance) {const registry = new FinalizationRegistry(() => {// 实例被GC回收后的处理console.log('Instance cleaned up');});registry.register(instance, 'instance');}
}

六、架构层面的思考

  1. ​依赖注入整合​
interface IService {operation(): void;
}class RealService implements IService {operation() {console.log('Real operation');}
}class SingletonService {private static instance: IService;static provide(impl?: new () => IService) {if (!this.instance) {this.instance = impl ? new impl() : new RealService();}return this.instance;}
}// 在应用入口
const service = SingletonService.provide();// 测试时可注入mock实现
class MockService implements IService {operation() {console.log('Mock operation');}
}
SingletonService.provide(MockService);
  1. ​微前端架构下的单例管理​
class FederatedSingleton {static instances = new Map();static register(name, instance) {if (!this.instances.has(name)) {this.instances.set(name, instance);}}static get(name) {if (!this.instances.has(name)) {throw new Error(`Instance ${name} not registered`);}return this.instances.get(name);}
}// 主应用注册
FederatedSingleton.register('authService', new AuthService());// 子应用使用
const authService = FederatedSingleton.get('authService');

建议

  1. ​模式选择策略​​:

    • 简单场景:使用模块导出方案
    • 复杂生命周期:类静态属性实现
    • 测试需求:支持重置的变体
  2. ​性能考量​​:

    • 高频访问场景使用直接对象访问
    • 大数据量场景使用惰性加载
  3. ​架构演进​​:

    • 预留扩展点(如二次初始化方法)
    • 考虑可能的集群化扩展需求

正确应用单例模式能够有效管理系统中的特殊资源,但需要警惕其成为全局状态污染的源头。

在现代化前端架构中,建议结合DI容器或状态管理库使用,保持核心业务逻辑的纯净性。


文章转载自:

http://8TsNVirB.zLgbx.cn
http://OaF4i3EY.zLgbx.cn
http://SX85dwdL.zLgbx.cn
http://QPrgja2N.zLgbx.cn
http://uZvR0nbI.zLgbx.cn
http://XS8d38l3.zLgbx.cn
http://YetxbOqp.zLgbx.cn
http://eMcuA5rx.zLgbx.cn
http://JqDyuOmm.zLgbx.cn
http://nIFWP1h7.zLgbx.cn
http://xzg36UEg.zLgbx.cn
http://KBOkgrLz.zLgbx.cn
http://rJwodCa7.zLgbx.cn
http://IqV6VL03.zLgbx.cn
http://wUokvkMT.zLgbx.cn
http://BqckvBq1.zLgbx.cn
http://ZN71Jsos.zLgbx.cn
http://kCnfckgg.zLgbx.cn
http://Vt0DpSZU.zLgbx.cn
http://qCH3NECB.zLgbx.cn
http://YZCUyKCA.zLgbx.cn
http://ZjnoKHBb.zLgbx.cn
http://1xiZWSnk.zLgbx.cn
http://WRtIVoeB.zLgbx.cn
http://KGFuI6ZD.zLgbx.cn
http://Oi9ehH5A.zLgbx.cn
http://lnjSU0St.zLgbx.cn
http://AzYlEhEo.zLgbx.cn
http://7H4Fnood.zLgbx.cn
http://jZLRStFD.zLgbx.cn
http://www.dtcms.com/wzjs/641436.html

相关文章:

  • 网站页面维护菏泽建设集团
  • 网站建设平台设备网站的后台
  • 制作一个网站的一般步骤建材网站建设方案
  • 广东狮山网站建设网站开发文档需求撰写word
  • 视频网站开发架构贵阳小程序开发
  • 家居企业网站建设效果怎么自己做游戏软件的app
  • 打广告网站天眼查官网入口网页版
  • 烟台建设科技网站谷歌浏览器网址
  • 做购物网站步骤视频剪辑培训班的学费是多少
  • 沈阳做网站的设计公司个人如何制作网站
  • 企业3合1网站建设价格谷歌商店paypal下载官网
  • 数据库作业代做网站长葛住房和城乡建设局网站
  • 北京微信网站物流网站怎么做代理
  • ps做网站字号大小辽宁鞍山最新通知
  • 旅游网站开题报告wordpress自带ajax失效
  • 做课题的网站有多少是备案的注册一个logo需要多少钱
  • 自己怎么做单页网站热门搜索关键词
  • 广州高端网站制作公司做国外网站 国外人能看到吗
  • 电子商务网站数据库怎么做360网站上做宣传要多少钱
  • 网站优化一般怎么做南京华夏天成建设有限公司网站
  • 房地产公司网站建设与推广方案公司网站域名如何申请
  • 做房产网站不备案可以吗深圳龙岗区租房子多少钱一个月
  • 做服装外贸的网站设计保定seo排名优化
  • 太原网站的公司专业网站建设找哪家公司
  • 美食网站模版曹县网站建设
  • wordpress开启全站sslwordpress 代码解析
  • 云主机搭建asp网站龙井建设局网站
  • 网站建设包括的内容wordpress 小说 批量
  • 蓝色的网站360seo
  • mugeda做网站注册个人网站域名top