本文主要是介绍通过proxy和普通模式实现单例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
通过 proxy 和普通模式实现单例
- 新建变量存储
- porxy实现
代码实现如下
import * as Cesium from 'cesium'
import { createVNode, render } from 'vue'
import Prompt from "@/components/Cesium/CesiumPrompt/index"
/** @Description: 事件管理* @LastEditTime: 2021-09-06 14:02:03*/
class EventManager {// 存储单例private static instance// 单例模式调用方式static getInstance({ viewer }) {if (!EventManager.instance) {EventManager.instance = new EventManager({ viewer })}return EventManager.instance}constructor({ viewer }) {this.viewer = viewerthis.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)this.promt = null}addEvent({ eventType, callback }) {}removeEvent({ eventType }) {}}const instanceEventManager = (ClassName) => {let insreturn new Proxy(ClassName, {construct(target, args) {if (!ins) {ins = new ClassName(args[0])}return ins}})
}
export default instanceEventManager(EventManager)
这篇关于通过proxy和普通模式实现单例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!