mirror of
https://github.com/Estom/notes.git
synced 2026-02-11 22:35:37 +08:00
go知识重新整理
This commit is contained in:
88
Go/DesignPattern/visitor/visitor.go
Normal file
88
Go/DesignPattern/visitor/visitor.go
Normal file
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/12 下午4:12
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package visitor
|
||||
|
||||
import "fmt"
|
||||
|
||||
//访问者模式: 可以给一系列对象透明的添加功能,并且把相关代码封装到一个类中
|
||||
//对象只要预留访问者接口Accept,则在后期为独享添加功能的时候就不需要改动对象了
|
||||
|
||||
type Customer interface {
|
||||
Accept(Visitor)
|
||||
}
|
||||
|
||||
type Visitor interface {
|
||||
Visit(Customer)
|
||||
}
|
||||
|
||||
type CustomerCol struct {
|
||||
customers []Customer
|
||||
}
|
||||
|
||||
func (c *CustomerCol) Add(customer Customer) {
|
||||
c.customers = append(c.customers, customer)
|
||||
}
|
||||
|
||||
func (c *CustomerCol) Accept(visitor Visitor) {
|
||||
for _, customer := range c.customers {
|
||||
customer.Accept(visitor)
|
||||
}
|
||||
}
|
||||
|
||||
//New EnterpriseCustomer
|
||||
func NewEnterpriseCustomer(name string) *EnterpriseCustomer {
|
||||
return &EnterpriseCustomer{
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
type EnterpriseCustomer struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (c *EnterpriseCustomer) Accept(visitor Visitor) {
|
||||
visitor.Visit(c)
|
||||
}
|
||||
|
||||
//New IndividualCustomer
|
||||
func NewIndividualCustomer(name string) *IndividualCustomer {
|
||||
return &IndividualCustomer{
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
type IndividualCustomer struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (i *IndividualCustomer) Accept(visitor Visitor) {
|
||||
visitor.Visit(i)
|
||||
}
|
||||
|
||||
//
|
||||
type ServiceRequestVisitor struct {
|
||||
}
|
||||
|
||||
func (s *ServiceRequestVisitor) Visit(customer Customer) {
|
||||
switch c := customer.(type) {
|
||||
case *EnterpriseCustomer:
|
||||
fmt.Printf("serving enterprise customer %s\n", c.name)
|
||||
case *IndividualCustomer:
|
||||
fmt.Printf("serving Individual customer %s\n", c.name)
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
type AnalisisVisitor struct {
|
||||
}
|
||||
|
||||
func (*AnalisisVisitor) Visit(customer Customer) {
|
||||
switch c := customer.(type) {
|
||||
case *EnterpriseCustomer:
|
||||
fmt.Printf("analysis enterprise customer %s\n", c.name)
|
||||
}
|
||||
}
|
||||
31
Go/DesignPattern/visitor/visitor_test.go
Normal file
31
Go/DesignPattern/visitor/visitor_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/13 上午10:31
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package visitor
|
||||
|
||||
func ExampleRequestVisitor() {
|
||||
c := CustomerCol{}
|
||||
c.Add(NewEnterpriseCustomer("NO.1"))
|
||||
c.Add(NewEnterpriseCustomer("NO.2"))
|
||||
c.Add(NewIndividualCustomer("bob"))
|
||||
c.Accept(&ServiceRequestVisitor{})
|
||||
//output :
|
||||
// enterprise
|
||||
// enterprise
|
||||
// individual
|
||||
}
|
||||
|
||||
func ExampleAnalisis() {
|
||||
c := CustomerCol{}
|
||||
c.Add(NewEnterpriseCustomer("A"))
|
||||
c.Add(NewIndividualCustomer("B"))
|
||||
c.Add(NewEnterpriseCustomer("C"))
|
||||
c.Accept(&AnalisisVisitor{})
|
||||
//output :
|
||||
// enterprise
|
||||
// enterprise
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user