IOC AOP Design Pattern

Source: Internet
Author: User

IOC AOP is not a technology but a design pattern to learn IOC AOP is actually learning an idea.

1.IOC

The IOC is actually extracting object creation and fetching to the outside. The required components are provided by the external IOC container.

Look at the following code:

     Public classGirl {//externally arranged directly into the boy class         Public voidkiss3 (Boy Boy) {Console.WriteLine ("Girl Kiss Boy");        Boy.kiss (); }       }  Public classBoy { Public voidKiss () {Console.WriteLine ("Boy Kiss Girl"); }    }

When I want to use the boy class in the Girl class, it is common practice to instantiate the boy class in girl to invoke the following code:

     Public class Girl    {        //1. Internal instantiation of the boy class         Public void Kiss1 ()        {             new boy ();            Boy.kiss ();        }    }

In IOC design mode, however, the instantiation operation is placed in the IOC container, where we use the main function as its container.

        Static void Main (string[] args)        {            new boy ();             New Girl ();            GIRL.KISS3 (boy);         }

2.AOP

AOP is actually a-proxy mode (proxy mode) that focuses on the main thing, but also allows you to focus on the business, other things let AOP help you complete.

Look at the code. I've defined a computed interface.

    /// <summary>    ///Abstract Theme Role (Subject)/// </summary>     Public InterfaceIMath {//Method        DoubleADD (DoubleXDoubley); DoubleSub (DoubleXDoubley); DoubleMul (DoubleXDoubley); DoubleDiv (DoubleXDoubley); }

    /// <summary>    ///real-World theme Role (realsubject) role/// </summary>     Public classMath:marshalbyrefobject, IMath {//Method         Public DoubleADD (DoubleXDoubleY) {returnX +y;}  Public DoubleSub (DoubleXDoubleY) {returnX-y;}  Public DoubleMul (DoubleXDoubleY) {returnX *y;}  Public DoubleDiv (DoubleXDoubleY) {returnX/y;} }

I do it by proxy, but at this point my Math class just does the addition add method, which gives the rest to its people.

    /// <summary>    ///Remote Proxy object Remotes "proxy object"///Agent subject (proxy) Role/// </summary>     Public classmathproxy {IMath math; //constructor Function         PublicMathproxy () {if(Math = =NULL) Math=NewMath (); }        //Method         Public DoubleADD (DoubleXDoubley) {returnMath.        ADD (x, y); }         Public DoubleSub (DoubleXDoubley) {return0; }         Public DoubleMul (DoubleXDoubley) {return0; }         Public DoubleDiv (DoubleXDoubley) {return0; }     }

        Static voidMain (string[] args) {            //Create a proxy and request a serviceProxy p =NewProxy ();            P.request (); //Create a functionMathproxy MP =NewMathproxy (); //Execute functionConsole.WriteLine ("4 + 2 = {0}"Mp. ADD (4,2)); Console.WriteLine ("4-2 = {0}"Mp. Sub (4,2)); Console.WriteLine ("4 * 2 = {0}"Mp. Mul (4,2)); Console.WriteLine ("4/2 = {0}"Mp. Div (4,2));        Console.ReadLine (); }

This process is called AOP. I only focus on one addition operation and the rest is given to others to do.

3.IOC AOP Federated Use

Just change the agent. Injection of dependent objects through an external IOC container

    /// <summary>    ///Remote Proxy object Remotes "proxy object"///Agent subject (proxy) Role/// </summary>     Public classmathproxy {IMath math; //constructor Function         Publicmathproxy (IMath _math) {Math= _math;        }        //Method         Public DoubleADD (DoubleXDoubley) {returnMath.        ADD (x, y); }         Public DoubleSub (DoubleXDoubley) {returnMath.        Sub (x, y); }         Public DoubleMul (DoubleXDoubley) {returnMath.        Mul (x, y); }         Public DoubleDiv (DoubleXDoubley) {returnMath.        Div (x, y); }     }

IOC AOP Design Pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.