Why internal class: control framework

Source: Internet
Author: User

So far, you have been exposed to a large number of syntaxes and concepts described for the operation of internal departments and classes. However, these do not really explain the causes of internal classes. Why is Sun so troublesome to add such a basic language feature in Java 1.1? The answer lies in the "control framework" we will learn here ".

An "Application Program Frameworks refer to one or more classes that are specifically designed to solve specific types of problems. For the application framework, we can inherit from one or more classes and cover some of them. We wrote Code Customizes the general solutions provided by those application frameworks to solve their actual problems. The "Control Framework" is a special type of application framework that is dominated by the need to respond to events. A system mainly used to respond to events is called an "event-driven system ". One of the most important issues in the application design language is the graphical user interface (GUI), which is almost entirely event-driven. As you have learned before, Java 1.1 AWT is a control framework that perfectly solves GUI problems through internal classes.

To understand how internal classes simplify the creation and use of control frameworks, you can think that the work of a control framework is to execute them after the events are "ready. Although "ready" means a lot, in this case, we are based on computer clock. Later, please realize that the Framework does not contain any specific information about what needs to be controlled by the control framework. First, it is a special interface that describes all control events. It can be an abstract class rather than an actual interface. Because the default behavior is controlled by time, some implementation details may include:

//: Event. Java
// The common methods for any control event
Package c07.controller;

Abstract Public class event {
Private long evttime;
Public event (long eventtime ){
Evttime = eventtime;
}
Public Boolean ready (){
Return System. currenttimemillis ()> = evttime;
}
Abstract Public void action ();
Abstract Public String description ();
}///:~

When you want the event to run, the builder simply captures the time. Ready () also tells us when to run it. Of course, ready () can also be overwritten in a runtime class to create events on other things except time.

Action () is the method that needs to be called after the event is ready, and description () provides event-related text information.

The following file contains the actual control framework used to manage and trigger events. The first class is actually a "helper" class, which is responsible for accommodating event objects. Replace it with any appropriate set. In addition, through the study in chapter 1, we will know that other sets can simplify our work, and we do not need to write these additional code:

//: Controller. Java
// Along with event, the generic
// Framework for all control systems:
Package c07.controller;

// This is just a way to hold event objects.
Class eventset {
Private event [] events = new event [100];
Private int Index = 0;
Private int next = 0;
Public void add (event e ){
If (index> = events. length)
Return; // (in real life, throw exception)
Events [index ++] = E;
}
Public event getnext (){
Boolean looped = false;
Int start = next;
Do {
Next = (next + 1) % events. length;
// See if it has looped to the beginning:
If (START = NEXT) looped = true;
// If It loops past start, the list
// Is empty:
If (next = (start + 1) % events. length)
& Looped)
Return NULL;
} While (events [next] = NULL );
Return events [next];
}
Public void removecurrent (){
Events [next] = NULL;
}
}

Public class controller {
Private eventset es = new eventset ();
Public void addevent (event c) {es. Add (c );}
Public void run (){
Event E;
While (E = es. getnext ())! = NULL ){
If (E. Ready ()){
E. Action ();
System. Out. println (E. Description ());
Es. removecurrent ();
}
}
}
}///:~

Eventset can accommodate 100 events (if you use a "real" set from Chapter 1 here, you don't have to worry about its maximum size because it will automatically change the size as needed ). The index is used to track the next available space, and next helps us find the next event in the list to see if we have already made a loop. This is crucial in the call to getnext (), because once it is run, the event object will be deleted from the list (using removecurrent ()). Therefore, getnext () will encounter an "empty" when moving forward in the list ".

Note that removecurrent () is not just a flag indicating that the object is no longer in use. Instead, it sets the handle to null. This is very important, because if the garbage collector finds that a handle is still in use, it will not clear the object. If you think your handles may be suspended as they are now, you 'd better set them to null so that the garbage collector can clear them normally.

Controller is the place for actual work. It uses an eventset to hold its own event object, and addevent () allows us to add new events to this list. But the most important method is run (). This method will traverse in eventset and search for a ready-to-run event object -- ready (). It finds that every object in ready () will call the action () method, print the description (), and delete the event from the list.

Note that in all designs so far, we still cannot accurately know what to do with an "Event. This is the key to the entire design. How does it "separate things that have changed from things that have not changed "? Or in my words, the "Change intention" causes different actions of various event objects. We create different event subclasses to express different actions.

Here is where the internal class shows its talents. They allow us to do two things:

(1) express all the implementation details of a control framework application in a single class, so as to completely encapsulate everything related to that implementation. Internal classes are used to express various types of actions (), which are used to solve actual problems. In addition, the following examples use private internal classes, so the implementation details are completely hidden and can be modified securely.

(2) The internal class makes our specific implementation more clever, because it can easily access any member of the external class. If you do not have this capability, the Code may not seem so comfortable and you have to find other solutions.

Now let's think about a specific implementation method of the control framework, which is designed to control the greenhouse function (note 4 ). Every action is completely different: control the opening and closing of lights, water supply, and automatic temperature adjustment, control the bell, and restart the system. However, the purpose of the control framework is to easily isolate different codes. For each type of action, you must inherit a new internal class of event and write the corresponding control code in Action.

④: For some special reasons, this is a very interesting problem that I often need to solve; the original example also appeared in C ++ inside & out, but Java provides a more comfortable solution.

As a typical behavior of the application framework, the greenhousecontrols class inherits from the Controller:

//: Greenhousecontrols. Java
// This produces a specific application of
// Control system, all in a single class. Inner
// Classes allow you to encapsulate different
// Functionality for each type of event.
Package c07.controller;

Public class greenhousecontrols
Extends controller {
Private Boolean light = false;
Private Boolean water = false;
Private string thermostat = "day ";
Private class lighton extends event {
Public lighton (long eventtime ){
Super (eventtime );
}
Public void action (){
// Put hardware control code here
// Physically turn on the light.
Light = true;
}
Public String description (){
Return "light is on ";
}
}
Private class lightoff extends event {
Public lightoff (long eventtime ){
Super (eventtime );
}
Public void action (){
// Put hardware control code here
// Physically turn off the light.
Light = false;
}
Public String description (){
Return "light is off ";
}
}
Private class wateon extends event {
Public wateon (long eventtime ){
Super (eventtime );
}
Public void action (){
// Put hardware control code here
Water = true;
}
Public String description (){
Return "greenhouse water is on ";
}
}
Private class wateroff extends event {
Public wateroff (long eventtime ){
Super (eventtime );
}
Public void action (){
// Put hardware control code here

The preceding content is transferred from Baidu.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.