1. Single case design mode
Function: single instance mode can ensure that there is only one instance of the class applying this mode in the system. That is, an object can only have one instantiated object.
Implementation steps
1. Privatize the construction method of the class so that it cannot instantiate the object outside the class through the new keyword;
2. Declare a unique instantiated object inside the class;
3. Define a static method to return the unique instantiated object;
Singleton pattern classification
1. Starving Han single case design mode
Hungry Chinese singleton design pattern is to instantiate the class first when the class is loaded, regardless of whether the instantiated object of the class is used later.
//1. Hungry Han single case design mode public class Person { //1. Privatize the construction method. External objects cannot be instantiated through new private Person(){ } //2. Define a Person object. Whether you need to get the instantiation of this object or not, you must first create the instantiation of this object, and the only one cannot be changed private static final Person p= new Person(); //3. Define a static method to return this unique object public static Person getInstance(){ return p; } } class Test{ public static void main(String[] args) { Person p1 = Person.getInstance(); Person p2 = Person.getInstance(); Person p3 = Person.getInstance(); System.out.println(p1); //com.cyy.design.Person@6d6f6e28 System.out.println(p2); //com.cyy.design.Person@6d6f6e28 System.out.println(p3); //com.cyy.design.Person@6d6f6e28 System.out.println(p1==p2); //true System.out.println(p1==p3); //true } }
2. Lazy singleton design pattern
The lazy singleton design pattern is to instantiate the object only when the getInstance() method is called. Don't worry about instantiating the object first, and wait until it needs to be used. This is lazy.
public class Person1 { //1. Privatization construction method private Person1(){} //2. Declare unique class variables private static Person1 p; //3. Define a method to create a unique instantiated object when called by the user, and add a synchronization lock to prevent multiple objects from being instantiated when multithreading public static synchronized Person1 getInstance(){ // If the value of the idiom variable p is null, it will be created. If it is not null, it means that the object has been created and can be returned directly if(p == null){ p = new Person1(); } return p; } } class Test1{ public static void main(String[] args) { Person1 p1 = Person1.getInstance(); Person1 p2 = Person1.getInstance(); Person1 p3 = Person1.getInstance(); System.out.println(p1);//com.cyy.design.Person1@6d6f6e28 System.out.println(p2);//com.cyy.design.Person1@6d6f6e28 System.out.println(p3);//com.cyy.design.Person1@6d6f6e28 System.out.println(p1==p2);//true System.out.println(p1==p3);//true } }
2. Multi case design pattern
The multi instance design pattern can ensure that there are a fixed number of instantiated objects in the class applying the pattern in the system. Multi instance classes should create and manage their own instances, and also provide the outside world with methods to obtain instances of this class.
Implementation steps
- Create a class and privatize the construction method so that it cannot instantiate the class object through the new keyword outside the class.
- Define the total number of classes created in the class
- Define the list collection of class instances in the class
- Provide a static code block in the class, and create an instance of the class in the static code block
- Provides static methods to get class instances
public class Person2 { //1. Privatization construction method private Person2(){} //2. Define a collection of objects of this type private static ArrayList<Person2> list = new ArrayList<Person2>(); //2.2. Create an inherent number of instantiated objects in the static code block and store them in the collection static{ for (int i = 0; i < 3; i++) { Person2 p = new Person2(); list.add(p); } } //3. Randomly get an instantiated object from the collection public static Person2 getInstance(){ //Get a set subscript at random Random random = new Random(); int i = random.nextInt(list.size()); return list.get(i); } } class Test2{ public static void main(String[] args) { for (int i = 0; i < 10; i++) { Person2 p = Person2.getInstance(); System.out.println(p); } /* com.cyy.design.Person2@135fbaa4 com.cyy.design.Person2@45ee12a7 com.cyy.design.Person2@330bedb4 com.cyy.design.Person2@45ee12a7 com.cyy.design.Person2@330bedb4 com.cyy.design.Person2@135fbaa4 com.cyy.design.Person2@330bedb4 com.cyy.design.Person2@330bedb4 com.cyy.design.Person2@330bedb4 com.cyy.design.Person2@45ee12a7 */ } }
3. Factory mode
Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern is a creation pattern, which provides the best way to create objects. Previously, when we created class objects, we used the form of new objects. In addition to the new object method, factory mode can also create objects.
//1. Write a Car interface and provide the run method public interface Car { public void run(); } //2. Write a FaLaLi class to implement the Car interface and rewrite the run method class FaLaLi implements Car{ @Override public void run() { System.out.println("Ferrari travels at 500 kilometers per hour........."); } } //3. Write a BenChi class to implement the Car interface class BenChi implements Car{ @Override public void run() { System.out.println("Mercedes Benz travels at 400 kilometers per hour........."); } } //4. Provide a carfactory for the production of automotive objects class CarFactory{ /** * * @param id Vehicle identification * @return */ public Car createCar(String id){ //If you want to produce BenChi, return a BenChi object; if you want to produce Ferrari, return a FaLaLi object; otherwise, return null if("BenChi".equals(id)){ return new BenChi(); }else if("FaLaLi".equals(id)){ return new FaLaLi(); } return null; } } class Test4{ public static void main(String[] args) { CarFactory carFactory = new CarFactory(); Car benChi = carFactory.createCar("BenChi"); benChi.run(); Car faLaLi = carFactory.createCar("FaLaLi"); faLaLi.run(); } }
The existence of factory pattern can change the way of creating class objects and solve the coupling between classes
4. Enumeration [understand]
Enumeration is a reference data type. Enumeration in java is a "special class" with a fixed number of objects. If some classes have a fixed number of objects
Can be defined as enumeration. Such as gender, season, direction.
public class Person3 { private String name; private Sex sex; public Person3() { } public Person3(String name, Sex sex) { this.name = name; this.sex = sex; } @Override public String toString() { return "Person3{" + "name='" + name + '\'' + ", sex=" + sex + '}'; } } enum Sex{ BOY, GIRL;//Male and female } class Test3{ public static void main(String[] args) { Person3 p1 = new Person3("Zhang San", Sex.BOY); Person3 p2 = new Person3("Li Si", Sex.GIRL); Person3 p2 = new Person3("Li Si", "male"); //This will report an error System.out.println(p1.toString()); } }
The essence of enumeration is a class, so there can also be member variables, member methods, etc.
Enumeration is essentially a class. The final effect of the Sex enumeration we just defined is as follows:
enum Sex{ BOY(3), GIRL(6);//Male and female public int age; Sex(int age) { this.age = age; } public void show(){ System.out.println(age); } } class Test3{ public static void main(String[] args) { Person3 p1 = new Person3("Zhang San", Sex.BOY); Person3 p2 = new Person3("Li Si", Sex.GIRL); Sex.BOY.show(); Sex.GIRL.show(); System.out.println(p1.toString()); } }
Operation results: