mirror of
https://github.com/tmrts/go-patterns.git
synced 2025-04-02 20:56:12 +03:00
Merge 50062f15c2
into f978e42036
This commit is contained in:
commit
c0f6d5ab37
1 changed files with 65 additions and 1 deletions
|
@ -4,6 +4,7 @@ Decorator structural pattern allows extending the function of an existing object
|
|||
Decorators provide a flexible method to extend functionality of objects.
|
||||
|
||||
## Implementation
|
||||
### Decorating single function
|
||||
`LogDecorate` decorates a function with the signature `func(int) int` that
|
||||
manipulates integers and adds input/output logging capabilities.
|
||||
|
||||
|
@ -23,7 +24,7 @@ func LogDecorate(fn Object) Object {
|
|||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
### Usage
|
||||
```go
|
||||
func Double(n int) int {
|
||||
return n * 2
|
||||
|
@ -36,6 +37,69 @@ f(5)
|
|||
// Execution is completed with the result 10
|
||||
```
|
||||
|
||||
### Decorating interface
|
||||
To ease decoration of interface with multiple methods, you can declare base decorator. The base decorator should simply calls all methods, then you can just override only one method in your target decorator.
|
||||
|
||||
|
||||
```go
|
||||
type PasswordService interface {
|
||||
CheckPassword(p string) bool
|
||||
ChangePassword(p string)
|
||||
// ... more methods
|
||||
}
|
||||
|
||||
type BaseDecoratorPasswordService struct {
|
||||
delegate PasswordService
|
||||
}
|
||||
|
||||
func (r BaseDecoratorPasswordService) CheckPassword(a string) bool {
|
||||
return r.delegate.CheckPassword(a)
|
||||
}
|
||||
|
||||
func (r BaseDecoratorPasswordService) ChangePassword(b string) {
|
||||
r.delegate.ChangePassword(b)
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Usage
|
||||
```go
|
||||
type Implementation struct {
|
||||
}
|
||||
|
||||
func (r Implementation) CheckPassword(p string) bool {
|
||||
// .. validating password
|
||||
return true
|
||||
}
|
||||
|
||||
func (r Implementation) ChangePassword(p string) {
|
||||
fmt.Printf("Implementation::ChangePassword(%v)\n", p)
|
||||
}
|
||||
|
||||
|
||||
func NewBigInterfaceMethodBLogger(delegate PasswordService) PasswordService {
|
||||
return &ChangePasswordLogger{BaseDecoratorPasswordService{delegate}}
|
||||
}
|
||||
|
||||
type ChangePasswordLogger struct {
|
||||
BaseDecoratorPasswordService
|
||||
}
|
||||
|
||||
func (r ChangePasswordLogger) ChangePassword(b string) {
|
||||
r.BaseDecoratorPasswordService.ChangePassword(b)
|
||||
fmt.Println("ChangePassword() was called")
|
||||
}
|
||||
|
||||
func main() {
|
||||
ci := NewBigInterfaceMethodBLogger(&Implementation{})
|
||||
ci.CheckPassword("echo123")
|
||||
ci.ChangePassword("qwerty")
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Rules of Thumb
|
||||
- Unlike Adapter pattern, the object to be decorated is obtained by **injection**.
|
||||
- Decorators should not alter the interface of an object.
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue