How to design a Java framework----a simple example of "translation"

Source: Internet
Author: User

Original: http://www.programcreek.com/2011/09/how-to-design-a-java-framework/

Both the original and the translation are only references, if not, please correct me.

You might wonder how the framework works, and I'll create a simple framework to demonstrate.

Objectives of the framework
First, why do we need frameworks rather than libraries? The goal of the framework is to define a process that allows developers to implement certain functions based on personalization requirements, in other words, the framework defines the skeleton and the developer fills it.

A simple frame
In the following example, the first three classes are part of the framework, and the fourth class is the client code for that framework.

Main.java is the entry point of the frame and cannot be modified.

 1  //  2  public   main{ 3  public  static  void   main (string[] args) { 4  Huma n h=new  Human (new   Walk ());  5   H.domove ();  6   7  }

Move.java is a hook that allows developers to define/inherit functions based on their needs.

1  Public Abstract class move{2      Public Abstract void action (); 3 }

Human.java is a template that shows how the framework works.

1  Public classhuman{2     Privatemove move;3      PublicHuman (Move m) {4          This. move=m;5     }6      Public voidDomove () {7          This. Move.action ();8     }9}

This simple framework allows and requires the developer to inherit the "Move" class, in fact, in this framework, the action method is the only thing developers can change.

In an internal implementation, different "action" can be written to achieve different purposes. For example, the following example prints out "5 miles per hour" and, of course, you can also change to "miles per hour".

1  Public class Walk Extend move{2    @Override3public     void  Action () {4         System.out.println ("5 miles per hour-it is slow!" ); 5     }6 }

Summarize

This example shows just how a simple template and hook works, a practical framework that is more complex than the template-instance relationship, and a complex process for how to effectively improve performance and program usability.

How to design a Java framework----a simple example of "translation"

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.