The Java language provides an interface mechanism. This interface mechanism makes Java's object-oriented programming more flexible. We can use interfaces to define a class of performance, but the interface cannot contain any implementation. In the "Thinking In Java" book, the author has such a description of the interface: "Interface is more step by step than the abstract" concept. You can regard an interface as a pure abstract class. " I think the author is more accurate to this interpretation of the interface. Understand and use a good interface mechanism will help us better master the object-oriented programming language of Java. Let's discuss the use rules of the interface and related applications. First, the definition of the interface and the implementation of the interface and the definition class are similar, just to change the Class keyword to Interface. The method name, return type and parameter list are required, and there is no method body. Fields can be defined in the interface, which are all finaled as static and final, so it should be set to the value of these fields as needed. E.g:
Public interface flyable {void fly ();} public interface talkable {void Talk ();} public interface message {int max_size = 4096; string getMessage ();
In several interfaces defined above, Flyable and Talkable only define a method, and Message has a field Max_Size in addition to the method. It can be seen that these interfaces define only the form of performance, without any implementation, so it cannot be used directly. To use these interfaces, you need to have a corresponding class to implement them. When implementing an interface, you should use the imports keyword after the class name, and if you want to implement multiple interfaces, you should use a comma to separate them, and then implement the methods defined in these interfaces one by one. Such as follows:
Public Class Parrot Implements Flyable, Talkable {Public Void Fly () {System.out.Println ("Flying Like A Parrot ...");} public void talk () {system.out.println ("Hello! i am a parrot! ");}} public class TextMessage implements Message {String message; public void setMessage (String msg) {message = msg; if (message.length ()> MAX_SIZE) message = message.substring (0, MAX_SIZE);} public String GetMessage () {returnome;}}
In the Parrot (Parrot) example, we use interface Flyable to represent flight capabilities, Talkable represents speaking skills, but they do not include specific implementation. Parrot also has these two capabilities, so we have achieved Flyable and Talkable two interfaces for the Parrot class. Also we can also define a Swallow class, but the swallows are only flying, so we only need to implement flyable for Swallow. Since their respective flight methods are different, they have their own specific implementation of flight.
In addition, it is precisely because a class can implement multiple interfaces simultaneously, making Java's object-oriented features very flexible. Using this feature, we can implement multi-inheritance in the C language, even more flexible features. Let's discuss the application of the interface in the actual application. Second, use the interface to define some global variables
Because the fields in the interface are Static and Final, we can easily use this to create some constants. E.g:
Public interface constants {string root = "/ root"; int max_count = 200; int min_count = 100;}
The constant can be referenced directly in the form of constants.root during use. We can also use the following way to create constants of uncertain initial values.
Public interface randomcolor {int red = math.random () * 255; int Green = math.random () * 255; int blue = math.random () * 255;} where Red, Green and Blue are in the first Established when you are accessed and remain unchanged.
Third, use the interface to define the basic data structure
In the initial stage of designing a software system, we can use the interface to make some descriptions of some basic data elements, and then perform different implementations as needed. Please take a look at the following example:
Public interface user {int getage (); string getName (); string getPassword ();} public class xmluser imports user {// This () uses XML technology to implement the method in the user interface () PUBLIC INTETAGE () {...} public string getName () {...} public String getPassword () {...}} public abstract class UserFactory {public static UserFactory getUserFactory () {return new XMLUserFactory ();} public User getUser (String name); public User getAdmin ( ); public User createUser (String name, String password, int age); public void addUser (User user); public void delUser (User user);} public class XMLUserFactory extends UserFactory {// UserFactory here implemented with XML abstract art method}
In this example, we define an interface user and an abstract class UserFactory. Then we implement these two classes in XML technology. It can be seen that we only need to get an instance of a UserFactory from getUserFactory () with userFactory without having to consider the specific implementation method of this instance. With this instance of UserFactory, we can also get the User instance, nor do you need to take specific implementation methods.
If we decide to implement USER and UserFactory with JDBC technology, we only need to implement JDBCUSER and JDBCUserFactory in the above form. Then modify the GetUserFactory method in the userfactory to change their implementation methods. And we have written USERFACTORY and USER's part of the user do not need to make any modifications.
This is a simple example of using an interface to define data structures. There is still a lot of flexible use in practical applications. You need to constantly understand during the learning process.
4. Understanding the principles of distributed applications currently have many software projects use distributed technologies. Java has a variety of technologies that support distributed applications, and there are more technologies such as RMI, CORBA, and now EJB technology is more popular. However, these technologies have been developing, in fact, they are based on interfaces.
Call the RMI (Remote Method Invocation) as an example. When writing RMI applications, we need to do two most basic things, first define an interface, this interface should inherit the java.rmi.Remote interface, this interface should contain the method name you want from the remote call. Next is written a class to implement the method in this interface. E.g:
public interface Product extends java.rmi.Remote {String getName () throws java.rmi.RemoteException;} public class ProductImpl implements Product {String name; public ProductImpl (String n) {name = n;} public String getName () throws java .rmi.RemoteException {return name;}}
In this example, the interface product is placed on the client, and the ProductIMPL is placed on the server side. If the customer only needs to get the sample with the specified rule, it is not necessary to consider how the method in the Product interface is implemented. . After defining these two classes, use the Java development package command "RMIC ProductImpl" to help us automatically generate two kinds of ProductImpl_skel and ProductImpl_stub. These two classes contain the operation mechanism of RMI calls. Interested friends can study these two classes after compiling. You will find that the productImpl_stub is actually an implementation class of the interface product. The RMI mechanism is to use this class to generate an example of Product for clients. Another class productImpl_skel is a class that responds to the call request for ProductImpl_stub. The principle of RMI's optimal communication is to use ObjectInputStream and ObjetputStream to pass the Socket to the method name and parameter list to be called to the server side. The server side will call the implementation class (in this example is ProductImpl) through a specific method. The result will then pass the Socket back to the client. Since the SKEL and STUB classes are generated by tools, the development time is greatly saved. Also, if we need to modify some implementation methods or errors, you only need to modify the server-side implementation class, that is, most maintenance of this distributed application can be completed on the server side.
More and more applications are now using EJB technology. EJB is a technology that is developed from RMI, which is more perfect than RMI, and can get better object-oriented properties. But it is complicated than RMI. But no matter how complicated, it also uses an interface to define a variety of different beans, and also need to write the corresponding implementation class to complete the specific function, and finally communicate via Socket. The EJB operation mechanism itself has certain complexity, so the efficiency of its application will be affected. Therefore, when choosing development techniques, it should be carefully considered according to the scale and characteristics of the application, and there is not necessarily popular technology to adapt to your application. If you master the object-oriented design principle, you can design it. Maybe you can design a more suitable distributed application structure according to the characteristics of your own application.
V. conclusion
In addition to some of the above applications, there are many places to use interfaces, such as in the event mechanism of Java, often uses them. In addition, for some developed systems, large adjustments have been unrealistic on structures, which can be completed by defining some interfaces and adding corresponding implementations to complete the expansion of the functional structure. In short, learning the interface can help us better understand and use the object-oriented design principles. Make us design better software systems. Due to the limitations of my own level, please refer to it.
-------------------
Author: home cat