The Java object-oriented programming interface applications



Java language provides an interface (interface) mechanisms.This interface mechanism to Java object-oriented programming has become more flexible.We can use the interface to define a class form of expression, but the interface does not contain any implementation.In the "Thinking in Java" book, the author interface has this description: "Interface (interface) than abstract (abstract) concept a step further. You can put an interface as a pure abstract class."I think that this interpretation of the interface most accurate enough.
Understand and make good use of the interface mechanisms will help us better grasp of this object-oriented Java programming language.Below we discuss the use of the interface rules and related applications.

First, the interface definition and implementation

Define the interface and class definitions similar to, but should change as the class keyword interface.Define method requires only the method name, return type and parameter list, can not have a method body.Interface can be defined fields that have been alluding to as static and final, and should therefore be based on these fields need to be good value.For example:

public interface Flyable {
void fly ();
}

public interface Talkable {
void talk ();
}

public interface Message {
int MAX_SIZE = 4096;
String getMessage ();
}

Several interface defined above, Flyable and Talkable only defines a method, and Message in addition to the method but also have a field MAX_SIZE.The definition of these interfaces can be seen that only the manifestation of the class and does not include any implementation, it can not be used directly.To use these interfaces need to have the appropriate class to implement them.Implement an interface class name should be first in the state after the keyword with the implements the interface to be achieved, if you want to implement multiple interfaces, should be separated by a comma, and then one by one to achieve the methods defined in these interfaces.Such as the following example:

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 () {
return message;
}
}

In the Parrot (Parrot) case, we use the interface Flyable ability to represent the flight, Talkable that speech, yet they do not include specific implementation.The Parrot also has both capabilities, so we Parrot class implements both Flyable and Talkable the two interfaces.Similarly, we can also define a Swallow (Swallow) class, but the ability to swallow only a flight, so we only need to achieve Flyable Swallow on the line.Because their respective flight are different, so they have their own concrete implementation on the flight.

In addition, because a class can implement multiple interfaces, object-oriented features make Java become very flexible.Using this feature, we can achieve a similar C + + language features like multiple inheritance, and even more flexible features.Below we discuss the practical application interface.

Second, using the interface to define some global variables

Because in the interface fields are static and final, so we can easily use this to create some constants.For example:

public interface Constants {
String ROOT = "/ root";
int MAX_COUNT = 200;
int MIN_COUNT = 100;
}

Can be used directly to refer to this form Constants.ROOT one of the constants.We can also use the following method to create the initial value of this constant uncertainty.

public interface RandomColor {
int red = Math.random () backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp 255;
int green = Math.random () backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp 255;
int blue = Math.random () backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp 255;
}

In which red, green and blue values will be accessed in the establishment of the first, and then remain unchanged.

Thirdly, use the interface to define the basic data structure
In the early stages of designing a software system, we can use the interface to some of the basic data elements to carry out some of the features described, according to the need for different implementations.Please take a look at this example:

public interface User {
int getAge ();
String getName ();
String getPassword ();
}

public class XMLUser implements User {
/ / Here User Interfaces with XML technology to achieve the method
public int getAge () {...}
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 {
/ / Here's UserFactory using XML technology to achieve the abstract method
}

In this case, we define an interface and an abstract class User UserFactory.Then we use XML technology to achieve these two classes.Can see, we just want to use UserFactory the getUserFactory () to get a UserFactory instance, without having to consider the specific method instance.By UserFactory this instance we can get an instance of User, not spent test specific implementation.

If we decide to use the JDBC technology to achieve User and UserFactory, we just need to realize the above form and JDBCUserFactory JDBCUser on the line.UserFactory then change it in the getUserFactory methods can change their implementation.And we have already written some call UserFactory and User do not need to make any changes.

This is the interface to define the data structure with a simple example, there are many practical applications in the use of flexible, we need to keep in the learning process to experience.

Fourth, understand the principle of distributed applications

There are many software programs use a distributed technology.There are many Java technologies support distributed applications, early use of a relatively large number of RMI, CORBA and other technologies, and now some of EJB technology more popular.But no matter how the development of these technologies are actually based on the interface.

To remote method invocation RMI (Remote Method Invocation) as an example.RMI in the preparation of applications, we need to do two basic things, we must first define an interface, this interface should inherit java.rmi.Remote interface, this interface should contain your name from the remote method call.The next step is to write a class to implement the interface methods.For example:

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 is placed in the client Product, which ProductImpl is on the server side, customers only need use the rules of the specified instance of Product on the line, do not have to consider the Product in the interface method is how to achieve.In these two classes defined, the use of Java Development Kit Command "rmic ProductImpl" to help us automatically generate two classes ProductImpl_Skel and ProductImpl_Stub.These two classes contain RMI calls to the operating mechanism.Interested friends can these two types of anti-compiled look.You will find the interface Product ProductImpl_Stub actually an implementation class.RMI mechanism is to use this class to generate an instance of Product for clients.Another class is in the service side ProductImpl_Skel ProductImpl_Stub call request response classes.The bottom RMI communication principle is to use the ObjectInputStream and ObjetOutputStream Socket will be called by the method name and parameter list to the server side, server-side method calls and through specific implementation class (in this case ProductImpl) of the mapping methodThen the results back to the client through the Socket on the line.As Skel and Stub classes are generated with the tool, so significant savings in development time.In addition, if we need to modify some implementations or wrong, just on the server side implementation class can be modified, which means that most of the maintenance of distributed applications can be completed on the server side.

Now, more and more applications that use EJB technology.EJB RMI evolved from a technology that was more perfect than the definition of RMI, you can get a better object-oriented features.But its complex rules than RMI.But no matter how complex it is, it is also the interface used to define a variety of Bean, also need to prepare the corresponding implementation class to perform specific functions and, finally, to communicate through the Socket.The operating mechanism of EJB itself has a certain degree of complexity, the efficiency of its application will be affected to some degree a matter of course.Therefore, development of technology in the choice of the application should be based on careful consideration of the size and characteristics, not necessarily popular technologies we will be able to adapt to your application.If you have a good grasp of object-oriented design principles, you can design their own.Maybe you can design your application features more suitable for distributed application structure.

V. CONCLUSIONS

In addition to some of the above applications, there are many places you can use an interface, such as Java's event mechanism in respect to the interface used.In addition, some have developed a good system to carry out major structural adjustment has not realistic, then you can define the appropriate interface and additional functionality to achieve to complete the structure of extension.

In short, to learn interface, you can help us better understand and apply the principles of object-oriented design.So that we can design better software systems.As I level of restrictions, if any, the Department also requested a lot of error correction.Thank you!

References
1. "Thinking in Java" 2nd Edition
2. "Enterprise JavaBeans" 3rd Edition
3. "Java2 core" Second Edition