Now, what GUI do you use to develop the most and hottest applications in Java? andorid! No one should know! Do you know of AWT, Swing, SWT, and JavaFX? Although they are primarily used to develop desktop applications, it is not entirely true, and JavaFX can be exported as a mobile app. Whatever, no matter what platform they face, they are Java-based GUI toolkit or framework, as the technology development, there will be more and more GUI tools will appear, this is certain. Are we programmers so hard, every time the technical update, we need to start again from 0? The answer is no!. Just like us humans, although everyone is different, there is something inside that allows us to think, communicate and walk. We need to peel off the GUI and refine those timeless things, which is the purpose of the Reflex framework. So I say we are blessed, let us seize the eternal and core things, status quo, so, no matter how the GUI changes, are so easy!
Reflex's Chinese meaning is reflection, the entire frame is created according to Reflex arc mode. Yes, it is the reflection arc that our junior high school biology textbook has attended. If you are interested, you can go to the Baidu Encyclopedia to know the new. Let me briefly talk about its structure and try not to get bogged down in too many theories. For everyone to have an intuitive understanding, this article is mainly based on a simple Hello World example.
The reflex framework divides an application into 5 parts: views, receptors, business hubs, data models, effectors. The business hub is equivalent to the business layer, and the data model is the data tier. The framework is independent of the view, our main work is:
- Defines a receptor to perceive the behavior of the view.
- The receptor notifies the business hub that the business hub changes the data model or coordinates the relationships between the various business hubs.
- Defines the effectors, binds the data to the specified view.
From the above, only the receptor and the effector are related to the view, one is the monitoring view behavior, one is to provide data for the view. To be independent of the view, we can specify the view by annotation.
Nonsense no longer say, later interested, we can slowly understand. Simple, rough, first.
This example is simple, there are two elements on the view, one is a button, and the other is text. The action is this: click on the button, in the business center there is a count, each point, the count is increased by 1. The text shows "Hello World" plus the count in the business hub. Such as:
As you can see, the text display is automatically updated when the button is clicked. The code for the monitor button click Is this:
@Receptor Public classBtreceptor {/*** Automatic injection of business hub, access to business hub via interface*/@AutowiredPrivateIhellocenter Hellocenter; /*** The Sensing object is a view of the ID BT, and the behavior stimulus is: the Click event. * @paramView*/@Recept (Target= "BT", stimulation= "Android.view.view$onclicklistener") Private voidonremovebtclicked (view view) {Hellocenter.changecount (); }}
We do not need to create the receptor object manually, with the @receptor tag on the receptor class, and the framework will automatically create the object when appropriate. On the Onremovebtclicked method, there is a recept annotation that defines the views and interfaces of the listener. Views and interfaces are now represented in string form for general purposes. In different GUI frameworks, different matching algorithms are required to match the view. On Android, here's BT, will automatically match r.id.bt this button. As for the interface, currently only support write full interface, a bit cumbersome, is not it? Try to solve it later. Given a behavior interface, this annotated method is equivalent to that of the interface's callback function. In this example, the Changecount method that invokes the business hub is called after the Monitor button is clicked.
Business hub because of the @autowired relationship, the Business hub object is automatically injected when Btreceptor is instantiated. The concrete implementation of it is this:
@Center Public classHellocenterextendsBindableawareImplementsIhellocenter, initializable {Private intCount = 0; @Override Public voidChangecount () {count++; Invalidatebind ("Hello"); } @Bindable (Name= "Hello") @Override Public intGetCount () {returncount; } @Override Public voidoninitialized () {}}
Functionally simple, not much to say, is to add a count. Need to describe the data binding, the business hub needs to inherit the Bindableaware class, it has a method, Invalidatebind. Call the Invalidatebind method to notify the outside, and what fails. In this case, the data binding named Hello is invalidated and needs to be renewed. The data binding named Hello corresponds to the GetCount method, which means that the GetCount method is obsolete.
For example, the effector code calls the GetCount method:
@Effector Public classTexteffector {/*** Automatic injection of business hub, access to business hub via interface*/@AutowiredPrivateIhellocenter Hellocenter; /*** The Effect object is the view with the ID text, and the effect orientation is the Text property. * @paramView*/@Effect (Target= "Text", site= "text") PublicString Gethellotext () {return"Hello World" +Hellocenter.getcount (); }}
The framework then calls the Gethellotext method again and assigns the result to the R.id.text view, which completes the process. Example program complete code here
In fact, the amount of code is not many, the structure is very clear, is not very simple? To summarize briefly, Reflex did the following things for everyone:
- The method of interacting with views is defined in a annotation manner, independent of the view.
- The program is divided into several parts, each of which has its own special responsibility, the structure is clear.
- Automatic data binding
- The business object is automatically injected.
Well, today it is simple to say that, Reflex is still only a prototype, from the real practical and there is a great distance, but I will improve. Although so to say, but my personal strength is limited, if who is interested in progress together, the project here, here is extremely grateful.
The programmer is blessed: GUI-independent Java application Framework Reflex was born!