19-Definition and use of interface

Definition and use of interface

When you can design with abstract classes and interfaces flexibly, it basically means that you understand the concept of object-oriented. This step requires a lot of code accumulation.

Basic definition of interface

The biggest advantage of abstract classes over ordinary classes is that they can control the overwriting of subclass methods. Some ordinary methods are reserved in the abstract class,
and these ordinary methods may involve some security or privacy operation issues. Then during development, if you want to hide all the technical details from the outside, you can use the interface to describe it.
The interface can be understood as a pure abstract class (the most primitive defined interface contains only abstract methods and global constants), but JDK1.8 began to introduce the concept of Lambda expressions, and the definition of interfaces has also been strengthened, in addition to In addition to abstract methods and global constants, you can also define ordinary methods or static methods.
From the design perspective, the composition of the interface should still be based on abstract methods and global constants.
In Java, the interface keyword is mainly used for definition.

interface IMessage{
	public static final String info = 'test';  //全局常量
	public abstract String getInfo();	//抽象方法
}

Since the class name is the same as the interface definition requirement, the capital letter I (Interface) is often added before the interface name in order to distinguish the interface name;
problem: the interface at this time cannot directly generate instantiated objects, so the principles for using the interface are as follows:

  • Interfaces need to be implemented by subclasses (implements), one subclass can implement multiple parent interfaces;
  • Subclass (if it is not an abstract class) then must override all abstract methods in the interface;
  • Interface objects can be instantiated using the upward transformation of subclass objects;
interface IMessage{
	public static final String info = 'test';  //全局常量
	public abstract String getInfo();	//抽象方法
}
class MessageImpl implements Imessage{		//实现接口
	public String getInfo(){
		return "test";
	}
}

The above is the use of interfaces, but the main purpose of using interfaces in Java is that one subclass can implement multiple interfaces, and the concept of multiple inheritance can be realized by using interfaces;

interface IMessage{
	public static final String info = 'test';  //全局常量
	public abstract String getInfo();	//抽象方法
}
interface IChannel{
	public abstract boolean connect();
}
class MessageImpl implements Imessage,IChannel{		//继承多个接口
	public String getInfo(){
		return "test";
	}
	public boolean connect(){
		return true;
	}
}

But at this time, we must consider the actual situation, regarding the transformation of the object, at this time the subclass object can realize the transformation of any parent interface.
In Java, the interface is not allowed to inherit from the parent class, so the interface will never be a subclass of Object, so the interface can use Object to receive.
Object class objects can receive all data types, including basic data types, class objects, interface objects, and arrays.
The interface describes a public definition standard, so the access rights of all abstract methods in the interface are public, which means that it is the same whether to write or not (public must be used when overwriting);
although the interface can be successfully defined, in actual development The interface may be implemented in an abstract class, an abstract class can implement multiple interfaces, a common class can only inherit one abstract class and can implement multiple parent interfaces, but it is required to inherit first and then implement;

The abstract in the interface can be omitted, and it is not allowed to be omitted in the abstract class. Although an interface cannot inherit a parent class, an interface can inherit several parent interfaces through extends, which is called multiple inheritance of interfaces.
In actual development, the use of interfaces often has three forms:

  • Make standard settings;
  • Indicates an operational capability;
  • Expose the remote method view: this is generally used in RPC distributed development;

Enhanced interface definition

The earliest main feature of the interface is that it is all composed of abstract methods and global constants, but if the project is not involved, serious problems may occur;
improper design of the interface: I can't guarantee that the interface design is sufficiently perfect at the beginning, in order to facilitate the convenience of subclasses , Often do not let the subclass directly inherit the interface, but add an abstract class in the middle:
but starting from JDK1.8, in order to solve the defects of interface design, developers are allowed to define common methods in the interface, and the common methods in the interface The default statement must be added. It should be noted that this operation is a rescue function. If it is not necessary, it should not be the first choice;
in addition to adding ordinary methods, you can also add static methods, and static can be called directly through the interface;

Use interface definition standards

For the interface, the most important application in development is the formulation of standards.

interface IUSB{
    
    		//定义USB标准
	public boolean check();
	public void work();
}

Factory design pattern (Factory)

For the interface, it must be subclassed, and the class can obtain the instantiated object of the interface through the upward transformation of the object, but there may be design problems in the instantiation process;
coupling problem : keyword new, take JVM design as an example, Java The key to achieving portability is: JVM uses a virtual machine to run Java programs. All programs are not related to a specific operating system, but are matched by JVM;
Therefore, good design should avoid coupling;

Proxy design pattern (Proxy)

The main function of the agent design can help users to focus all the development attention only on the processing of core business functions, such as being hungry, thinking about how to eat,

interface IEat{
    
    
	public void get();
}
class EatReal implements IEat{
    
    
	public void get(){
    
    
		System.out.println("[真实主题]得到一份食物")
	}
}
class EatProxy implements IEat{
    
    		//服务代理
	private IEat eat;		//为吃服务
	public EatProxy(IEat eat){
    
    		//一定要有一个代理项
		this.eat = eat;
	}
	public void get(){
    
    
		this.prepare();
		this.eat.get();
	}
	public void prepare(){
    
    		//准备过程
		System.out.println("[代理主题]:购买食材");
		System.out.println("[代理主题]:处理食材");
	}
}

The main design features of the agency design pattern have two subcategories. One of the subcategories is a real business operation category, and the other is an agency business operation category. Without agency business operations, real business cannot be performed.

The difference between abstract class and interface

In actual development, you can find that the definitions of abstract classes and interfaces are very similar. This is especially obvious since JDK1.8, because JDK1.8 can also define default and static methods, but there are still obvious definitions for the two. And use difference;

No the difference Abstract class interface
1 definition Abstract abstract class name {} interface interface name{}
2 composition Construction, ordinary methods, static methods, global constants, members Abstract methods, global constants, ordinary methods, static methods
3 Authority Various permission definitions can be used Only use public
4 Subclass use Subclasses can inherit a keyword through the extends keyword Subclasses can implement multiple interfaces using the implements keyword
5 Relationship between the two Abstract classes can implement several interfaces Interfaces are not allowed to inherit abstract classes, but allow multiple parent interfaces
6 use 1. The abstract class or interface must define a subclass 2. The subclass must overwrite all abstract methods in the abstract class or interface 3. The abstract class or interface object instantiation is realized through the upward transformation of the subclass

When both abstract classes and interfaces can be used, the interface is given priority, because the interface can avoid the limitation of single inheritance of subclasses; in
addition, from a normal design perspective, the overall design of the project needs to be carried out first from the interface;

Guess you like

Origin blog.csdn.net/MARVEL_3000/article/details/111400696