The simplest Java framework

Source: Internet
Author: User

The purpose of the frame framework is to define a skeleton scheme that deals with the same underlying details, and when developers use the framework, they can implement their own functions according to their own needs--just fill in their own things/flesh.

The simplest frame, similar to JUnit, has a main (string[] args) that launches the framework. If it is an applet or GUI framework, there is too much underlying detail to handle.

Package principle.callback.lower;/** * Simplest java framework *  * @author yqj2065 * @version 2014.10 */public Interface Myframewor k{public    Double op (double m,double n);
The simplest frame is a function interface. lower. Myframework, which returns a double value after a two double operation.

Package Principle.callback.lower;import tool. God;public class main{    /**     * command line launches this framework.     * @param args at least two double     *   /public static void main (string[] args) {        myframework f = (myframework) God.create ("2065");        Double D =f.op (Double.parsedouble (Args[0]), double.parsedouble (args[1]));        System.out.println (d);    }    /**     *, like JUnit, provides the way the app is called.     */public    static double runframework (myframework f,double x,double y) {        return f.op (x, y);    }}

Note that the technique used by the framework is reflection and callback .

    • Tool. God uses reflection and configuration files to create an object,
    • Main calls F.op (double,double), the specific method body is provided by the upper code.

OK, the frame has been designed and completed.

Now is the time for us to apply the programmer to work.

The application programmer is the first of course to write HelloWorld, to be familiar with the framework ... HelloWorld is not able to be in a package with the arrogant frame.

Package Principle.callback;public class HelloWorld implements principle.callback.lower.myframework{public    Double op (double m,double n) {        return m+n;    }}
Application programmers also need to write their own configuration files according to the requirements of the framework. If the framework's documentation explains that the object to be created needs to be defined in my.properties, we add the following in the My.properties:

2065=principle.callback.helloworld

Next, run the framework on the command line, for example:

E:\designpattern>java Principle.callback.lower.Main 1.2 3.4
4.6

Sometimes, our application staff launches or invokes the framework in the application, or it can:

Package Principle.callback;import tool. God;import Principle.callback.lower.myframework;import Principle.callback.lower.main;public class App{public    static void Main (string[] args) {        myframework f = (myframework) god.create ("2065");        Double d = main.runframework (f,1.2,3.4);        System.out.println (d);    }}
Of course, the programmers who design the app and the programmers who design the HelloWorld have no reflection:

    public static void Test () {        double d = main.runframework (new HelloWorld (), 1,3);        System.out.println (d);                Myframework f = (double m,double n)->{return m * n;};        D = main.runframework (f,1,3);        System.out.println (d);                f = (m,n)->{return m +2*n;};        D = main.runframework (f,1,3);        System.out.println (d);    }
Output:

4.0
3.0
7.0

Callback The programmer who writes the upper modules has a new experience-in-the- blanks programming .

This has the difference between the library and the framework --the programmer of the upper module calls directly, belongs to the library function, and requires the programmer of the upper module to provide the callback function, which belongs to the framework.

If we take all the upper and lower layers, we design the framework with a callback mechanism; if we let the stupid application programmers fill in the blanks, we tell them a term-control reversal.

If we are an application member of the framework, ... Well, we also design a framework that controls inversion.




The simplest Java framework

Related Article

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.