本教程操作環境:windows7系統、GO 1.18版本、Dell G3電腦。
什么是aop?
【資料圖】
在軟件業,AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期間動態代理實現程序功能的統一維護的一種技術。AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生范型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
面向切面編程是面向對象中的一種方式而已。在代碼執行過程中,動態嵌入其他代碼,叫做面向切面編程。常見的使用場景:
日志
事物
數據庫操作
面向切面編程,就是將交叉業務邏輯封裝成切面,利用AOP的功能將切面織入到主業務邏輯中。所謂交叉業務邏輯是指,通用的,與主業務邏輯無關的代碼,如安全檢查,事物,日志等。若不使用AOP,則會出現代碼糾纏,即交叉業務邏輯與主業務邏輯混合在一起。這樣,會使業務邏輯變得混雜不清。
主要應用場景:日志記錄,性能統計,安全控制,事務處理,異常處理等等。
核心概念
JoinPoint:連接點。是程序執行中的一個精確執行點,例如類中的一個方法。
PointCut:切入點。指定哪些組件的哪些方法使用切面組件。
Advice:通知,用于指定具體作用的位置,是方法之前或之后等等,分為前置通知,后置通知,異常通知,返回通知,環繞通知。
Aspect: 切面。封裝通用業務邏輯的組件,即我們想要插入的代碼內容。
其內在設計模式為代理模式。
go語言支不支持aop?
go語言支持aop。
Go實現AOP的示例:
// User type User struct {Name stringPass string}// Auth 驗證func (u *User) Auth() {// 實際業務邏輯fmt.Printf("register user:%s, use pass:%s\n", u.Name, u.Pass)}// UserAdvice type UserAdvice interface { // Before 前置通知 Before(user *User) error // After 后置通知After(user *User)}// ValidatePasswordAdvice 用戶名驗證type ValidateNameAdvice struct {}// ValidatePasswordAdvice 密碼驗證type ValidatePasswordAdvice struct {MinLength intMaxLength int}func (ValidateNameAdvice) Before(user *User) error {fmt.Println("ValidateNameAdvice before")if user.Name == "admin" {return errors.New("admin can"t be used")}return nil}func (ValidateNameAdvice) After(user *User) {fmt.Println("ValidateNameAdvice after")fmt.Printf("username:%s validate sucess\n", user.Name)}// Before 前置校驗func (advice ValidatePasswordAdvice) Before(user *User) error {fmt.Println("ValidatePasswordAdvice before")if user.Pass == "123456" {return errors.New("pass isn"t strong")}if len(user.Pass) > advice.MaxLength {return fmt.Errorf("len of pass must less than:%d", advice.MaxLength)}if len(user.Pass) < advice.MinLength {return fmt.Errorf("len of pass must greater than:%d", advice.MinLength)}return nil}func (ValidatePasswordAdvice) After(user *User) {fmt.Println("ValidatePasswordAdvice after")fmt.Printf("password:%s validate sucess\n", user.Pass)}// UserAdviceGroup,通知管理組type UserAdviceGroup struct {items []UserAdvice}// Add 注入可選通知func (g *UserAdviceGroup) Add(advice UserAdvice) {g.items = append(g.items, advice)}func (g *UserAdviceGroup) Before(user *User) error {for _, item := range g.items {if err := item.Before(user); err != nil {return err}}return nil}// Afterfunc (g *UserAdviceGroup) After(user *User) {for _, item := range g.items {item.After(user)}}// UserProxy 代理,也是切面type UserProxy struct {user *User}// NewUser return UserProxyfunc NewUser(name, pass string) UserProxy {return UserProxy{user:&User{Name:name, Pass:pass}}}// Auth 校驗,切入點func (p UserProxy) Auth() {group := UserAdviceGroup{}group.Add(&ValidatePasswordAdvice{MaxLength:10, MinLength:6}) group.Add(&ValidateNameAdvice{}) // 前置通知if err := group.Before(p.user); err != nil {panic(err)} // 實際邏輯p.user.Auth() // 后置通知group.After(p.user)}
使用AOP模式進行解耦,分離主業務與副業務。其實也就那樣。
以上就是go語言不支持aop嗎的詳細內容,更多請關注php中文網其它相關文章!