Interface-oriented programming--three kinds of dependencies for objects

Source: Internet
Author: User

Interface-oriented programming--three kinds of dependencies for objects

Dependencies between modules occur through abstraction, and there should be no direct dependency between the implementing classes, and dependencies are generated through interfaces or abstract classes, which are interface-oriented programming. Interface-oriented programming, when the business logic changes, only need to modify the business scenario of the client call class, do not modify the lower layer of the module, reduce maintenance costs, reduce risk generation.

Interface Dependency Injection:

Driver interface and implementation class

Public interface Idriver {

public void driver (ICar car);

}

public class Driver implements idriver{

@Override

public void driver (ICar car) {

TODO auto-generated Method Stub

Car.running ();

}

}

Automotive interface and Implementation class

Public interface ICar {

public void running ();

}

public class BMW implements ICar {

@Override

public void running () {

TODO auto-generated Method Stub

SYSTEM.OUT.PRINTLN ("BMW drive..");

}

}

public class Benz implements ICar {

@Override

public void running () {

TODO auto-generated Method Stub

System.out.println ("Mercedes-Benz Drive.");

}

}

Scenario Client Invocation

public class Benz implements icar{

@Override

public void running () {

TODO auto-generated Method Stub

System.out.println ("Mercedes-Benz Drive");

}

}

Constructor Dependency Injection:

Driver interface and implementation class

Public interface Idriver {

public void Drive ();

}

public class Driver implements idriver{

Private ICar car;

Public Driver (ICar _car) {

This.car = _car;

}

@Override

public void Drive () {

TODO auto-generated Method Stub

Car.running ();

}

}

Automotive interface and Implementation class

Public interface ICar {

public void running ();

}

public class BMW implements ICar {

@Override

public void running () {

TODO auto-generated Method Stub

SYSTEM.OUT.PRINTLN ("BMW drive..");

}

}

public class Benz implements ICar {

@Override

public void running () {

TODO auto-generated Method Stub

System.out.println ("Mercedes-Benz Drive.");

}

}

Scenario Client Invocation

public class Client {

public static void Main (string[] args) {

Idriver Driver = new Driver (new Benz ());

Driver.drive ();

}

}

Setter Dependency Injection:

Driver interface and implementation class

Public interface Idriver {

public void Drive ();

public void Setcar (ICar car);

}

public class Driver implements Idriver {

Private ICar car;

@Override

public void Drive () {

TODO auto-generated Method Stub

Car.running ();

}

Public ICar Getcar () {

return car;

}

public void Setcar (ICar _car) {

This.car = _car;

}

}

Automotive interface and Implementation class

Public interface ICar {

public void running ();

}

public class BMW implements ICar {

@Override

public void running () {

TODO auto-generated Method Stub

SYSTEM.OUT.PRINTLN ("BMW drive..");

}

}

public class Benz implements ICar {

@Override

public void running () {

TODO auto-generated Method Stub

System.out.println ("Mercedes-Benz Drive.");

}

}

Scenario Client Invocation

public class Client {

public static void Main (string[] args) {

Idriver Driver = new driver ();

Driver.setcar (New Benz ());

Driver.drive ();

}

}

Interface-oriented programming--three kinds of dependencies for objects

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.