mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-24 22:16:12 +03:00
structural/decorator: adding exmample for interface
This commit is contained in:
parent
f978e42036
commit
50062f15c2
@ -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.
|
Decorators provide a flexible method to extend functionality of objects.
|
||||||
|
|
||||||
## Implementation
|
## Implementation
|
||||||
|
### Decorating single function
|
||||||
`LogDecorate` decorates a function with the signature `func(int) int` that
|
`LogDecorate` decorates a function with the signature `func(int) int` that
|
||||||
manipulates integers and adds input/output logging capabilities.
|
manipulates integers and adds input/output logging capabilities.
|
||||||
|
|
||||||
@ -23,7 +24,7 @@ func LogDecorate(fn Object) Object {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
### Usage
|
||||||
```go
|
```go
|
||||||
func Double(n int) int {
|
func Double(n int) int {
|
||||||
return n * 2
|
return n * 2
|
||||||
@ -36,6 +37,69 @@ f(5)
|
|||||||
// Execution is completed with the result 10
|
// 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
|
## Rules of Thumb
|
||||||
- Unlike Adapter pattern, the object to be decorated is obtained by **injection**.
|
- Unlike Adapter pattern, the object to be decorated is obtained by **injection**.
|
||||||
- Decorators should not alter the interface of an object.
|
- Decorators should not alter the interface of an object.
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user