Scalable Java application development mode

Source: Internet
Author: User

Nowadays, many JAVA books generally do everything in the main class (their own JFrame subclass or JPanel) to make JAVA beginners easy to understand and get started when introducing SWING event models: either the main class acts as the listener, and the method of the interface determines what to do by determining the event source; or the listener is implemented by using the implicit class in the main class, (because the implicit class can easily access the member variables in the main class ). Many JAVA beginners also adopt this mode in later programming (at least I used to do this ). In fact, this approach is simple, but lacks readability, scalability, and maintainability. I have read an article on this issue (from JavaPro) and I will share it with you.

Below I will use a simple example to illustrate how to use the mode to solve this problem.

Now, let's write a program that uses several single-choice buttons to select different groups from a long list, as shown in. When you click the Female single-choice button, only the girl's name is displayed. When you click the Male button, the program displays the boy's name.


The simplest version of this program is to do everything in the main class. The main class contains arrays and vectors that hold the child's name ), another simplest method is to create an object for each child. Each object has a method like isFemal () to help us determine whether we want to display the object as a man or a woman. In the program, we call every child's class called Timer mer:

public class Swimmer{
 private String name;
 private int age;
 private String club;
 private boolean female;

 public Swimmer(String name,int age,String club,boolean female){
  this.name=name;
  this.age=age;
  this.club=club;
  this.female=female;
 }

 public boolean isFemale(){
  return female;
 }

 public String getName(){
  return name;
 }

 public int getAge(){
  return age;
 }

 public String getClub(){
  return club;
 }
}

What we need to do next is to determine which children will be displayed in the entire list. We can do this in the main user interface class, but it is best to do it in a collection class called aggregmers. This class has a getList () method, it has a Boolean parameter to determine whether it is a boy or a girl:

//get a vector of swimmers who are (female)
public Vector getList(boolean female){
 Vector v=new Vector();
 for(int i=0;i   Swimmer swm=(Swimmer)kids.elementAt(i);
  if(swm.isFemal()==female) v.add(swm);
 }
 return v;
}

We also provide a multi-state getList () method without parameters to return the entire list.

Now let's enter the first important part of the program. to display the names of these children, we need to add an event listener for all three radio buttons, so that each of them can display the correct children:

public void actionPerformed(ActionEvent e){
 //listen for button clicks and do the right thing
 Object obj=e.getSource();
 if(obj==female) loadFemales();
 if(obj==male) loadMales();
 if(obj==both) loadBoth();
}

private void loadFemales(){
 //display female swimmers
 Vector v=swimmers.getList(true);
 loadList(v);
}

private void loadMales(){
 //display male swimmers
 Vector v=swimmers.getList(false);
 loadList(v);
}

Although this method works well in simple and small cases, it does not have good scalability. If you have 10 buttons, extend the actionreceivmed () method to test each button and call some corresponding operations, which will make the readability very poor. Instead, we 'd better remove all the judgments from the class containing the user interface.

One way to do this is to use the Command mode. When we use this mode, we need to create a Command interface:

//the Command interface
public interface Command{
 public void execute();
}

We can extend the three single-choice buttons to a specific class with the Command interface, so that we can transfer the execution of the Command from the JFrame class to the class of each button. At the same time, we create a base class called SexButton and move the ActionListener code to the base class. In this way, we do not need to add event listeners to each button separately:

//abstract radio button class
public abstract class SexButton
extends JRadioButton implements Command{
 protected Swimmers simmers;
 //JawList is a subclass of JScrollPane contained a JList;
 protected JawList kidList;

 public SexButton(String title,Swimmers sw,
 JawList klist,ActionListener al){
  super(title);
  swimmers=sw;
  kidList=klist;
  addActionListener(al);
 }

 //abstract execute method
 public abstract void execute();
}

Note that this class must be extended to make it useful, because we have no specific execute () method. This basic abstract button class is just a template of the specific class we export from it, which is actually a simple example of the template design pattern.

//radio button to select female swimmers
public class FemaleButton extends SexButton{
 public execute(){
  Vector v=swimmers.getList(true);
  loadList(v);
 }

 private void loadList(Vector v){
  kidList.clear();
  for(int i=0;i    Swimmer swm=(Swimmer)v.elementAt(i);
   kidList.add(swm.getName());
  }
 }
}

Now let's take a look at our work. All the buttons become Command buttons, and the actionreceivmed () method is simplified as follows:

public void actionPerformed(ActionEvent e){
 Command cmd=(Command)e.getSource();
 cmd.execute();
}

As you can see, this is simpler and completely scalable.

But we still have something to do. Now we have written three radio button classes that know how to load a list box. When we rush to get a message out of the main class, we require that every button be aware of the list box. If we want to change to a different display mode, we must modify these three classes.

It would be much better if we create a mediator class between the button and the list, because they do not need to know each other. This can be done in the Mediator design mode. When a button is clicked, we create a scheduler class load list. In this way, all the buttons only know the Mediator and the list to be loaded will only be known to the dispatcher. If we want to change to a different display mode, we only need to modify the Mediator:

public class Mediator{
 private JawList kidList;

 public Mediator(JawList klist){
  kidList=klist;
 }

 public void loadList(Vector v){
  kidList.clear();
  for(int i=0;i    Swimmer sw=(Swimmer)v.elementAt(i);
   kidList.add(sw.getName());
  }
 }
}

In this way, our base class becomes like this:

public abstract class SexButton
extends JRadioButton implements Command{
 protected Swimmers swimmers;
 protected Mediator med;

 public SexButton(String title,Swimmers sw,Mediator md,ActionListener al){
  super(title);
  swimmers=sw;
  med=md;
  addActionListener(al);
 }

 public abstract void execute();
}

FemalButton becomes like this:

public class FemaleButton extends SexButton{
 //use the mediator to load the list
 public void execute(){
  Vector v=swimmers.getList(true);
  med.loadList(v);
 }
}

You see, we use Mediator to separate buttons and lists so that they do not know each other.

In addition to the simple program that can improve less than 100 lines of Java code by using commands, schedulers, and template modes, you can also use the Observer (Observer) mode. This program uses JListData to pass the data to the list and obtain the data from the list, and uses LawList to observe the changes of JListData.

public class JListData extends AbstractListModel{
 private Vector data;

 public JListData(){
  data=new Vector();
 }

 public int getSize(){
  return data.size();
 }

 public Object getElementAt(int index){
  return data.elementAt(index);
 }

 public void addElement(String s){
  data.addElement(s);
  fireIntervalAdded(this,data.size()-1,data.size());
 }

 public void removeElement(String s){
  data.removeElement(s);
  fireIntervalRemoved(this,0,data.size());
 }

 public void clear(){
  int size=data.size();
  data.clear();
  fireIntervalRemoved(this,0,size);
 }
}

public class JawList extends JScrollPane{
 private JList listwindow;
 private JListData listContents;

 public JawList(){
  listContents=new JListData();
  listwindow=new JList(listContents);
  getViewport().add(listwindow);
 }

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.