Command dispatching command scheduling
IN C and C , programs offen use a set of global functions and function Pointers to represent a series of commands or operations and kil. For example: In C / C , programmers often define some global functions And function pointer to describe a series of commands or operations, then scheders them through a list. An example:
#include
Java, though, does things differently. It does not use pointers and does not have global functions. Instead, Java uses a technique of creating and referencing object instances through which the objects call a method known to be implemented by an object of a Given Type. Although there is no global function and no function pointer in Java, Java is implemented in different ways. The replacement method is to implement this function using the reference of the Java.
How does The Technique Work In Practice? Consider this Example: How this technology is implemented, you can refer to the following example:
/ / Define an interface interface action {public void DOIT ();} // Defines class FUNC1, this class implements an action interface class func1 imports action {public void DOIT () {system.out.println ("func1"); }} // Define class func2, which also implements an action interface class func2 implements action {public void DOIT () {system.out.println ("func2");}} // Public class call {Private by dynamic call Static action alist [] = {new func1 (), new func2 ()};
Public static void main (string args []) {for (INT i = 0; I } In this example, an interface Action is defined, and any class that implements the interface must define a method doit (this notion, by the way, is basic to the meaning of interfaces). Objects of the class can be referred to with a reference TO THEMENTED Interface Name Type, SO That A Reference Of Type Action CAN REFER TO An Object of a Class That Implements Action. In this example, an Action interface is defined. The interface defines a DOIT method and two implementations. Class FUNC1 and FUNC2. For instances that implement the Action interface, you can reference the Action's reference. So this action can point to an instance of the class that implements this interface. In C Programming Action Would Be A base Class, with func1 and func2 being derived classes, and object manipulation would be performed via a base class pointer. with Java, an Action reference supports a similar manipulation, although Action is not a superclass of func1 and func2. in C language, Action interface Convert into a base class, FUNC1 and FUNC2 can be a subclass, and for subclass of instances can be saved with base class variables. In Java, although the Action interface is not a specific class of parent classes, but by the base class reference quotation The mechanism is still the same. The Java technique given here sometimes goes by the name "method wrappers," and is quite different from the C / C approach. There are some tradeoffs, of course, as to which approach works the best in a given situation. This Java Technology It is often referred to as "method packaging", which is very different from C . But what is appropriate to see business use. A Second Example Second Example Another Example of The Method Wrappers Technique Is Used Fairly Offer The Abstract Windowing Toolkit (AWT). Consider The Following: About the method package, this technology is usually used in AWT. See the code: import java.awt *;. import java.awt.event *;. // a button listener implements ActionListener interface public class buttonlistener implements ActionListener {// implements actionPerformed method public void actionPerformed (ActionEvent e) {System.exit (0); Public static void main (string args []) {frame f = new frame ("testing"); button b = new button ("ok"); b.AddActionListener (New ButtonListener ()); f.Add (b); F.PACK (); f.setvisible (true);}} The example creates a frame and adds an OK button to it. When the button is selected, the program terminates. An action listener is added to the button. The action listener is guaranteed to implement a method actionPerformed, because an instance of buttonlistener implements the ActionListener Interface. Selecting The Button Results In a call to the action listner for the button. New window is created, the window has only one OK button. When the button is pressed, the program ends. For the OK button, we add him to him A listener, the listener implements an actionPerformed method, and the result of the button will call the listener of this button. Finally, the listeners concept is extremely important in the AWT. The underlying basis of listeners involves implementing specified interfaces and thereby guaranteeing that a listener will be able to respond to a particular known method invocation on it. Finally emphasize that the concept of listeners It is very important in AWT.