Java interface-oriented programming

Source: Internet
Author: User

In OOP, there is a design principle is interface-oriented programming, interface-oriented programming has a lot of advantages, detailed Baidu a large. Let me talk a little bit about the detailed use of some of the immature insights.

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

What are the advantages of this?

This is a big advantage. Let's say we declare a car's interface. We have the following behavior on the car, we can drive. Car wash, trolley, car repair, car demolition, etc.

Details are 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'll be able to drive and 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 ("Take your hands and assist two steps. Start. Acceleration ~ ~ ~ ");    }    @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.") Roadside Zhao Root stick. themselves by the chain ");}    }

The above code realizes the basic behavior of a bicycle (to now the bicycle can not afford, just to think about later, after buying a bike will be how, a little excited AH). Even though it's a bicycle, it's a car man,

My colleague asked me how I came, I lifted my head, and said proudly, driving.

This is an advantage of the interface, will implement encapsulation, the caller does not need to know the detailed implementation can be used, just to know how to use the details

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

Then one day my colleague walked together and saw that I was riding a bike. Hey, Dick Silk one, I thought it was Gaofu. Have you met, yes, see some of the implementation of the source code is a bit off. just use it.

Whatever it is!!

Work for 10 years to save 10w dollars, ready to change, think of money not easy ah. Still do not change, then there is a new bold idea, the bicycle into a tricycle, and then the bicycle big unloading eight pieces, bought a wheel, a half-day, the money also spent, the bicycle is also waste, then tricycle riding old anchor, hey. Want to die of heart all have Ah!

! (in the original class changed to change, make logic confusion.) System maintenance is getting worse, and finally doing the system refactoring), at this time. No way, can not walk to work ah, just can buy a car, save the money to marry daughter-in-law 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 ("hanging block. Come on, go ");    }    @Override public    void Washcar () {        System.out.println ("spend money washing cars. Helpless ");    }    @Override public    void Fixcar () {        System.out.println ("Master"). Change a tyre, cheap point ");}    }

Finally be able to work, but the bicycle can only sell scrap iron, which day the car broke down to go to work!!


Therefore, the second advantage of interface-oriented programming is that when the customer needs change, only need to write the business logic of the new implementation class (Buy a car), by changing the configuration file (such as the spring Framework) of the interface implementation class can be completed requirements. There is no need to rewrite existing code to reduce the impact on the system.

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

Java interface-oriented programming

Related Article

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.