From b3ac04909da5840e1e6dd85835ed4bb886f1ff17 Mon Sep 17 00:00:00 2001 From: Edward Date: Fri, 8 May 2020 17:07:20 +0800 Subject: [PATCH] organize structural patterns --- README.md | 4 ++-- structural/{ => decorator}/decorate.go | 13 +++++++------ structural/{ => decorator}/decorator.md | 0 structural/{ => proxy}/proxy.md | 0 4 files changed, 9 insertions(+), 8 deletions(-) rename structural/{ => decorator}/decorate.go (80%) rename structural/{ => decorator}/decorator.md (100%) rename structural/{ => proxy}/proxy.md (100%) diff --git a/README.md b/README.md index bf70658..aa222d7 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ A curated collection of idiomatic design & application patterns for Go language. |:-------:|:----------- |:------:| | [Bridge](/structural/bridge/main.go) | Decouples an interface from its implementation so that the two can vary independently | ✔ | | [Composite](/structural/composite/main.go) | Encapsulates and provides access to a number of different objects | ✔ | -| [Decorator](/structural/decorator.md) | Adds behavior to an object, statically or dynamically | ✔ | +| [Decorator](/structural/decorator/decorator.md) | Adds behavior to an object, statically or dynamically | ✔ | | [Facade](/structural/facade/main.go) | Uses one type as an API to a number of others | ✔ | | [Flyweight](/structural/flyweight/main.go) | Reuses existing instances of objects with similar/identical state to minimize resource usage | ✔ | -| [Proxy](/structural/proxy.md) | Provides a surrogate for an object to control it's actions | ✔ | +| [Proxy](/structural/decorator/proxy.md) | Provides a surrogate for an object to control it's actions | ✔ | ## Behavioral Patterns diff --git a/structural/decorate.go b/structural/decorator/decorate.go similarity index 80% rename from structural/decorate.go rename to structural/decorator/decorate.go index 4dccbe6..1f9c294 100644 --- a/structural/decorate.go +++ b/structural/decorator/decorate.go @@ -1,5 +1,7 @@ package main +import "log" + type Object func(int) int func LogDecorate(fn Object) Object { @@ -10,19 +12,18 @@ func LogDecorate(fn Object) Object { log.Println("Execution is completed with the result", result) - return result + return result } } func Double(n int) int { - return n * 2 + return n * 2 } -func main(){ - f := LogDecorate(Double) - f(5) +func main() { + f := LogDecorate(Double) + f(5) } - // Starting execution with the integer 5 // Execution is completed with the result 10 diff --git a/structural/decorator.md b/structural/decorator/decorator.md similarity index 100% rename from structural/decorator.md rename to structural/decorator/decorator.md diff --git a/structural/proxy.md b/structural/proxy/proxy.md similarity index 100% rename from structural/proxy.md rename to structural/proxy/proxy.md