1.Singleton mode:
Starving Chinese:
Instantiated direct call:
1 public class Car{ 2 3 private static Car car = new Car(); 4 5 public static Car getCar(){ 6 7 return car 8 9 } 10 11 }
Slacker:
When is it instantiated when used?
1 public class Car{ 2 3 private static Car car; 4 5 public static Car getCar(){ 6 7 if(car == null){ 8 9 car = new Car() 10 11 } 12 13 return car 14 15 } 16 17 }
Slacker: initialization in a static method. Time for space. (no recommendation, time is important)
Starving style: initialize the declared object. Space for time. (recommended, space is not a problem)
2.Factory mode:
The program adds a transition end between the interface and the subclass, through which the subclass instantiation object that implements the common interface can be dynamically obtained.
The sample code is as follows:
1 public interface Student { 2 //Defining an interface 3 public void stady();//Defining a common method 4 } 5 6 7 public class Xiaohong implements Student { 8 //Define a subclass Xiaohong inheritance interface 9 @Override 10 public void stady() { 11 System.out.println("Xiao Hong is learning.); 12 }//override method 13 14 } 15 16 17 public class Xiaoming implements Student { 18 //Define a subclass Xiaoming inheritance interface 19 @Override 20 public void stady() { 21 System.out.println("Xiao Ming is learning.); 22 }//override method 23 24 } 25 26 27 public class Fatory {//Define factory class 28 public static Student getinterface(String Name) { 29 Student s = null;//Defining interface 30 31 if("Xiaoming".equals(Name)) { 32 s = new Xiaoming(); 33 } 34 if("Xiaohong".equals(Name)) { 35 s = new Xiaohong(); 36 } 37 return s; 38 //Judgement and instantiation 39 } 40 } 41 42 43 public class FatoryDamo { 44 public static void main(String[] args) { 45 Student s = null;//Defining interface objects 46 s = Fatory.getinterface(args[0]);//Calling factory method 47 if(s != null) { 48 s.stady();//Invocation method 49 } 50 } 51 }
3.Adapter design pattern:
A general definition of an adapter pattern: a class inherits the adapter to implement the methods we need to implement.
Implementing ideas: By writing an adapter class, all the abstract methods are written in it, but these methods are empty and the adapter class is defined as abstract, which makes no sense if the adapter class can implement itself. The function of the adapter, inheritance adapter, and the need for a new method can be simplified.Do.
public interface Student { void math(); void English(); }//Write an interface ublic class Techer implements Student { //Write a class to inherit the Student interface, but not to rewrite it. @Override public void math() { // TODO Auto-generated method stub } @Override public void English() { // TODO Auto-generated method stub } } public class Xiaoming extends Techer { //Write a class to inherit the Techer class. public void math() { System.out.println("Xiao Ming teaches Maths.); //Rewriting math method } } public class Inclass { public static void main(String[] args) { Xiaoming x = new Xiaoming(); x.math();//Method for instantiating object to call rewrite } }
The general definition of template design pattern is to define an algorithm skeleton and give specific implementation to subclasses.
Implementation: define an abstract method in the class, and implement it from the subclass.
public interface Student { void English(); } public class ClassName implements Student { @Override public void English() { System.out.println("Class is in English.); } }