Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

A small step every day, a big step to success. Hello everyone, I'm GW_gw, and I'm happy to learn daily trivia with you.Copy the code

The following content is from the network, if there is any infringement, please contact me to delete, this article is only used for learning exchange, not for any commercial purposes.

Abstract

This paper mainly introduces the prototype pattern and singleton pattern of creative design pattern.Copy the code

The prototype pattern

Specify the type of object to create with a prototype instance, and create new objects by copying the prototype.

In simple terms, it takes an object as a prototype and creates objects with the same structure as the object.

Specific code:

UML diagrams

Prototype class

/** * @author Gw_gw */ public class AbstractCar implements Cloneable{ String carName; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } @Override public Object clone(){ Object object = null; try{ object = super.clone(); }catch(CloneNotSupportedException e){ System.out.println("AbstractCar is not Cloneable"); } return object; }}Copy the code

Clone classes:

public class BWMCar extends AbstractCar{ public BWMCar(){ setCarName("BWM car"); }}Copy the code
public class BenzCar extends AbstractCar{ public BenzCar() { setCarName("Benz Car"); }}Copy the code

Test

public class PrototypePatternTest { public static void main(String[] args) { AbstractCar bwmCar = new BWMCar(); AbstractCar benzCar = new BenzCar(); System.out.println(bwmCar.getCarName()); System.out.println(benzCar.getCarName()); }}Copy the code

The singleton pattern

  • A class has only one instance, and the entire system is callable.
  • The singleton pattern provides only private constructors, so only instances of this class can be created.
  • Class that defines a static private object of that class
  • Define a static public function that creates or retrieves a static private object from a class.

Specific code:

public class Singleton { /** * Private constructor */ private Singleton() { } /** * Create private static objects */ private static volatile Singleton instance = null; /** * Create public static methods to * access or create private objects */ public static Singleton getInstance(){ // If the object is not instantiated, Synchronized (instance == null){synchronization (Singleton){if(instance == null){instance = new Singleton(); } } } return instance; }}Copy the code

Volatile keyword:

  • This ensures that the variable is visible to different threads when they operate on it. If one thread changes the value of this variable, the other threads immediately see it.
  • Disallow instruction reordering

Synchronized keywords:

  • Modify instance object: Locks the current object instance
  • Modify static methods: Lock the current class.
  • Modifier code block: Specifies the lock object. It can be an object or a class.

conclusion

The above is the prototype pattern and singleton pattern of the creative design pattern, hope to help readers. Readers are welcome to criticize and correct any errors.

You are welcome to click on the portal to learn about other creation modes:

Creating Design Patterns (part 1)

Let’s talk about creative design patterns (PART 2)– Generator patterns