mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-25 14:36:06 +03:00
create Facade page and link to main Readme
This commit is contained in:
parent
12855d0ea4
commit
907cf7fa61
@ -28,7 +28,7 @@ A curated collection of idiomatic design & application patterns for Go language.
|
|||||||
| [Bridge](/structural/bridge.md) | Decouples an interface from its implementation so that the two can vary independently | ✘ |
|
| [Bridge](/structural/bridge.md) | Decouples an interface from its implementation so that the two can vary independently | ✘ |
|
||||||
| [Composite](/structural/composite.md) | Encapsulates and provides access to a number of different objects | ✘ |
|
| [Composite](/structural/composite.md) | 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.md) | Adds behavior to an object, statically or dynamically | ✔ |
|
||||||
| [Facade](/structural/facade.md) | Uses one type as an API to a number of others | ✘ |
|
| [Facade](/structural/facade.md) | Uses one type as an API to a number of others | ✔ |
|
||||||
| [Flyweight](/structural/flyweight.md) | Reuses existing instances of objects with similar/identical state to minimize resource usage | ✘ |
|
| [Flyweight](/structural/flyweight.md) | 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/proxy.md) | Provides a surrogate for an object to control it's actions | ✔ |
|
||||||
|
|
||||||
|
75
structural/facade.md
Normal file
75
structural/facade.md
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
# Facade Pattern
|
||||||
|
|
||||||
|
[Façade pattern](https://en.wikipedia.org/wiki/Facade_pattern) provides a simplified interface to a complex subsystem.
|
||||||
|
|
||||||
|
## Implementation
|
||||||
|
|
||||||
|
`LogDecorate` decorates a function with the signature `func(int) int` that
|
||||||
|
manipulates integers and adds input/output logging capabilities.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Subsystem1 represents a complex subsystem
|
||||||
|
type Subsystem1 struct{}
|
||||||
|
|
||||||
|
func (s *Subsystem1) Operation1() {
|
||||||
|
fmt.Println("Subsystem1: Operation1")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subsystem2 represents another complex subsystem
|
||||||
|
type Subsystem2 struct{}
|
||||||
|
|
||||||
|
func (s *Subsystem2) Operation2() {
|
||||||
|
fmt.Println("Subsystem2: Operation2")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subsystem3 represents yet another complex subsystem
|
||||||
|
type Subsystem3 struct{}
|
||||||
|
|
||||||
|
func (s *Subsystem3) Operation3() {
|
||||||
|
fmt.Println("Subsystem3: Operation3")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Facade provides a simplified interface to the complex subsystems
|
||||||
|
type Facade struct {
|
||||||
|
subsystem1 *Subsystem1
|
||||||
|
subsystem2 *Subsystem2
|
||||||
|
subsystem3 *Subsystem3
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFacade() *Facade {
|
||||||
|
return &Facade{
|
||||||
|
subsystem1: &Subsystem1{},
|
||||||
|
subsystem2: &Subsystem2{},
|
||||||
|
subsystem3: &Subsystem3{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Facade) OperationA() {
|
||||||
|
fmt.Println("Facade: OperationA")
|
||||||
|
f.subsystem1.Operation1()
|
||||||
|
f.subsystem2.Operation2()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Facade) OperationB() {
|
||||||
|
fmt.Println("Facade: OperationB")
|
||||||
|
f.subsystem2.Operation2()
|
||||||
|
f.subsystem3.Operation3()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
facade := NewFacade()
|
||||||
|
facade.OperationA()
|
||||||
|
facade.OperationB()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
1. _Subsystem1_, _Subsystem2_, and _Subsystem3_ represent complex subsystems with their own operations.
|
||||||
|
1. The _Facade_ struct provides a simplified interface to these subsystems.
|
||||||
|
1. The _NewFacade_ function initializes the subsystems and returns a new instance of the Facade.
|
||||||
|
1. The _Facade_ struct has methods _OperationA_ and _OperationB_ that internally call the operations of the subsystems, providing a simplified interface to the client
|
||||||
|
|
||||||
|
This way, the client code can interact with the Facade instead of dealing with the complexities of the subsystems directly
|
Loading…
Reference in New Issue
Block a user