Java interface-oriented programming

Source: Internet
Author: User

In OOP, there is a design principle is interface-oriented programming, interface-oriented programming has many advantages, specific Baidu a large, I will talk about the specific use of some of the immature insights!!

First, interface-oriented programming can eliminate dependencies between classes, making the business dependent on interfaces only.

What good is this?

Such benefits greatly, for example, we declare a car interface, we have the following behavior, we can drive, car wash, carts, repair cars, demolition cars and so on,

Specifically implemented in code:

Package com.panther.dong.faceinterface;/** * Created by Panther on 15-8-9. */public interface Car {public    void Drivecar ();//Drive public    void Washcar ();//car wash public    void Fixcar ();//Repair car}

If I say I graduated from college, work, pay a salary, money, and then (money will have to burn AH), bought a bike!! I can drive, wash the car, sometimes the car breaks down,

If you can't eat your food, you'll have to fix your own car.

Package com.panther.dong.faceinterface;/** * Created by Panther on 15-8-9. */public class Bicycle implements Car {    @Override public    void Drivecar () {        System.out.println ("hands, assists two steps, Start, Accelerate ~ ~ ~ ");    }    @Override public    void Washcar () {        System.out.println ("Buy two wipes, make a maintenance for the bike");    @Override public    void Fixcar () {        System.out.println ("The car dropped the chain, the roadside Zhao sticks the stick, himself pressed the chain");}    

The above code realizes the basic behavior of a bicycle (to now the bicycle can not afford, but to think about later, after buying a bike will be how, a little excited ah), although it is a bicycle, but also a car person,

My colleague asked me how I came, I lifted my head, and said proudly, driving!! This is a benefit of the interface, will be implemented encapsulation, the caller does not need to know the specific implementation can be used, just know how to use it.

The internal implementation of the code can be hidden, without exposing it, reducing the coupling of the code.

Then one day work colleagues walk together, see I rode a bike, hey, Dick Silk one, I thought it was Gaofu, you have met, yes, see some of the source code implementation is a bit off, but use up you will

Whatever it is!!

Worked for 10 years to save 10w dollars, ready to change, think of money is not easy ah, or not changed, then there is a new bold idea, the bicycle into a tricycle bar, and then the bicycle unloading eight pieces, bought a wheel, a half-day, the money also spent, the bicycle also waste, then tricycle riding the old anchor, hey, Want to die of heart all have AH!! (in the original class changed to change, make logical confusion, system maintenance is getting worse, finally make the system reconstruction), at this time, no way, can not walk to work Ah, can only buy a car, save the money to buy a car, drive cool Ah,

The behavior code of a car:

Package com.panther.dong.faceinterface;/** * Created by Panther on 15-8-9. */public class Enginecar implements Car {    @Override public    void Drivecar () {        System.out.println ("Hang up, refuel, walk up") );    }    @Override public    void Washcar () {        System.out.println ("Spend money washing cars, helpless");    }    @Override public    void Fixcar () {        System.out.println ("Master, change a tyre, cheaper");}    

Finally can go to work, but the bicycle can only sell scrap iron, which day the car broke must walk to work!!


So the second benefit of interface-oriented programming is that when the customer needs change, just write the new implementation class of the business logic (buy a car), by changing the configuration file (such as the spring Framework) of the interface implementation class can complete the requirements, do not need to overwrite the existing code, reduce the impact on the system.

I now appreciate the two benefits, and then realize that the advantages of not to talk!!!! ~~~~~~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java interface-oriented programming

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.