AOP_01 AOP understanding and simple implementation

Official explanation: AOP (Aspect-Oriented Programming, aspect-oriented programming), which is a technology that can dynamically add functions to the program without modifying the source code through precompilation and dynamic proxy at runtime. It is a new methodology and it is a supplement to traditional OOP programming. OOP is concerned with dividing the demand function into different and relatively independent, well-encapsulated classes, and letting them have their own behavior, relying on inheritance and polymorphism to define the relationship between each other; AOP hopes to be able to make the general demand function never The separation of related classes can enable many classes to share a behavior. Once a change occurs, there is no need to modify many classes, but only need to modify this behavior. AOP uses aspects to modularize cross-cutting concerns, and OOP uses classes to modularize state and behavior. In the OOP world, programs are organized through classes and interfaces, and it is appropriate to use them to implement the core business logic of the program. However, it is very difficult to achieve cross-cutting concerns (functional requirements that span multiple modules of the application), such as logging, permission verification, and exception interception. 

Understand: Through certain means, monitor and intercept related business [such as the implementation of monitoring methods]

AOP: Simple implementation:

 

 

   public interface IMessage 
    { 

        void SendMessage (string msg); 
    } 
    public class Message: IMessage 
    { 
        public virtual void SendMessage (string msg) 
        { 
            Console.WriteLine ($ "{nameof (Message)} _ msa: {msg}"); 
        } 
    } 


    / // <summary> 
    /// This uses a simple proxy class that wraps the methods in Message 
    /// </ summary> 
    public class ProxyMessage: Message 
    { 
        public override void SendMessage (string msg) 
        { 
            BeforExcute (msg) ; // before execution 
            base.SendMessage (msg); 
            AfterExcute (msg); // after execution 
        }

        void BeforExcute(string msg)
        {
            Console.WriteLine($"{nameof(BeforExcute)}_msa:{msg}");
        }
        void AfterExcute(string msg)
        {
            Console.WriteLine($"{nameof(AfterExcute)}_msa:{msg}");
        }

    }

  Disadvantages: When the business volume is large, it is impossible to add a proxy object for each object, which is troublesome to implement

 

Guess you like

Origin www.cnblogs.com/hnzheng/p/12723128.html