Learn martial arts, how important the mind is. For example, those who practice wrong mind method, such as Uncle Ouyang Feng, wrong mind method, went astray, died on the top of Huashan mountain. Also some must first from the palace of martial arts, perhaps the back of a page on the previous page is not needed. So the six principles, we also understand the heart, to practice good martial arts ah.

Let’s start with an overview of the six principles of design patterns.

  • 1, the open closed principle | open for extension, but closed for modification.

  • 2, the principle of substitution on the Richter scale | subclasses can do instead of the base class.

  • 3, dependency inversion principle | for interface programming, relies on the abstract rather than concrete.

  • 4, the interface segregation principle | use multiple isolated interface, it is better than using a single interface. (Reduce coupling between classes)

  • 5, Demeter (at least know principle) | an entity shall, as far as possible to interact with other entities.

  • 6 and the responsibilities of the single | a class should have only one reason of the change of the it, a duty.

Of course, this is still too abstract, the following will be divided into 4 days (4) small comics + code to explain in detail ~

The first day


Last link: Design patterns start


public class Animal {
   public void eat(a) {
       System.out.println("eat something");
   }
   public void sleep(a) {
 System.out.println("sleeping ~~");  } } Copy the code

public void eat (String kind) {
    if (kind.equals("rabbit")) {
        System.out.println("eat some carmine");
    }
    if (kind.equals("tigger")) {
 System.out.println("eat some meat");  } } Copy the code


public class Rabbit extends Animal {
   public void eat(a) {
       System.out.println("eat some carmine");  
   }
}
Copy the code


public class Cooker {
   public void cook(Potato potato) {
       System.out.println("Let's start cooking.");
       System.out.println(potato.getFood());
   }
}  class Potato {  public String getFood(a) {  return "Hot and sour potato shreds";  } } Copy the code

public interface FoodMaterial {
   String getFood(a);
}

class Potato implements FoodMaterial{
 @Override  public String getFood(a) {  return "Hot and sour potato shreds";  } }  class Chicken implements FoodMaterial{  @Override  public String getFood(a) {  return "Coke Chicken Wings";  } }  public class Cooker {   public void cook(FoodMaterial foodMaterial) {  System.out.println("Let's start cooking.");  System.out.println(foodMaterial.getFood());  }  public static void main (String []args) {  Cooker cooker = new Cooker();  cooker.cook(new Potato());  } } Copy the code

The second day



public interface EmployService {

   void sign (a); / / clock
   void knockCode(a);/ / type code
   void recruit(a); / / recruitment
 void relationShip(a); // Employee relations  void performanceEvaluation(a); // Performance management  void procedureDesign(a); // Program design  void makeBugs(a); / / ha ha ha  }  public class HrServiceImpl implements EmployService{  @Override  public void sign(a) {  System.out.println("Clock");  }  @Override  public void knockCode(a) {   }  @Override  public void recruit(a) {  System.out.println("Recruiting");  }  @Override  public void relationShip(a) {  System.out.println("Employee Relations");  }  @Override  public void performanceEvaluation(a) {  System.out.println("Performance Appraisal");  }  @Override  public void procedureDesign(a) {   }  @Override  public void makeBugs(a) {   } }  public class ProgrammerServiceImpl implements EmployService {   @Override  public void sign(a) {  System.out.println("Clock");  }  @Override  public void knockCode(a) {  System.out.println("Knock code");  }  @Override  public void recruit(a) {   }  @Override  public void relationShip(a) {   }  @Override  public void performanceEvaluation(a) {   }  @Override  public void procedureDesign(a) {  System.out.println("Programming");  }  @Override  public void makeBugs(a) {  System.out.println("Build some bug");  } }  public class ProgrammerClient {   public void sign (EmployService employService) {  employService.sign();  }   public void knockCode(EmployService employService) {  employService.knockCode();  }   public void procedureDesign(EmployService employService) {  employService.procedureDesign();  }   public void makeBugs(EmployService employService) {  employService.makeBugs();  } }   public class HrClient {   public void sign (EmployService employService) {  employService.sign();  }   public void performanceEvaluation(EmployService employService) {  employService.performanceEvaluation();  }   public void recruit(EmployService employService) {  employService.recruit();  }   public void relationShip(EmployService employService) {  employService.relationShip();  } } Copy the code

public interface EmployService {
   void sign(a); / / clock
}

public interface HrService {
 void recruit(a); / / recruitment  void relationShip(a); // Employee relations  void performanceEvaluation(a); // Performance management }  public class HrServiceImpl implements HrService.EmployService{  @Override  public void sign(a) {  System.out.println("Clock");  }  @Override  public void recruit(a) {  System.out.println("Recruiting");  }  @Override  public void relationShip(a) {  System.out.println("Employee Relations");  }  @Override  public void performanceEvaluation(a) {  System.out.println("Performance Management");  } }   public interface ProgrammerService {  void knockCode(a);/ / type code  void procedureDesign(a); // Program design  void makeBugs(a); / / ha ha ha }  public class ProgrammerServiceImpl implements ProgrammerService.EmployService {  @Override  public void sign(a) {  System.out.println("Sign in");  }  @Override  public void knockCode(a) {  System.out.println("Knock code");  }  @Override  public void procedureDesign(a) {  System.out.println("Programming");  }  @Override  public void makeBugs(a) {  System.out.println("Build some bug");  } } Copy the code


On the third day


public interface CarService{
   String getName ();
   void drive ();
   String color();
}
 public class Sedan implements CarService {  private String name;  private String color;  private int speed;   public Sedan(String name, String color,int speed{  this.name = name;  this.color = color;  this.speed = speed;  }   @Override  public String getName() {  return this.name;  }   @Override  public void drive() {  System.out.println("Ordinary car, ordinary driving." + this.speed);  }   @Override  public String color() {  return this.color;  } } Copy the code

public class FastSedan extends Sedan {
   public FastSedan(String name, String color, int speed) {
       super(name, color, speed);
   }

 public void drive(a) {  System.out.println("Go for it, go for it." + this.getSpeed() * 2);  } } Copy the code

public class Boss {
// Publish tasks to the CTO   public void pushJob(CTO cto) {
       List<Programmer> list = new ArrayList<Programmer>();
       for(int i = 0; i < 5; i++) { list.add(new Programmer());  }  cto.assignmentTask(list);  } }  public class CTO { // Assign tasks public void assignmentTask(List<Programmer> programmers) {  for (Programmer programmer:programmers) {  programmer.backPot();  }  } }  public class Programmer { / / pot back public void backPot () {  System.out.println("The programmer carries the pan on his back.");  } }  public class Client {  public static void main (String []args) {  Boss boss = new Boss();  boss.pushJob(new CTO());  } } Copy the code

The last day

Let’s consolidate






The public account “Java Xiaokaxiu” focuses on the Java field, and learns and progresses together with your friends


This article is formatted using MDNICE