From 9f56436745538c0703b7a2314c77fa883ad5f7c9 Mon Sep 17 00:00:00 2001 From: Edward Date: Mon, 27 Apr 2020 10:27:37 +0800 Subject: [PATCH] adjust structure & finish option mode --- README.md | 68 ++++++++------- behavior/26_option/README.md | 3 + behavior/26_option/functional_options_test.go | 15 ++++ behavior/26_option/options.go | 65 +++++++++++++++ {behavior => creation}/06_builder/builder.go | 0 .../06_builder/builder_test.go | 0 idiom/functional_options_test.go | 83 ------------------- 7 files changed, 119 insertions(+), 115 deletions(-) create mode 100644 behavior/26_option/README.md create mode 100644 behavior/26_option/functional_options_test.go create mode 100644 behavior/26_option/options.go rename {behavior => creation}/06_builder/builder.go (100%) rename {behavior => creation}/06_builder/builder_test.go (100%) delete mode 100644 idiom/functional_options_test.go diff --git a/README.md b/README.md index 86e4ff9..5e80076 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ -# Go 语言设计模式示例集合 +# 语言设计模式示例集合(Go Patterns Examples) -参考资料:[大话设计模式](https://book.douban.com/subject/2334288/) | [图解设计模式](https://book.douban.com/subject/26933281/) | [菜鸟教程—设计模式](https://www.runoob.com/design-pattern/design-pattern-tutorial.html) - -[![Build Status](https://travis-ci.org/senghoo/golang-design-pattern.svg?branch=master)](https://travis-ci.org/senghoo/golang-design-pattern) +Go常用的、面向工程化和最佳实践的模式套路,包含常见的23种设计模式,重点是这里全部是例子、通俗易懂,每个模式,改一下名字就可以直接用在项目和工程中. ## 姿势 @@ -12,43 +10,49 @@ ## 创建型模式 -+ [简单工厂模式(Simple Factory)](./creation/00_simple_factory) -+ [工厂方法模式(Factory Method)](./creation/04_factory_method) -+ [抽象工厂模式(Abstract Factory)](./creation/05_abstract_factory) -+ [创建者模式(Builder)](./creation/06_builder) -+ [原型模式(Prototype)](./creation/07_prototype) -+ [单例模式(Singleton)](./creation/03_singleton) ++ [简单工厂模式(Simple Factory)](./creation/00_simple_factory) ++ [工厂方法模式(Factory Method)](./creation/04_factory_method) ++ [抽象工厂模式(Abstract Factory)](./creation/05_abstract_factory) ++ [创建者模式(Builder)](./creation/06_builder) ++ [原型模式(Prototype)](./creation/07_prototype) ++ [单例模式(Singleton)](./creation/03_singleton) ++ [对象池模式(OBject Pool)](./creation/24_object_pool) ++ [New模式(New)](./creation/25_new) ## 结构型模式 -+ [外观模式(Facade)](./01_facade) -+ [适配器模式(Adapter)](./02_adapter) -+ [代理模式(Proxy)](./09_proxy) -+ [组合模式(Composite)](./13_composite) -+ [享元模式(Flyweight)](./18_flyweight) -+ [装饰模式(Decorator)](./20_decorator) -+ [桥模式(Bridge)](./22_bridge) ++ [外观模式(Facade)](./structure/01_facade) ++ [适配器模式(Adapter)](./structure/02_adapter) ++ [代理模式(Proxy)](./structure/09_proxy) ++ [组合模式(Composite)](./structure/13_composite) ++ [享元模式(Flyweight)](./structure/18_flyweight) ++ [装饰模式(Decorator)](./structure/20_decorator) ++ [桥模式(Bridge)](./structure/22_bridge) ## 行为型模式 -+ [中介者模式(Mediator)](./08_mediator) -+ [观察者模式(Observer)](./10_observer) -+ [命令模式(Command)](./11_command) -+ [迭代器模式(Iterator)](./12_iterator) -+ [模板方法模式(Template Method)](./14_template_method) -+ [策略模式(Strategy)](./15_strategy) -+ [状态模式(State)](./16_state) -+ [备忘录模式(Memento)](./17_memento) -+ [解释器模式(Interpreter)](./19_interpreter) -+ [职责链模式(Chain of Responsibility)](./21_chain_of_responsibility) -+ [访问者模式(Visitor)](./23_visitor) ++ [中介者模式(Mediator)](./behavior/08_mediator) ++ [观察者模式(Observer)](./behavior/10_observer) ++ [命令模式(Command)](./behavior/11_command) ++ [迭代器模式(Iterator)](./behavior/12_iterator) ++ [模板方法模式(Template Method)](./behavior/14_template_method) ++ [策略模式(Strategy)](./behavior/15_strategy) ++ [状态模式(State)](./behavior/behavior16_state) ++ [备忘录模式(Memento)](./behavior/17_memento) ++ [解释器模式(Interpreter)](./behavior/19_interpreter) ++ [职责链模式(Chain of Responsibility)](./behavior/21_chain_of_responsibility) ++ [访问者模式(Visitor)](./behavior/23_visitor) ++ [闭包选项模式(Function Option)](./behavior/26_option) -## Design patters Articles +## 参考资(Design patters Articles) -[GO模式](https://github.com/tmrts/go-patterns) +[大话设计模式](https://book.douban.com/subject/2334288/) -[参考代码1](https://github.com/tmrts/go-patterns) -[参考代码2](https://github.com/senghoo/golang-design-pattern) +[GO模式文档](https://github.com/tmrts/go-patterns) + +[菜鸟教程—设计模式](https://www.runoob.com/design-pattern/design-pattern-tutorial.html) + +[GO设计模式](https://github.com/senghoo/golang-design-pattern) ## 更多 diff --git a/behavior/26_option/README.md b/behavior/26_option/README.md new file mode 100644 index 0000000..a8ed321 --- /dev/null +++ b/behavior/26_option/README.md @@ -0,0 +1,3 @@ +# 说明 + +Option模式又称万能用于参数传递和初始化的创建模式,一般常常与New模式配合使用,同时具有Builder模式的一些特点,创建对象更灵活. \ No newline at end of file diff --git a/behavior/26_option/functional_options_test.go b/behavior/26_option/functional_options_test.go new file mode 100644 index 0000000..6af457d --- /dev/null +++ b/behavior/26_option/functional_options_test.go @@ -0,0 +1,15 @@ +package option + +import ( + "testing" +) + +func TestFileFunctionOptions(t *testing.T) { + + Introduce("tom", Gender(true), Company("land company")) + + Introduce("lily", Company("sky commnay"), UID(123)) + + Introduce("admin", Company("risky commnay"), UID(883)) + +} diff --git a/behavior/26_option/options.go b/behavior/26_option/options.go new file mode 100644 index 0000000..41f1d8a --- /dev/null +++ b/behavior/26_option/options.go @@ -0,0 +1,65 @@ +package option + +import ( + "fmt" +) + +//Options is key struct,关键数据结构,聚合所有外部可传入的参数 +type Options struct { + UID int + GID int + Flags int + Company string + Gender bool //is male +} + +//Option func is key func +type Option func(*Options) + +//UID set User ID +func UID(userID int) Option { + return func(args *Options) { + args.UID = userID + } +} + +//GID set User Group ID +func GID(groupID int) Option { + return func(args *Options) { + args.GID = groupID + } +} + +//Company set Company Name +func Company(cname string) Option { + return func(args *Options) { + args.Company = cname + } +} + +//Gender for male or female +func Gender(gender bool) Option { + return func(args *Options) { + args.Gender = gender + } +} + +//Introduce someone +func Introduce(name string, setters ...Option /*传入闭包设置函数*/) { + // Default Options + args := &Options{ + UID: 0, + GID: 0, + Company: "", + Gender: true, + } + for _, setter := range setters { + setter(args) + } + gender := "famale" + if args.Gender { + gender = "male" + } + fmt.Println("----------------------") + fmt.Println("im am: ", name, "\nfrom: ", args.Company, "\ngender: ", gender, "\nUID: ", args.UID) +} diff --git a/behavior/06_builder/builder.go b/creation/06_builder/builder.go similarity index 100% rename from behavior/06_builder/builder.go rename to creation/06_builder/builder.go diff --git a/behavior/06_builder/builder_test.go b/creation/06_builder/builder_test.go similarity index 100% rename from behavior/06_builder/builder_test.go rename to creation/06_builder/builder_test.go diff --git a/idiom/functional_options_test.go b/idiom/functional_options_test.go deleted file mode 100644 index 5141b55..0000000 --- a/idiom/functional_options_test.go +++ /dev/null @@ -1,83 +0,0 @@ -package idiom - -import ( - "os" - "testing" -) - -func TestFileFunctionOptions(t *testing.T) { - err := New("empty.txt") - if err != nil { - panic(err) - } - os.Remove("empty.txt") - - err = New("file.txt", UID(1000), Contents("input some data")) - if err != nil { - panic(err) - } - os.Remove("file.txt") -} - -///Options is key struct -type Options struct { - UID int - GID int - Flags int - Contents string - Permissions os.FileMode -} - -//Option func is key func -type Option func(*Options) - -func UID(userID int) Option { - return func(args *Options) { - args.UID = userID - } -} - -func GID(groupID int) Option { - return func(args *Options) { - args.GID = groupID - } -} - -func Contents(c string) Option { - return func(args *Options) { - args.Contents = c - } -} - -func Permissions(perms os.FileMode) Option { - return func(args *Options) { - args.Permissions = perms - } -} - -func New(filepath string, setters ...Option) error { - // Default Options - args := &Options{ - UID: os.Getuid(), - GID: os.Getgid(), - Contents: "", - Permissions: 0666, - Flags: os.O_CREATE | os.O_EXCL | os.O_WRONLY, - } - - for _, setter := range setters { - setter(args) - } - - f, err := os.OpenFile(filepath, args.Flags, args.Permissions) - if err != nil { - return err - } - defer f.Close() - - if _, err := f.WriteString(args.Contents); err != nil { - return err - } - - return err -}