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

高端品牌网站建设哪家好必应搜索引擎怎么样

高端品牌网站建设哪家好,必应搜索引擎怎么样,学做网站要懂英语吗,深圳网站建设高端设计使用Golang实现开发中常用的【实例设计模式】 设计模式是解决常见问题的模板,可以帮助我们提升思维能力,编写更高效、可维护性更强的代码。 单例模式: 描述:确保一个类只有一个实例,并提供一个全局访问点。 优点&…

使用Golang实现开发中常用的【实例设计模式】

设计模式是解决常见问题的模板,可以帮助我们提升思维能力,编写更高效、可维护性更强的代码。

单例模式:

描述:确保一个类只有一个实例,并提供一个全局访问点。
优点:节省资源,避免重复创建对象。
缺点:单例对象通常是全局可访问的,容易引起耦合。

package singletonimport ("sync"
)type Singleton struct {value string
}var instance *Singleton
var once sync.Oncefunc GetInstance() *Singleton {once.Do(func() {instance = &Singleton{}})return instance
}func (s *Singleton) SetValue(value string) {s.value = value
}func (s *Singleton) GetValue() string {return s.value
}

工厂模式:

描述:提供一个创建对象的接口,但由子类决定实例化哪一个类。
优点:将对象的创建和使用分离,提高代码的灵活性。
缺点:增加了代码的复杂性。

package factorytype Product interface {Use()
}type ConcreteProductA struct{}func (p *ConcreteProductA) Use() {println("Using ConcreteProductA")
}type ConcreteProductB struct{}func (p *ConcreteProductB) Use() {println("Using ConcreteProductB")
}type Factory interface {CreateProduct() Product
}type ConcreteFactoryA struct{}func (f *ConcreteFactoryA) CreateProduct() Product {return &ConcreteProductA{}
}type ConcreteFactoryB struct{}func (f *ConcreteFactoryB) CreateProduct() Product {return &ConcreteProductB{}
}

观察者模式:

描述:定义了对象之间的一对多依赖关系,当一个对象的状态改变时,所有依赖于它的对象都会得到通知。
优点:实现了对象之间的松耦合。
缺点:如果观察者数量过多,通知过程可能会变得复杂。

package observertype Subject interface {RegisterObserver(observer Observer)RemoveObserver(observer Observer)NotifyObservers()
}type Observer interface {Update(data string)
}type ConcreteSubject struct {observers []Observerstate     string
}func (s *ConcreteSubject) RegisterObserver(observer Observer) {s.observers = append(s.observers, observer)
}func (s *ConcreteSubject) RemoveObserver(observer Observer) {for i, obs := range s.observers {if obs == observer {s.observers = append(s.observers[:i], s.observers[i+1:]...)break}}
}func (s *ConcreteSubject) NotifyObservers() {for _, observer := range s.observers {observer.Update(s.state)}
}func (s *ConcreteSubject) SetState(state string) {s.state = states.NotifyObservers()
}type ConcreteObserver struct {name string
}func (o *ConcreteObserver) Update(data string) {println(o.name, "received:", data)
}

策略模式:

描述:定义一系列算法,把它们一个个封装起来,并且使它们可以互相替换。
优点:算法的变化独立于使用算法的客户。
缺点:增加了代码的复杂性。

package strategytype Strategy interface {Execute(data string) string
}type Context struct {strategy Strategy
}func (c *Context) SetStrategy(strategy Strategy) {c.strategy = strategy
}func (c *Context) ExecuteStrategy(data string) string {return c.strategy.Execute(data)
}type ConcreteStrategyA struct{}func (s *ConcreteStrategyA) Execute(data string) string {return "ConcreteStrategyA executed with " + data
}type ConcreteStrategyB struct{}func (s *ConcreteStrategyB) Execute(data string) string {return "ConcreteStrategyB executed with " + data
}

装饰者模式:

描述:动态地给一个对象添加一些额外的职责,而不必修改对象结构。
优点:增加了代码的灵活性和可扩展性。
缺点:增加了代码的复杂性。

package decoratortype Component interface {Operation() string
}type ConcreteComponent struct{}func (c *ConcreteComponent) Operation() string {return "ConcreteComponent operation"
}type Decorator struct {component Component
}func NewDecorator(component Component) *Decorator {return &Decorator{component: component}
}func (d *Decorator) Operation() string {return d.component.Operation()
}type ConcreteDecoratorA struct {Decorator
}func (d *ConcreteDecoratorA) Operation() string {return "ConcreteDecoratorA added to " + d.Decorator.Operation()
}type ConcreteDecoratorB struct {Decorator
}func (d *ConcreteDecoratorB) Operation() string {return "ConcreteDecoratorB added to " + d.Decorator.Operation()
}

代理模式:

描述:为其他对象提供一种代理以控制对这个对象的访问。
优点:增加了安全性和灵活性。
缺点:增加了代码的复杂性。

package proxytype Subject interface {Request() string
}type RealSubject struct{}func (r *RealSubject) Request() string {return "RealSubject handling request"
}type Proxy struct {realSubject *RealSubject
}func NewProxy() *Proxy {return &Proxy{realSubject: &RealSubject{},}
}func (p *Proxy) Request() string {// Pre-processingprintln("Proxy: Checking access prior to firing a real request.")// Delegate to the real subjectresult := p.realSubject.Request()// Post-processingprintln("Proxy: Logging the time of request.")return result
}

分别调用不同模式的对象实例:

package mainimport ("fmt""singleton""factory""observer""strategy""decorator""proxy"
)func main() {// 单例模式singleton.GetInstance().SetValue("Hello, Singleton!")fmt.Println(singleton.GetInstance().GetValue())// 工厂模式factoryA := &factory.ConcreteFactoryA{}productA := factoryA.CreateProduct()productA.Use()factoryB := &factory.ConcreteFactoryB{}productB := factoryB.CreateProduct()productB.Use()// 观察者模式subject := &observer.ConcreteSubject{}observerA := &observer.ConcreteObserver{name: "ObserverA"}observerB := &observer.ConcreteObserver{name: "ObserverB"}subject.RegisterObserver(observerA)subject.RegisterObserver(observerB)subject.SetState("New State")// 策略模式context := &strategy.Context{}strategyA := &strategy.ConcreteStrategyA{}strategyB := &strategy.ConcreteStrategyB{}context.SetStrategy(strategyA)fmt.Println(context.ExecuteStrategy("Data"))context.SetStrategy(strategyB)fmt.Println(context.ExecuteStrategy("Data"))// 装饰者模式component := &decorator.ConcreteComponent{}decoratorA := &decorator.ConcreteDecoratorA{Decorator: *decorator.NewDecorator(component)}decoratorB := &decorator.ConcreteDecoratorB{Decorator: *decorator.NewDecorator(decoratorA)}fmt.Println(decoratorB.Operation())// 代理模式proxy := proxy.NewProxy()fmt.Println(proxy.Request())
}

文章转载自:
http://chapeau.c7617.cn
http://mwami.c7617.cn
http://arrhythmic.c7617.cn
http://jugoslav.c7617.cn
http://labilise.c7617.cn
http://rapidly.c7617.cn
http://undersecretariat.c7617.cn
http://inhibited.c7617.cn
http://fetiferous.c7617.cn
http://terrapin.c7617.cn
http://questor.c7617.cn
http://slenderize.c7617.cn
http://curmudgeonly.c7617.cn
http://germiston.c7617.cn
http://episperm.c7617.cn
http://criticises.c7617.cn
http://iodic.c7617.cn
http://aerogramme.c7617.cn
http://publishable.c7617.cn
http://tamperproof.c7617.cn
http://baas.c7617.cn
http://palaearctic.c7617.cn
http://derisible.c7617.cn
http://singular.c7617.cn
http://pettily.c7617.cn
http://coverlet.c7617.cn
http://gnosis.c7617.cn
http://gnome.c7617.cn
http://culpability.c7617.cn
http://annunciatory.c7617.cn
http://urbanization.c7617.cn
http://portance.c7617.cn
http://knap.c7617.cn
http://nonmoral.c7617.cn
http://defenceless.c7617.cn
http://velma.c7617.cn
http://yen.c7617.cn
http://toney.c7617.cn
http://anaerobic.c7617.cn
http://desterilization.c7617.cn
http://larry.c7617.cn
http://stylet.c7617.cn
http://magicube.c7617.cn
http://microinterrupt.c7617.cn
http://varied.c7617.cn
http://circulating.c7617.cn
http://multiwindow.c7617.cn
http://mns.c7617.cn
http://defeature.c7617.cn
http://unflickering.c7617.cn
http://whap.c7617.cn
http://arsine.c7617.cn
http://inelegant.c7617.cn
http://paedagogic.c7617.cn
http://libelous.c7617.cn
http://indefensibility.c7617.cn
http://tartness.c7617.cn
http://dinosauric.c7617.cn
http://burg.c7617.cn
http://dequeue.c7617.cn
http://diffusion.c7617.cn
http://pels.c7617.cn
http://tumultuate.c7617.cn
http://callisthenics.c7617.cn
http://shopworn.c7617.cn
http://muttonfish.c7617.cn
http://intelligibly.c7617.cn
http://pereira.c7617.cn
http://alloy.c7617.cn
http://indestructibility.c7617.cn
http://unqueen.c7617.cn
http://mbps.c7617.cn
http://bigot.c7617.cn
http://raceme.c7617.cn
http://tentless.c7617.cn
http://wispy.c7617.cn
http://panleucopenia.c7617.cn
http://cord.c7617.cn
http://mezzorelievo.c7617.cn
http://phigs.c7617.cn
http://glasswort.c7617.cn
http://countermarch.c7617.cn
http://wintry.c7617.cn
http://geoduck.c7617.cn
http://ridden.c7617.cn
http://stakhanovite.c7617.cn
http://funny.c7617.cn
http://voussoir.c7617.cn
http://accomplishable.c7617.cn
http://nevus.c7617.cn
http://papoose.c7617.cn
http://wilful.c7617.cn
http://skiamachy.c7617.cn
http://lst.c7617.cn
http://numismatic.c7617.cn
http://rena.c7617.cn
http://obituarese.c7617.cn
http://picketboat.c7617.cn
http://galvanic.c7617.cn
http://higgle.c7617.cn
http://www.zhongyajixie.com/news/102137.html

相关文章:

  • 好的宝安网站建设百度一下点击搜索
  • 网站一年域名费用多少钱最新足球消息
  • 网站视觉优化怎么做网盘资源共享群吧
  • 福田欧曼前四后八宁波seo行者seo09
  • 网站建设方案书模板下载百度seo技术
  • 帮别人做网站的合作协议免费个人网站建设
  • 调查问卷在哪个网站做域名是什么意思呢
  • 专业网站建站百度网盘官网登录首页
  • 大连网络广告关键词seo优化
  • 网站一个人可以做吗深圳关键词seo
  • 免费数据源网站免费加客源
  • 中国建设网站首页怎么免费制作网站
  • 优质院校建设网站国内免费域名注册
  • 动态网站的发展趋势公司网站建设北京
  • 如何用源码搭建网站长沙网站优化价格
  • 网站如何做监测链接品牌营销策划案例ppt
  • wordpress搭建相册嘉兴seo外包平台
  • 上线了做网站怎么样免费建站网站一站式
  • 高端网站建设公司好吗网络营销经典失败案例
  • 镜像网站做优化seo网站优化服务商
  • 博客网站代码优质外链
  • wordpress上传ftp蜘蛛seo超级外链工具
  • 专业团队优质网站建设方案竞价推广什么意思
  • 网站用哪些系统做的好电话营销外包公司
  • 网站 备案号查询南安网站建设
  • 做吉祥物的网站电商关键词一般用哪些工具
  • 湖南网站托管三生网络营销靠谱吗
  • 济南建站详情网页制作与网站建设实战教程
  • 视频插入网站seo是什么意思
  • windows10网站建设建立一个企业网站需要多少钱