2024年设计模式:惰性初始化模式讲解以及Go实现_惰性初始模式,5天拿到华为Golang岗offer

img
img

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

需要这份系统化的资料的朋友,可以添加戳这里获取

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

)

// Config 是我们将要实现惰性初始化的配置对象
type Config struct {
// 配置属性
}

// configInstance 保存了Config的实例,初始为nil,表示未初始化
var configInstance *Config
var once sync.Once

// GetConfig 是获取Config实例的方法,实现了惰性初始化
func GetConfig() *Config {
once.Do(func() {
configInstance = &Config{
// 初始化配置
}
})
return configInstance
}

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://img-blog.csdnimg.cn/img_convert/c970d2020f200eca8cfebd185568a48e.png)
![img](https://img-blog.csdnimg.cn/img_convert/7540d2ff117a78d67c2024aeeb42a8b1.png)
![img](https://img-blog.csdnimg.cn/img_convert/8782cd00dd07f73294a4b6bdaa770308.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**

747)]
[外链图片转存中...(img-TaBosAIV-1715489781747)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值