Generally, we need to design our classes so that each class does "one thing." This is especially important when it comes to user interface code, because it's easy to encapsulate "what you want to do" and "how to show it." This effective coordination prevents the code from being reused. Not to mention it's satisfying to differentiate our "logic of Things" from the GUI. With this approach, we can reuse the logic of things more easily, and it makes it easier to reuse the GUI.
Other controversies are the multi-level systems in which the "Action object" exists to complete the separation machine. The main positioning rule of the action allows all new events to be modified immediately, and this is such a compelling way to set up the system. However, these action objects can be used in a number of different applications and are therefore not constrained by some special display modes. They perform action actions reasonably and there are no extra events.
The following example demonstrates how easy it is to distinguish logic from GUI code:
: Separation.java//separating GUI logic and Business Objects import java.awt.*;
Import java.awt.event.*;
Import java.applet.*;
Class Businesslogic {private int modifier;
businesslogic (int mod) {modifier = mod;
public void setmodifier (int mod) {modifier = mod;
public int Getmodifier () {return modifier;
}//Some business operations:public int calculation1 (int arg) {return arg * modifier;
public int calculation2 (int arg) {return arg + modifier;
} public class separation extends Applet {TextField t = new TextField, mod = new TextField (20);
Businesslogic bl = new Businesslogic (2);
Button Calc1 = New button ("Calculation 1"), CALC2 = New button ("Calculation 2");
public void Init () {Add (t);
Calc1.addactionlistener (New calc1l ());
Calc2.addactionlistener (New calc2l ()); Add (CALC1);
Add (CALC2);
Mod.addtextlistener (New ModL ());
Add (New Label ("Modifier:"));
Add (MoD); } Static int GetValue (TextField tf) {try {return Integer.parseint (Tf.gettext ());
catch (NumberFormatException e) {return 0; The class calc1l implements ActionListener {public void actionperformed (ActionEvent e) {t.settext (Integer).
ToString (Bl.calculation1 (GetValue (t))); The class calc2l implements ActionListener {public void actionperformed (ActionEvent e) {t.settext (Integer).
ToString (Bl.calculation2 (GetValue (t))); } class ModL implements TextListener {public void textvaluechanged (TextEvent e) {bl.setmodifier (getValue
(mod));
} public static void Main (string[] args) {Separation applet = new separation ();
Frame aframe = new Frame ("separation"); Aframe.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {System.
Exit (0);
}
});
Aframe.add (applet, borderlayout.center);
Aframe.setsize (200,200); Applet.init ();
Applet.start ();
Aframe.setvisible (TRUE); }
} ///:~
As you can see, the logic of things is a class that completes its operations directly without prompting and can be used in a GUI environment. It's just right for it to work. The distinguishing action records the details of all the UI, and it communicates with the logic only through its public interface. All operations revolve around the center to get information back and forth through the UI and things logic objects. So differentiate, take turns to do its work. Because the distinction only knows its logical object dialogue (that is, it does not have a high degree of union), it can be forced to talk to other types of objects without more trouble.
Think about the conditions that distinguish the UI from the logic of things, and think about how to make it easier to survive when we tweak the traditional Java code to make it run.