interface-oriented programming and implementation-oriented programming ___ programming

Source: Internet
Author: User

Interface-oriented representation subclasses are implementation interfaces or abstract classes, and implementation-oriented representations inherit common classes.

Obviously the former coupling is relatively low, because the modification code is relatively low, because the interface and abstract class itself some methods do not do things, but subclasses to achieve.

Design principles:

Identify the parts of the application that may need to change, and separate them from the ones that don't change.

implementation-oriented use cases:

Implementation-oriented Programming/** * Common class Animal/class Animal {public $name;
        Public function __construct ($name) {$this->name = $name; echo $this->name. ": \
    n "; Public function Eat () {} public Function run () {}}/** * Subclass dog/class Dog extends Anim
    Al {public Function eat () {//dog eats with his mouth echo "Eat with mouth\n";
    The public Function run () {//dog run with four legs echo "run with four legs\n"; }/** * Subclass Bear/class Bear extends Animal {public function eat () {//bears eat with their mouths and hands echo "Eat W
    ith mouth and hands\n ";
    The public Function run () {//bear ran off with two legs echo "run with two legs\n";
        }/** * Test class */class testdriver{Public Function test () {$dog = new dog (' dog ');
        $dog->eat ();
        $dog->run ();
        $bear = new Bear (' Bear ');
        $bear->eat ();
    $bear->run ();
}} $test = new Testdriver (); $tesT->test (); 



Implementation-oriented UML:


Output:


interface-oriented use cases:

Interface-oriented Programming/** * Interface: Eat * * Interface Eatbehavior {public function Eat ();}
    /** * Dogs Eat * * Class Dogeat implements eatbehavior{public function eat () {echo "Eat with mouth\n"; }/** * Bear to eat * * Class Beareat implements Eatbehavior {public function eat () {echo ' Eat with mouth
    D hands\n ";

}/** * Interface: Run/interface Runbehavior {public function run ();}
    /** * Dog Run/class Dogrun implements Runbehavior {public function run () {echo "Run with four legs\n"; }/** * Bear Run/class Bearrun implements Runbehavior {public function run () {echo run with two L
    Egs\n ";

    }/* * Dog category/class Animal {public $name;
        Public function __construct ($name) {$this->name = $name; Echo $this->name.
    ": \ n";
    Class Dog extends animal{public $runBehavior;
    Public $eatBehavior; Public function __construct ($name) {parent::__construct ($name);
        $this->runbehavior = new Dogrun ();
    $this->eatbehavior = new Dogeat ();
    Public function Eat () {$this->eatbehavior->eat ();
    Public Function Run () {$this->runbehavior->run ();
    } class Bear extends animal {public $runBehavior;

    Public $eatBehavior;
        Public function __construct ($name) {parent::__construct ($name);
        $this->runbehavior = new Bearrun ();
    $this->eatbehavior = new Beareat ();
    Public function Eat () {$this->eatbehavior->eat ();
    Public Function Run () {$this->runbehavior->run ();
        }/** * Test class */class Testdriver {public function test () {$dog = new dog (' dog ');
        $dog->eat ();
        $dog->run ();
        $bear = new Bear (' Bear ');
        $bear->eat ();
    $bear->run ();
}} $test = new Testdriver (); $test->test ();
interface-oriented programming UML:


Output:


Section:

After an interface-oriented programming refactoring, the number of classes is significantly increased, but the implementation of the animal animal and eat eat, run the easy solution coupling. If you want to achieve crawling, rolling, yelling and other animal behavior, there is no need to modify the animal class animal code, just define a new interface class.


********************************************

* Author: Ye Wentao

* Title: interface-oriented programming and implementation-oriented programming

* Time: 2012-5-13

Reference

* "Head design mode" Eric Freeman waits

Reprint please indicate source ***************

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.