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 are too many underlying details to deal with.

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), and the detailed method body is provided by the upper code.

OK, the frame has been designed.

Now is the time for our application ape to work.

Application Ape Most of course write HelloWorld, come to know the frame ... HelloWorld can not and arrogant frame in a package.

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

2065=principle.callback.helloworld

Next, execute the framework on the command line, such as:

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

Sometimes, our application ape launches or invokes the framework in the application, and 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 program of the design of the app Ape and Design HelloWorld Program Ape Freemasonry, their own people do not have to reflect:

    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 program that writes the upper modules has a new experience-- fill in the blanks.

This has the difference between the library and the framework -the upper module of the program ape directly called, belongs to the library function, the request of the upper module of the program Ape provides the callback function, belongs to the framework.

Suppose we take all the upper and lower layers and use a callback mechanism when designing the framework; Suppose we let the foolish application Ape fill in the blanks, haha, we tell them a term-control reversal.

Suppose we are the framework of the application ape, ... 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.