Dependency Injection (DI) and control inversion (IOC) "Review"

Source: Internet
Author: User

The idea of IOC is widely used in Java development and is also widely used in PHP.

1 Interface Coder 2 {3      Public function coding (); 4 }

Implementing Class Javaer

1 classJavaerImplementsCoder2 {3     var $name;4 5      Public function__construct ($name)6     {7         $this->name =$name;8     }9      Public functionCoding ()Ten     { One         Echo"$this->name.is Coding Java code.<br> "; A     } -}

Implementing Class Phper

1 classPhperImplementsCoder2 {3     var $name;4 5      Public function__construct ($name)6     {7         $this->name =$name;8     }9      Public functionCoding ()Ten     { One         Echo"$this->name.is Coding php code.<br> "; A     } -}

Business Logic Class Task

1 classTask2 {3     var $name;4     var $owner;5     6      Public function__construct ($name)7     {8         $this->name =$name;9     }Ten      One      Public functionSetOwner ($owner){ A         $this->owner =$owner; -     } -  the      Public functionStart () -     { -         Echo"$this->name.started!<br> "; -         $this->owner->coding (); +     } -}

Test

1         $task New Task ("Task #1"); 2 //         $owner = new Phper ("XIAOHB"); 3         $owner New Javaer ("Xiaoxiao"); 4         $task->setowner ($owner); 5         $task->start ();

The maintainability and extensibility of the code is obvious.

Dependency Injection (DI) and control inversion (IOC) "Review"

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.