Seven principles of JAVA design pattern -- the principle of dependency inversion

Article directory

1: Basic introduction

Dependency inversion principle refers to:

  1. High level modules should not rely on low level modules, both of which should rely on their abstractions
  2. Abstraction should not depend on details. Details should depend on abstraction
  3. The central idea of dependency inversion is interface oriented programming
  4. The principle of dependency inversion is based on the design concept that abstract things are much more stable than the variability of details. The architecture based on abstraction is more stable than that based on details. In java, abstraction refers to an interface or an abstract class, and details are concrete implementation classes
  5. The purpose of using an interface or abstract class is to formulate a specification without any specific operation. The task of presenting details is left to their implementation class to complete

Let's illustrate with the following code
DependecyInversion.class

public class DependecyInversion {
	public static void main(String[] args) {
		Person person = new Person();
		person.receive(new Email());
	}

}
class Email {
	public String getInfo() {
		return "Email information: hello,world";
	}
}
//Complete the function of Person receiving messages
class Person {
//Architecture based on details (class, Email class)
	public void receive(Email email ) {
		System.out.println(email.getInfo());
	}
}

If the objects we get are wechat, SMS and so on, we need to add new classes, and Perons also needs to add corresponding receiving methods, which is very troublesome

Solution: introduce an abstract interface IReceiver to represent the receiver, so that the Person class depends on the interface IReceiver. Because Email, WeiXin, etc. belong to the receiving scope, they can implement IReceiver interface respectively, so we can comply with the dependency inversion principle

After improvement
DependecyInversionImprove.class

public class DependecyInversion {
	public static void main(String[] args) {
		//Client does not need to change
		new Person().receive(new email());
		new Person().receive(new weiixng());
	}
}

//Defining interfaces
interface IReceiver {
	public String getInfo();
}

class Email implements IReceiver {
	public String getInfo() {
		return "Email information: hello,world";
	}
}

//Add WeChat
class WeiXin implements IReceiver {
	public String getInfo() {
		return "WeChat info: hello,ok";
	}
}

//Mode 2
class Person {
	//The architecture based on abstraction (Interface) is more stable than that based on details (class)
	public void receive(IReceiver receiver ) {
		System.out.println(receiver.getInfo());
	}

1: The architecture based on abstraction is more stable than that based on details. In java, abstraction refers to an interface or an abstract class, and details are concrete implementation classes
2: The purpose of using an interface or abstract class is to formulate a specification without any specific operation. The task of presenting details is left to their implementation class to complete

Three ways of dependency transfer and application cases
1) Interface delivery
DependencyPass .class

public class DependencyPass {
	public static void main(String[] args) {
		ChangHong changHong = new ChangHong();
		OpenAndClose openAndClose = new OpenAndClose();
		openAndClose.open(changHong);
	}
}

// Mode 1: implement dependency through interface delivery
// Switch interface
 interface IOpenAndClose {
 public void open(ITV tv); //Abstract method, receive interface
 }

 interface ITV { //ITV interface
 public void play();
 }
 
 class ChangHong implements ITV {

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("Changhong TV, turn it on");
	}
	 
 }
// Implementation interface
 class OpenAndClose implements IOpenAndClose{
 public void open(ITV tv){
 tv.play();
 }
 }

2) Construction method transfer
DependencyPass.class

public class DependencyPass {

	public static void main(String[] args) {
		ChangHong changHong = new ChangHong();	
		//Dependency passing through the constructor
		OpenAndClose openAndClose = new OpenAndClose(changHong);
		openAndClose.open();
	}

}
	//Mode 2: pass by construction method dependency
 interface IOpenAndClose {
 public void open(); //Abstract method
 }
 interface ITV { //ITV interface
 public void play();
 }
 class ChangHong implements ITV {

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("Changhong TV, turn it on");
	}
	 
 }
 class OpenAndClose implements IOpenAndClose{
 public ITV tv; //member
 public OpenAndClose(ITV tv){ //constructor
 this.tv = tv;
 }
 public void open(){
 this.tv.play();
 }
 }
}

3) setter delivery
DependencyPass.class

public class DependencyPass {

	public static void main(String[] args) {
		ChangHong changHong = new ChangHong();
		//Dependency passing through setter method
		OpenAndClose openAndClose = new OpenAndClose();
		openAndClose.setTv(changHong);
		openAndClose.open();
	}

}
// Mode 3, passed through setter method
interface IOpenAndClose {
	public void open(); // Abstract method

	public void setTv(ITV tv);
}
interface ITV { // ITV interface
	public void play();
}

class OpenAndClose implements IOpenAndClose {
	private ITV tv;

	public void setTv(ITV tv) {
		this.tv = tv;
	}

	public void open() {
		this.tv.play();
	}
}

class ChangHong implements ITV {

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("Changhong TV, turn it on");
	}
	 

2: Notes and details on the principle of relying on reversal

  1. Low level modules should have abstract classes or interfaces as much as possible, or both, with better program stability
  2. The declaration type of variable should be abstract class or interface as far as possible, so that there is a buffer between our variable reference and the actual object, which is conducive to program expansion and optimization
  3. Follow the principle of Richter's substitution when inheriting
139 original articles published, 57 praised, 2813 visited
Private letter follow

Tags: Java Programming

Posted on Mon, 13 Jan 2020 05:24:38 -0500 by teamfox20