mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-25 14:36:06 +03:00
2.0 KiB
2.0 KiB
Facade Pattern
Façade 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.
// 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
func main() {
facade := NewFacade()
facade.OperationA()
facade.OperationB()
}
- Subsystem1, Subsystem2, and Subsystem3 represent complex subsystems with their own operations.
- The Facade struct provides a simplified interface to these subsystems.
- The NewFacade function initializes the subsystems and returns a new instance of the Facade.
- 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