Design pattern (5)-----Dependency Reversal principle

Source: Internet
Author: User

Dependency reversal Principle Definition

A. High-level modules should not rely on the underlying modules. Two should all rely on abstraction.

B. Abstraction should not depend on details. Detail should be dependent on abstraction.

In the object-oriented world, the so-called abstractions refer to excuses and abstract classes, and the deeper understanding of dependency reversal principle is "interface-oriented programming".

Example

In a car auto-detection system, the system can automatically detect the car run and stop.

 Packagecom.csdhsm.designpattem.dependence;/*** @Title: Jeepcar.java * @Description: Jeep *@author: Han * @date: June 19, 2016 PM 2:41:33*/   Public classJeepcar { Public voidrun () {System.out.println ("Jeep is Running"); }         Public voidStop () {System.out.println ("Jeep is Stop"); }}
 Packagecom.csdhsm.designpattem.dependence;/*** @Title: Autosystem.java * @Description: Auto System *@author: Han * @date: June 19, 2016 PM 2:40:09*/   Public classAutosystem {PrivateJeepcar Jeepcar;  PublicAutosystem (Jeepcar jeepcar) { This. Jeepcar =Jeepcar; }         Public voidRuncar () {jeepcar.run (); }         Public voidStopcar () {jeepcar.stop (); }}

Client

 Package com.csdhsm.designpattem.dependence;  Public class Solution {    publicstaticvoid  main (string[] args) {         New Jeepcar ();         New Autosystem (jeepcar);                System.runcar ();        System.stopcar ();    }}

Obviously, the demand is satisfied, but such a structure is very fragile and stiff, in the software world, the demand is likely to change at any time, if not to check the Jeep today, replaced by Ford car, it is embarrassing that our automatic system does not seem to be able to detect the Ford car, modified Autosystem, It's not hard to find a car, but it's a clear violation of our last principle, the open-closed principle. Think carefully, but all the models that meet the run and stop two actions should be able to be detected, so we only need to add a runableandstopable interface, the problem is very good to solve.

Runableandstopable interface

 Package com.csdhsm.designpattem.dependence; /**    * @Title:  runableandstopable.java    @author: Han    * @date:   June 19, 2016 PM 2 : 49:18    */public  interface  runableandstopable        {  Public void run ();          Public void stop ();}
 Packagecom.csdhsm.designpattem.dependence;/*** @Title: Jeepcar.java * @Description: Jeep *@author: Han * @date: June 19, 2016 PM 2:41:33*/   Public classJeepcarImplementsrunableandstopable { Public voidrun () {System.out.println ("Jeep is Running"); }         Public voidStop () {System.out.println ("Jeep is Stop"); }}
 Packagecom.csdhsm.designpattem.dependence;/*** @Title: Fordcar.java * @Description: Ford car *@author: Han * @date: June 19, 2016 PM 2:51:03*/   Public classFordcarImplementsrunableandstopable {@Override Public voidrun () {System.out.println ("Ford is Running"); } @Override Public voidStop () {System.out.println ("Ford is Stop"); }}
 Packagecom.csdhsm.designpattem.dependence;/*** @Title: Autosystem.java * @Description: Auto System *@author: Han * @date: June 19, 2016 PM 2:40:09*/   Public classAutosystem {/*** The car with runableandstopable interface is realized*/    Privaterunableandstopable Car;  PublicAutosystem (runableandstopable car) { This. Car =car; }         Public voidRuncar () {car.run (); }         Public voidStopcar () {car.stop (); }}

Client

 Package com.csdhsm.designpattem.dependence;  Public class Solution {    publicstaticvoid  main (string[] args) {        new  Fordcar ();         New Autosystem (car);                System.runcar ();        System.stopcar ();    }}

OK, success, next encounter what BMW car, horse-drawn car are not afraid of, as long as you can run and stop, can be detected by me.

Design pattern (5)-----Dependency Reversal principle

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.