-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_method.go
64 lines (53 loc) · 2.25 KB
/
template_method.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package behavioural
import "fmt"
/*
Summary:
Template Method is used to define the skeleteon of the algorithm
as a sequence of operations.
How the operation is performed is left to the concrete implementations.
Example: SurgeryWorkflowTemplate method
Let's perform a surgery workflow for a patient using this. Surgery's
CareProvider interface defines the workflow. The template method executes
that workflow for different concrete implementations: shalby, practo surgeries
Benefit:
The skeleton of the algorithm which performs certain work
in some sequence is decoupled with actual implementations. This is very
useful way to implement workflows and also help in testing as it works on
an interface.
*/
// CareProvider defines what makes a care provider for surgery
type CareProvider interface {
// Book appointment to consult a doctor
BookConsult()
// Consult can lead to surgery or not
Consult()
// BookSurgery book the surgery
BookSurgery()
// Operate is used to perform the surgery
Operate()
// Recover is used to recover from surgery
Recover()
}
// SurgeryWorkflowTemplate is the template method that does a sequence of work to
// get the surgery done
func SurgeryWorkflowTemplate(provider CareProvider) {
provider.BookConsult()
provider.Consult()
provider.BookSurgery()
provider.Operate()
provider.Recover()
}
type shalbyHospital struct{}
func NewShalbyHospital() CareProvider { return &shalbyHospital{} }
func (s *shalbyHospital) BookConsult() { fmt.Printf("shalby book consult ") }
func (s *shalbyHospital) Consult() { fmt.Printf("shalby consult ") }
func (s *shalbyHospital) BookSurgery() { fmt.Printf("shalby book surgery ") }
func (s *shalbyHospital) Operate() { fmt.Printf("shalby operate ") }
func (s *shalbyHospital) Recover() { fmt.Printf("shalby recover") }
type practoCareSurgery struct{}
func NewPractoCareSurgery() CareProvider { return &practoCareSurgery{} }
func (s *practoCareSurgery) BookConsult() { fmt.Printf("practo book consult ") }
func (s *practoCareSurgery) Consult() { fmt.Printf("practo consult ") }
func (s *practoCareSurgery) BookSurgery() { fmt.Printf("practo book surgery ") }
func (s *practoCareSurgery) Operate() { fmt.Printf("practo operate ") }
func (s *practoCareSurgery) Recover() { fmt.Printf("practo recover") }