mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 03:46:03 +03:00
add functional_options
This commit is contained in:
parent
512f18568a
commit
1b7e2c91c5
57
README.md
57
README.md
@ -4,47 +4,48 @@
|
||||
|
||||
[![Build Status](https://travis-ci.org/senghoo/golang-design-pattern.svg?branch=master)](https://travis-ci.org/senghoo/golang-design-pattern)
|
||||
|
||||
|
||||
## 姿势
|
||||
|
||||
以实际代码示例展示设计模式
|
||||
|
||||
+ 所谓模式就是套路,如功夫,招有定式
|
||||
+ 这里就是以实际代码示例展示设计模式,通俗易懂
|
||||
+ 除了常见的23种普适的设计模式,Go也有一些属于自己的模式
|
||||
|
||||
## 创建型模式
|
||||
|
||||
* [简单工厂模式(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)
|
||||
|
||||
## 结构型模式
|
||||
|
||||
* [外观模式(Facade)](./01_facade)
|
||||
* [适配器模式(Adapter)](./02_adapter)
|
||||
* [代理模式(Proxy)](./09_proxy)
|
||||
* [组合模式(Composite)](./13_composite)
|
||||
* [享元模式(Flyweight)](./18_flyweight)
|
||||
* [装饰模式(Decorator)](./20_decorator)
|
||||
* [桥模式(Bridge)](./22_bridge)
|
||||
+ [外观模式(Facade)](./01_facade)
|
||||
+ [适配器模式(Adapter)](./02_adapter)
|
||||
+ [代理模式(Proxy)](./09_proxy)
|
||||
+ [组合模式(Composite)](./13_composite)
|
||||
+ [享元模式(Flyweight)](./18_flyweight)
|
||||
+ [装饰模式(Decorator)](./20_decorator)
|
||||
+ [桥模式(Bridge)](./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)](./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)
|
||||
|
||||
## Design patters Articles
|
||||
|
||||
[GO模式](https://github.com/tmrts/go-patterns)
|
||||
|
||||
[本设计模式的原始代码](https://github.com/senghoo/golang-design-pattern)
|
||||
[参考代码1](https://github.com/tmrts/go-patterns)
|
||||
[参考代码2](https://github.com/senghoo/golang-design-pattern)
|
||||
|
83
idiom/functional_options_test.go
Normal file
83
idiom/functional_options_test.go
Normal file
@ -0,0 +1,83 @@
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user