Code Monkey home page Code Monkey logo

go-design-pattern's Introduction

go-design-pattern

更多系列文章可以扫描下方二维码关注博主

微信公众号

总结

原本预计是在十月底更新完毕这个系列,到今天是11-05,晚了几天,不过也还好,这是第一次这么密集的去更新博客上的内容,更多的是以笔记的形式来呈现,加上这篇一共24篇文章差不多两个半月的时间,平均每周输出两篇,感觉还是不错。后续可能会视情况不定期的更新一些实战内容,也有可能没有。接下来下一个系列应该是数据结构与算法,包含对 Go 中一些底层数据和标准库包的学习,例如 slice, sort 等等。

话说回来,回头再看学习设计模式我们究竟需要学习一些什么?

  • 写 Go 需要使用到设计模式么?
    • 需要,但是切记请勿使用其他语言的方式来写 Go
    • 如果看过之前的一些文章,就会发现类似 JAVA 的这些面向对象语言中的某些设计模式的写法在 Go 中会十分的别扭
    • 但是 Go 不需要设计模式么?不是的,设计模式的**是想通的,并且我们一直都在使用,例如我们常见的对象创建方式 NewXXX 这其实就是一个简单工厂
  • 设计模式学习的重点是什么?
    • 设计原则,以及设计模式的使用场景和优缺点,实现相对来说还没有那么重要
    • 如果是常见的设计模式是武术招式,那么设计原则就是内功心法,没有内功心法那么招式套路也就是花架子
    • 熟练掌握不同设计模式的使用场景可以帮助我们学会见招拆招,灵活应用而不是只会套路
  • 最后设计模式不是银弹,不要拿着🔨就觉得哪里都像是钉子,不要过早优化,持续重构才是正道

设计原则

同时这也是 Code Review 的重要标准之一

点击展开设计原则

设计原则

设计模式

点击展开设计模式

设计模式

Go设计模式

  • 单例模式包含饿汉式和懒汉式两种实现
  • 工厂模式包含简单工厂、工厂方法、抽象工厂、DI容器
  • 代理模式包含静态代理、动态代理(采用 go generate 模拟)
  • 观察者模式包含观察者模式、eventbus
类型 设计模式(Github) 常用 博客
创建型 单例模式(Singleton Design Pattern) Go设计模式01-单例模式
工厂模式(Factory Design Pattern) Go设计模式02-工厂模式&DI容器
建造者模式(Builder Design Pattern) Go设计模式03-建造者模式
原型模式(Prototype Design Pattern) Go设计模式04-原型模式
结构型 代理模式(Proxy Design Pattern) Go设计模式06-代理模式(generate实现类似动态代理)
桥接模式(Bridge Design Pattern) Go设计模式07-桥接模式
装饰器模式(Decorator Design Pattern) Go设计模式08-装饰器模式
适配器模式(Adapter Design Pattern) Go设计模式09-适配器模式
门面模式(Facade Design Pattern) Go设计模式10-门面模式
组合模式(Composite Design Pattern) Go设计模式11-组合模式
享元模式(Flyweight Design Pattern) Go设计模式12-享元模式
行为型 观察者模式(Observer Design Pattern) Go设计模式13-观察者模式(实现简单的EventBus)
模板模式(Template Method Design Pattern) Go模板模式14-模板模式
策略模式(Strategy Method Design Pattern) Go设计模式15-策略模式
职责链模式(Chain Of Responsibility Design Pattern) Go设计模式16-职责链模式(Gin的中间件实现)
状态模式(State Design Pattern) Go设计模式17-状态模式
迭代器模式(Iterator Design Pattern) Go设计模式18-迭代器模式
访问者模式(Visitor Design Pattern) Go设计模式19-访问者模式
备忘录模式(Memento Design Pattern) Go设计模式20-备忘录模式
命令模式(Command Design Pattern) Go设计模式21-命令模式
解释器模式(Interpreter Design Pattern) Go设计模式22-解释器模式
中介模式(Mediator Design Pattern) Go设计模式23-中介模式

go-design-pattern's People

Contributors

clamyang avatar mohuishou avatar shanghai-jerry avatar wandh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-design-pattern's Issues

Web Server Design Pattern

which design pattern is suitable for web server ? if the web server directory is :

  • route
  • db
  • controller
  • model
  • entity

01 单例模式的问题

我测试的时候, 发现示例单例模式有问题

singleton.go

type Singleton struct {
}

var singleton *Singleton
var singleton2 *Singleton
var tag bool

func init()  {
	singleton = &Singleton{}
	singleton2 = &Singleton{}
}

func GetInstance() *Singleton{
	tag = !tag
	if tag{
		return singleton
	}
	return singleton2
}

singleton_test.go

import (
	. "github.com/smartystreets/goconvey/convey"
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestGetInstance(t *testing.T) {
	Convey("grpc server test", t, func() {
		a := GetInstance()
		b := GetInstance()
		assert.Equal(t, a, b)
	})
}

预期应该是不可以通过, 因为返回的是两个实例;
但是测试发现, 这样是能通过的,

错误的原因在于, singleton结构体是一个空结构体, 对于空结构体, go有特殊实现, 每次返回的地址都是全局变量 zerobase 的地址;

参考文档:https://segmentfault.com/a/1190000039315999

A problem about dynamic_proxy.

There is a problem that the dynamic_proxy, when I run this function “generate("./static_proxy.go”)”, I got a template string, but how should I run it, which is template string? Is this template a proxy? It really seemed like a go code, but I now just got a string template and I don’t know how to run this template string. right?
So how to change this template string to a real go code? I mean running it, so I can see the information in the console.

singleton_lazy

singleton_lazy.go 内 12 -14行代码 好像可以移除

单例本身没有问题,但是你代码中 singleton 的申明是需要加上字段的;另外 assert.Equal 直接调用也是有问题的,不会判断地址值而是判断的对象值。参考:[https://github.com/stretchr/testify/issues/1076](https://github.com/stretchr/testify/issues/1076)

          单例本身没有问题,但是你代码中 singleton 的申明是需要加上字段的;另外 assert.Equal 直接调用也是有问题的,不会判断地址值而是判断的对象值。参考:[https://github.com/stretchr/testify/issues/1076](https://github.com/stretchr/testify/issues/1076)

所以你的 singleton_test 并没有达到实际的测试效果,只是“负负得正”而已。一个正确的单测:

func TestGetInstance(t *testing.T) {
	s1 := GetInstance()
	s2 := GetInstance()
	s3 := GetLazyInstance()
	s4 := GetLazyInstance()
	assert.Equal(t, reflect.ValueOf(s1).Pointer(), reflect.ValueOf(s2).Pointer())
	assert.Equal(t, reflect.ValueOf(s3).Pointer(), reflect.ValueOf(s4).Pointer())
	assert.NotEqual(t, reflect.ValueOf(s2).Pointer(), reflect.ValueOf(s3).Pointer())
}

Originally posted by @xyling1024 in #2 (comment)

建造者模式

我怎么觉得建造者模式,不应该返回error,应该返回builder对象。在build的时候在返回error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.