设计模式:惰性初始化模式讲解以及Go实现_惰性初始模式(1),Golang高级工程师面试实战

21 阅读4分钟

}

func main() { // 使用配置 config := GetConfig() fmt.Println(config) }


在这个例子中,`sync.Once` 保证了`Config`的实例`configInstance`只会被初始化一次,即使在多线程环境下也能正确工作。这正是Go语言在并发处理上的强项之一。


##### 类图




#mermaid-svg-eg6EqtsIkVsLfiyO {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-eg6EqtsIkVsLfiyO .error-icon{fill:#552222;}#mermaid-svg-eg6EqtsIkVsLfiyO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-eg6EqtsIkVsLfiyO .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-eg6EqtsIkVsLfiyO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-eg6EqtsIkVsLfiyO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-eg6EqtsIkVsLfiyO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-eg6EqtsIkVsLfiyO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-eg6EqtsIkVsLfiyO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-eg6EqtsIkVsLfiyO .marker.cross{stroke:#333333;}#mermaid-svg-eg6EqtsIkVsLfiyO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-eg6EqtsIkVsLfiyO g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-eg6EqtsIkVsLfiyO g.classGroup text .title{font-weight:bolder;}#mermaid-svg-eg6EqtsIkVsLfiyO .nodeLabel,#mermaid-svg-eg6EqtsIkVsLfiyO .edgeLabel{color:#131300;}#mermaid-svg-eg6EqtsIkVsLfiyO .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-eg6EqtsIkVsLfiyO .label text{fill:#131300;}#mermaid-svg-eg6EqtsIkVsLfiyO .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-eg6EqtsIkVsLfiyO .classTitle{font-weight:bolder;}#mermaid-svg-eg6EqtsIkVsLfiyO .node rect,#mermaid-svg-eg6EqtsIkVsLfiyO .node circle,#mermaid-svg-eg6EqtsIkVsLfiyO .node ellipse,#mermaid-svg-eg6EqtsIkVsLfiyO .node polygon,#mermaid-svg-eg6EqtsIkVsLfiyO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-eg6EqtsIkVsLfiyO .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-eg6EqtsIkVsLfiyO g.clickable{cursor:pointer;}#mermaid-svg-eg6EqtsIkVsLfiyO g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-eg6EqtsIkVsLfiyO g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-eg6EqtsIkVsLfiyO .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-eg6EqtsIkVsLfiyO .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-eg6EqtsIkVsLfiyO .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-eg6EqtsIkVsLfiyO .dashed-line{stroke-dasharray:3;}#mermaid-svg-eg6EqtsIkVsLfiyO #compositionStart,#mermaid-svg-eg6EqtsIkVsLfiyO .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-eg6EqtsIkVsLfiyO #compositionEnd,#mermaid-svg-eg6EqtsIkVsLfiyO .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-eg6EqtsIkVsLfiyO #dependencyStart,#mermaid-svg-eg6EqtsIkVsLfiyO .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-eg6EqtsIkVsLfiyO #dependencyStart,#mermaid-svg-eg6EqtsIkVsLfiyO .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-eg6EqtsIkVsLfiyO #extensionStart,#mermaid-svg-eg6EqtsIkVsLfiyO .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-eg6EqtsIkVsLfiyO #extensionEnd,#mermaid-svg-eg6EqtsIkVsLfiyO .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-eg6EqtsIkVsLfiyO #aggregationStart,#mermaid-svg-eg6EqtsIkVsLfiyO .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-eg6EqtsIkVsLfiyO #aggregationEnd,#mermaid-svg-eg6EqtsIkVsLfiyO .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-eg6EqtsIkVsLfiyO .edgeTerminals{font-size:11px;}#mermaid-svg-eg6EqtsIkVsLfiyO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}



















































uses


















LazyInitializedObject




-static instance: LazyInitializedObject




-LazyInitializedObject()




+static getInstance()
















Client




-LazyInitializedObject obj




+doSomething()









##### 流程图




#mermaid-svg-v67kfAMbYHqdmXtM {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-v67kfAMbYHqdmXtM .error-icon{fill:#552222;}#mermaid-svg-v67kfAMbYHqdmXtM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-v67kfAMbYHqdmXtM .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-v67kfAMbYHqdmXtM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-v67kfAMbYHqdmXtM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-v67kfAMbYHqdmXtM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-v67kfAMbYHqdmXtM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-v67kfAMbYHqdmXtM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-v67kfAMbYHqdmXtM .marker.cross{stroke:#333333;}#mermaid-svg-v67kfAMbYHqdmXtM svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-v67kfAMbYHqdmXtM .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-v67kfAMbYHqdmXtM .cluster-label text{fill:#333;}#mermaid-svg-v67kfAMbYHqdmXtM .cluster-label span{color:#333;}#mermaid-svg-v67kfAMbYHqdmXtM .label text,#mermaid-svg-v67kfAMbYHqdmXtM span{fill:#333;color:#333;}#mermaid-svg-v67kfAMbYHqdmXtM .node rect,#mermaid-svg-v67kfAMbYHqdmXtM .node circle,#mermaid-svg-v67kfAMbYHqdmXtM .node ellipse,#mermaid-svg-v67kfAMbYHqdmXtM .node polygon,#mermaid-svg-v67kfAMbYHqdmXtM .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-v67kfAMbYHqdmXtM .node .label{text-align:center;}#mermaid-svg-v67kfAMbYHqdmXtM .node.clickable{cursor:pointer;}#mermaid-svg-v67kfAMbYHqdmXtM .arrowheadPath{fill:#333333;}#mermaid-svg-v67kfAMbYHqdmXtM .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-v67kfAMbYHqdmXtM .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-v67kfAMbYHqdmXtM .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-v67kfAMbYHqdmXtM .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-v67kfAMbYHqdmXtM .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-v67kfAMbYHqdmXtM .cluster text{fill:#333;}#mermaid-svg-v67kfAMbYHqdmXtM .cluster span{color:#333;}#mermaid-svg-v67kfAMbYHqdmXtM div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-v67kfAMbYHqdmXtM :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}



















































请求对象/资源









存在









不存在
































 开始 
 










 检查对象是否存在 
 










 返回现有对象 
 



![img](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/8cd4ff7b2da3407fbbd07c78a8eaaadc~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771252630&x-signature=OZl%2BmryZgto%2B9fboohUpESo6SQc%3D)
![img](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/26172eae82504cc1a402ebfa72061de9~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771252630&x-signature=Oywn7eGL1vmKasne0DlzoYAq5f8%3D)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://gitee.com/vip204888)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**