Java programming interface design

  Abstract: The design of the Java programming interface 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="212" height="86" align="center" valign="top"> </ td> <td width="472" valign="top"> 

  Java language provides an interface (interface) mechanism.    This interface mechanism to Java object-oriented programming has become more flexible.    We can use interface to define a category of the manifestations, but can not contain any interface to achieve.    "Thinking in Java", a book, the author of this interface description: "Interface (interface) than abstract (abstract) concept goes a step further. You can put an interface as a purely abstract classes . "I think that the author of this interpretation of interface has accurately. 

  Understand and use interface mechanism will help us better control of this object-oriented Java programming language.    Below we discuss the interface to the use of rules, as well as related applications. 

  First, the definition and implementation of interface definition and the definition of interface definition similar to class just for the interface keyword.    Definition methods need only method name, return type and parameter list, and there are ways of not.    Interface can be defined fields, these fields have been alluding to static and final, it should be based on need to set the value of these fields.    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, and Talkable Flyable only defines one method, and Message in addition there is a method for the field MAX_SIZE.    These interfaces can be seen only definition of the category of forms, and does not contain any realized, it is not used directly.    To use these interfaces requires a corresponding category to realize them.    Implementation interface should be in the first category were later keyword Affirming that implements the interface to achieve, if multiple interfaces, they should be separated by a comma, and then on January 1 achieve these methods defined in the interface.    As in the following examples: 

  </ Td> </ tr> <tr> <td height="20" colspan="2"> 

  </ Td> </ tr> </ table> 

  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.substring message = (0, MAX_SIZE); 
  ) 

  Public String getMessage () ( 
  Return message; 
  ) 
  ) 

  In Parrot (parrot) example, we used to interface Flyable said flying abilities, Talkable said that the ability to speak, but they do not contain concrete realization.    The Parrot at the same time these two capabilities, we Parrot category at the same time as the two Flyable and Talkable interface.    Similarly, we can also define a Swallow (Swallow) category, but only swallows flying abilities, we need only realize Flyable Swallow on the trip.    Because their respective flight methods vary, they have their own on a concrete realization of the flight. 

  In addition, it is precisely because a class can implement multiple interfaces, making the object-oriented characteristics of Java has become very flexible.    Use of this feature, we can achieve similar to the C + + language, as many of the characteristics of succession, and even more flexible features.    Below we discuss the interface to the application in practice. 

  Second, the interface used to define global variables because some of the fields because the interface is static and final, so we can be very convenient to use this to create some constants.    For example: 

  (Public interface Constants 
  String ROOT = "/ root"; 
  Int MAX_COUNT = 200; 
  Int MIN_COUNT = 100; 
  ) 

  When in use can be directly used in the form of such Constants.ROOT quoted one of constants.    We can also use this method to create below the initial value of the constant uncertainty. 

  (Public interface RandomColor 
  Int red = Math.random () * 255; 
  Int green = Math.random () * 255; 
  Int blue = Math.random () * 255; 
  ) 

  Which red, green and blue values will be the first visit by the establishment, and then remain unchanged. 

  Third, to define the interface using basic data structure in the design of a software system in the early stages, we can use some of the basic interface to the data elements to carry out some of the characteristics described, the light of 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 to XML User Interface technology in the way 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 ( 
  / / XML technology here to the realization of the abstract methods UserFactory) 

  In this example, we defined a User interface and an abstract class UserFactory.    Then we use XML technology to achieve these two categories.    , We can see that just from the use UserFactory getUserFactory () can be a UserFactory example, and not take up consideration of specific examples of this method.    UserFactory through this example we can also be directly User example, do not take up the realization of specific test methods. 

  If we decided to use the JDBC technology to achieve User and UserFactory, we need only in accordance with the above in the form of JDBCUser and JDBCUserFactory on up.    Then the getUserFactory UserFactory methods can be modified to change their methods.    We have written and called User UserFactory and some do not make any changes. 

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

  Fourth, understand the principle of distributed applications currently there are many software projects using distributed technology.    Java support distributed applications with a variety of technologies, early use of a relatively large number of RMI, CORBA, and other technologies, and now EJB technology more popular.    But regardless of 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, the interface should inherit java.rmi.Remote interface, the interface should be included in the distal you should call the method name .    The next step is to write a class to achieve this 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 Product is on the client, and ProductImpl is on the server side, customers in the use, and only use designated Product examples of the rules on the line, not to consider Product interface, the method is how realize.    Good in the definition of these two categories, the use of the Java Development Kit Order "rmic ProductImpl" can help us to automatically generate and ProductImpl_Stub ProductImpl_Skel two categories.    This category includes the two RMI call the operating mechanism.    Interested friends can compile these two categories-after look.    You will find that which is actually ProductImpl_Stub Interface Product Implementation of an.    RMI is to use this mechanism to generate Product category for examples of the use of the client.    Another category ProductImpl_Skel is in the service-call response to the request ProductImpl_Stub category.    RMI and the bottom of the principle of communication is through the use of Socket ObjetOutputStream ObjectInputStream and will be called by name and the list of parameters to a server end, server-side and through the specific method calls implementation class (in this case is ProductImpl) corresponding methods , and then the results will be sent back through Socket client on the trip.    The Skel and Stub tools category is generated, and so they greatly save development time.    Also, if we need to amend the method or some mistakes, only the need for the realization of server-side changes can be, that is to say that the majority of distributed applications in the server-side maintenance work can be completed. 

  Now, more and more applications using EJB this technology.    EJB is an RMI developed from the technology, it has a more complete definition of RMI, can be better object-oriented features.    But its rules than RMI complicated.    But neither it more complicated, it is the same interface used to define a variety of Bean, and also the realization of the need to prepare the corresponding category to complete specific function, but also through the final Socket to communicate.    EJB mechanism for the operation of the complex itself to a certain extent, so the efficiency of their application will be granted a certain impact.    Therefore, in the choice of technology should be developed in accordance with the size and characteristics of careful consideration, not necessarily popular technology will certainly be able to adapt to your application.    If you have a good object-oriented design principles, you can design their own.    Maybe you can use in accordance with the characteristics of their own design more suitable for distributed application structure. 

  V. Conclusion In addition to a number of applications in addition to the above, there are many local interface can be used, for example, the events in Java on the mechanism used to the interface.    In addition, the number of good systems have been developed in the structure to a higher level of adjustment has been not realistic, then by definition some additional interface and the corresponding function of the realization of the structure to complete the expansion. 

  In short, master interface can help us better understand and use of object-oriented design principles.    So that we can design better software systems.    As I level restrictions, if errors also to pay more attention to the correction. 

  </ Td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , , , ,

Releated Java Articles

Comments

Leave a Reply