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

推荐 网站空间深圳十大广告公司

推荐 网站空间,深圳十大广告公司,哪些网站可以做装修,竞价网站策划使用Golang实现开发中常用的【实例设计模式】 设计模式是解决常见问题的模板#xff0c;可以帮助我们提升思维能力#xff0c;编写更高效、可维护性更强的代码。 单例模式#xff1a; 描述#xff1a;确保一个类只有一个实例#xff0c;并提供一个全局访问点。 优点…使用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[i1:]...)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 (fmtsingletonfactoryobserverstrategydecoratorproxy )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://www.hkea.cn/news/14508706/

相关文章:

  • 网站app 开发网站建设简述
  • 商业网站建立网站后台网址在哪输入
  • 杭州哪家做外贸网站教育行业网站开发
  • 专做动漫解说的网站gate网站合约怎么做空
  • 全国十大装修公司排行榜品牌关键词优化哪家便宜
  • 外贸视频网站怎么做局域网asp网站
  • dedecms网站二次开发网站导航条
  • 永泰县建设局网站近10天的时政新闻
  • 炽乐清网站建设python浪漫星空代码
  • 内销机械做哪个网站好中国最牛的央企排名
  • 做网站的高手网站开发的可行性
  • 如何推进网站建设北京如何优化搜索引擎
  • 如何免费建立可以交流的网站手机网站设计咨询
  • 临沂的各类网站建设做游戏代练去那个网站
  • vc 做网站源码设计师做兼职的网站
  • 上海建设网站哪家好app推广服务部
  • 网站推广应该注意什么网站内容多 询盘
  • 南城网站建设公司策划微信公众号开发创新
  • 网站内部优化工具网络推广策划书
  • 柏林网站建设重庆seo杨洋
  • 关于做网站的毕业设计企云网站建设
  • 南宁市住房建设局网站做快手电商需要什么条件
  • c2c电商网站怎么给餐饮店做网站
  • 瓯海建设网站网站建设流程图viso
  • 淘宝客网站免费建站怎么免费制作一个网站
  • 百度上找不到网站云南省建设工作网站
  • php自己做网站访问量计算软件开发文档的作用
  • 网页设计与网站建设教学视频建网站需要软件
  • 织梦网做企业网站需要授权吗如何申请免费的网站空间
  • wordpress 网站死机我的世界皮肤做壁纸的网站