🇬🇧 English | 🇨🇳 中文
AOP (Aspect Oriented Programming) is a kind of programming design idea, is the continuation of OOP (Object Oriented Programming), is a hot spot in software development. AOP can be used to isolate each part of business logic, so as to reduce the coupling degree between the parts of business logic, improve the reusability of the program, and improve the efficiency of development.
go get github.com/MicroWiller/AOP-golang
- First, define a structure type that implement the Aspect interface.
// BusProxy proxy bus
type BusProxy struct {
bus *Bus
}
func (ap BusProxy) Pointcut(ctx context.Context) error {
return ap.bus.Drive(ctx)
}
type Bus struct {
Name string
Route string
Passengers int64
}
- Instantiate the generic architecture AOP.
// NewBus instantiate generic AOP for BusProxy.
func NewBus(name, route string, p int64) AOP[BusProxy] {
busProxyAop := AOP[BusProxy]{}
bus := Bus{
Name: name,
Route: route,
Passengers: p,
}
proxy := BusProxy{bus: &bus}
busProxyAop.SetProxy(proxy)
return busProxyAop
}
- Instantiate a pre/post pointcut.
before := RegisterBefore[BusProxy]
aftere := RegisterAfter[BusProxy]
- Pointcut structures implement generic interfaces Before[T Aspect] / After[T Aspect].
type Police struct {
}
func (p Police) Before(ctx context.Context, bp *BusProxy) error {
return nil
}
func (p Police) After(ctx context.Context, bp *BusProxy) {
}
- Generate Option load points.
p := Police{}
pBeforeOpt := before(p)
pAfterOpt := aftere(p)
- Execute AOP proxy methods.
busAop.Proxy(ctx, pBeforeOpt, pAfterOpt)
See more examples test file
,Welcome to issue
.