"Prototype Pattern" of Design Patterns

What is Prototype Pattern

The Prototype pattern is a creational design pattern that allows you to use existing objects to create new objects without having to write code from scratch. In Java, the prototype pattern can implement the Cloneable interface to copy objects. The prototype pattern delegates the responsibility of cloning objects to the prototype object, thus avoiding reinitializing objects every time they are created like a factory method.

Why use the prototype pattern

Prototype pattern can help us avoid writing code from scratch, thus saving time and resources. By using existing objects to create new objects, the prototype pattern can provide better performance and less memory consumption. Also, Prototype pattern can provide us a more flexible way to create objects as it can dynamically create instances as needed.

Where do you use it at work

In Android development, the prototype pattern is often used to create new Activity or Fragment instances. For example, we can create a basic Activity or Fragment and define some necessary properties and methods in it. Then, we can use this base instance as a prototype to create a new instance by copying the properties and methods of the prototype instance, thus saving the workload of creating a new instance.
For example, suppose we need to create multiple Fragment instances with similar functions and UI styles in the application, we can create a basic Fragment instance and define it as a prototype. Then, we can use the prototype pattern to copy this base instance and modify some properties and methods as needed to create a new Fragment instance.

Design ideas

The design idea of ​​the prototype pattern is to create a prototype object, and then create other objects by cloning it. The prototype object implements the Cloneable interface so that we can use its clone() method to create new objects. In Java, the prototype pattern is usually implemented using the clone() method of the Object class.

Code Implementation of the Prototype Pattern

We will use a simple example to demonstrate the implementation of the prototype pattern. We will create a User class which contains id and name properties. We will create a prototype object and then use it to create other user objects. Here is a code example of a complete Java class:

public class User implements Cloneable {
    
    
  private int id;
  private String name;

  public User(int id, String name) {
    
    
    this.id = id;
    this.name = name;
  }

  public int getId() {
    
    
    return id;
  }

  public void setId(int id) {
    
    
    this.id = id;
  }

  public String getName() {
    
    
    return name;
  }

  public void setName(String name) {
    
    
    this.name = name;
  }

  @Override
  protected Object clone() throws CloneNotSupportedException {
    
    
    return super.clone();
  }
}

In the above code, we have implemented the Cloneable interface and overridden the clone() method to implement the prototype pattern. Now, we will use this prototype object to create other objects:

public class PrototypeApp {
    
    
  public static void main(String[] args) throws CloneNotSupportedException {
    
    
    User prototype = new User(1, "John");

    User user1 = (User) prototype.clone();
    user1.setId(2);

    User user2 = (User) prototype.clone();
    user2.setId(3);

    System.out.println("Prototype User: " + prototype.getId() + " - " + prototype.getName());
    System.out.println("User 1: " + user1.getId() + " - " + user1.getName());
    System.out.println("User 2: " + user2.getId() + " - " + user2.getName());
  }
}

In the above code, we created a prototype object and used it to create the User object. We use the clone() method to clone the prototype object and set the new id property. Then, we print the id and name attributes of each user object. The output is as follows:

Prototype User: 1 - John
User 1: 2 - John
User 2: 3 - John

Summarize

Prototype pattern is a very useful creational design pattern, it can help us avoid writing code from scratch, thus saving time and resources. By using existing objects to create new objects, the prototype pattern can provide better performance and less memory consumption. Also, Prototype pattern can provide us a more flexible way to create objects as it can dynamically create instances as needed. Therefore, the prototype pattern is a very good solution in cases where objects need to be created repeatedly.

Guess you like

Origin blog.csdn.net/weixin_45112340/article/details/129663094