more

 

First, localized changes - the goal is to reduce the number of directly affected by the change of a module;

1, is expected to expected changes (expected changes):

Ensure canVote () method returns true or false, and you can write a test to validate this method throws IllegalArgumentException.

Copy the code
public class Student {
 
  public boolean canVote(int age) {
      if (i<=0) throw new IllegalArgumentException("age should be +ve");
      if (i<18) return false;
      else return true;
  }
}
Copy the code

Guava class library provides a tool for a class parameter category --Preconditions inspection, this method may be better able to check such parameters, but in this example it is possible to check the

 

2, maintaining semantic consistency (semantic coherence):

3, generalization module (Generalize the module):

4, limited to select parameters (Limit possible options):

 

Second, to prevent a chain reaction - the goal is to limit the localized modification of the module, in order to prevent change to a module indirectly affect other modules;

 

Try to maintain existing interfaces such as the name or class of the same, to try to change the module to a minimum. Late binding time in my system has not been reflected.

Maintaining semantic consistency is to ensure that the module can work together between the different responsibilities, do not rely too much on other modules.

1, information hiding (Hide information):

2, to maintain the existing interface (Maintain existing interfaces):

3, the communication path limit (Restrict communication paths):

4, using the arbiter (Use an intermediary):

 Demeter using a chain reaction can be effectively prevented:

example:

In the American film "The Godfather" in order to get rid of the godfather if the opponent will do hands? Certainly not, godfather will arrange his men deal with. Godfather of such high position who will direct you to schedule tasks with the killer? Generally not, he will talk to trusted men of the description, and then executed by his men go.

As a result, we look at, take a look at three common roles, slain Person, killer Killer, confidant CoreMember, as follows:

Copy the code
/**
 * someone
 * @author ljtyzhr
 *
 */
public class Person{
    public String name;
}
 
/**
 * Killer
 * @author ljtyzhr
 *
 */
public class Killer{
     public void kill(Person someone){
         System.out.println (someone.name + "was killed");
     }
}
 
/**
 * Godfather around the core staff
 * 
 * @author ljtyzhr
 *
 */
public class CoreMember{
    private Killer killer;
}
Copy the code

In fact, the core staff to deal directly with the killer, godfather only deal with trusted, so the relationship should be as follows:

 

Copy the code
/**
 * Godfather around the core staff
 * 
 * @author ljtyzhr
 *
 */
public class CoreMember{
    private Killer killer;
    public void kill(Person someone){
              killer.kill(someone);
    }
}
Copy the code

My father holds a reference to the key personnel, as follows:

Copy the code
/**
 * The Godfather
 * @author ljtyzhr
 *
 */
public class GodFather{
     CoreMember coremember;
     
     public void kill(Person someone){
           Killer killer = new Killer();
           killer.kill(someone);
     }
}
Copy the code

 Achieve isolation block, such that the independence between the modules

 

 

 

Third, late binding time - the goal is to control deployment time and allows non-developers to modify.

1, run-time registration (Runtime registration):

2, the configuration file (Configuration files):

3, polymorphism (Polymorphism):

4, replace the component (Component replacement):

5, abide by the agreement (Adherence to defined protocols) have been defined:

 

After a delay of 0.5 seconds to send a network request, first thought of the handler, the results of such an error occurs, the solution is simple, is to call Looper.prepare () in the thread, and then call Looper.loop () on it

Copy the code
private void sendMessageToClient(final StringBuilder s){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
                sendToClient.sendDataToClient (s, clientSocketAddress); // network requests must child thread
            }
        }).start();

    }
Copy the code

Guess you like

Origin www.cnblogs.com/zjm15511858030/p/12464865.html