How to decouple java-7.3 interfaces from the ground up?

Source: Internet
Author: User

How to decouple java-7.3 interfaces from the ground up?

In this chapter, we will discuss how interfaces can be decoupled? Enhances code flexibility.

Let's take a look at the following code:

package com.ray.ch07;public class Test {public static void test(Vehicle vehicle) {vehicle.test();}public static void main(String[] args) {test(new BMW());test(new Audi());}}class Vehicle {public void test() {System.out.println(Vehicle test);}}class BMW extends Vehicle {@Overridepublic void test() {System.out.println(BMW test);}}class Audi extends Vehicle {@Overridepublic void test() {System.out.println(Audi test);}}



The above code mainly describes the vehicle test.

Let's add another set of code:

package com.ray.ch07;public class Test {public static void test(Instrument instrument) {instrument.test();}public static void main(String[] args) {test(new Wind());test(new Bass());}}class Instrument {public void test() {System.out.println(Instrument test);}}class Wind extends Instrument {@Overridepublic void test() {System.out.println(Wind test);}}class Bass extends Instrument {@Overridepublic void test() {System.out.println(Bass test);}}


The above code mainly describes the test of the instrument.

Comparing the two sets of code, you will find that the main code for the test is similar to everyone, but because both the instrument and the car do not inherit the same base class, the test code cannot be universal.

The main reason for unavailability is that Instrument and Vehicle are too tightly coupled with Test. The following interface is used to combine the two classes so that the code in Test can be universal.

package com.ray.ch07;public class Test {public static void test(TestObjectInterface testObjectInterface) {testObjectInterface.test();}public static void main(String[] args) {test(new Wind());test(new Bass());test(new BMW());test(new Audi());}}interface TestObjectInterface {void test();}class Instrument implements TestObjectInterface {@Overridepublic void test() {System.out.println(Instrument test);}}class Wind extends Instrument {@Overridepublic void test() {System.out.println(Wind test);}}class Bass extends Instrument {@Overridepublic void test() {System.out.println(Bass test);}}class Vehicle implements TestObjectInterface {@Overridepublic void test() {System.out.println(Vehicle test);}}class BMW extends Vehicle {@Overridepublic void test() {System.out.println(BMW test);}}class Audi extends Vehicle {@Overridepublic void test() {System.out.println(Audi test);}}


We create a common interface, and then implement the interfaces of the two base classes respectively, and then implement polymorphism through the upward transformation, so that the TestObjectInterface parent class points to different sub-classes, to call methods of different subclasses.

Here, we can extend it again. The above Code actually uses the policy design pattern. TestObjectInterface is always a fixed part of the execution algorithm, and then different implementations are executed through different types of input.

 

Summary: This chapter describes how to decouple interfaces.

 

 

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.