头图

Table of contents

definition

Create object instances using prototype instances, and then create new objects by copying those prototypes.

In java, the Cloneable interface is mainly implemented through the prototype, and then the clone method is used to copy the prototype instance to the client, so as to achieve the purpose of the prototype mode.

So what's the benefit of doing this? The main thing to create an object is to apply for memory + member variable assignment. In the system, this overhead can be ignored, so it is generally not recommended to use the prototype mode. However, if you need to perform operations such as loading files, encryption and decryption each time an object is created, these operations require a lot of time and resources, and the content loaded each time is similar, then we can consider using the prototype mode at this time.

accomplish

Next, let's experience the implementation of the prototype mode through a class that creates a database connection. First, there is a database configuration file with the configuration of the master and slave nodes. We need to load the relevant configuration file, and then return the connection class instance by copying. to the client.

The configuration file of the master and slave nodes config.properties

 master.ip=localhost
master.port=3306
master.username=root
master.password=123456
master.type=mysql

slave.ip=localhost
slave.port=3307
slave.username=root
slave.password=123456
slave.type=mysql

Connection - The parent class of all database connections implements the main work of the connection class such as loading the main configuration file

MasterConnection & SlaveConnection - Refine the database connection of different nodes and realize prototype replication

 public abstract class Connection implements Cloneable {
    protected static Properties properties;
    static {
        properties = new Properties();
        InputStream in = null;
        try {
             in = new FileInputStream("config.properties");
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                assert in != null;
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String ip;
    private String port;
    private String username;
    private String password;
    private String type;

    public String getIp() {
        return ip;
    }

    void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    void setPort(String port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    void setPassword(String password) {
        this.password = password;
    }

    public String getType() {
        return type;
    }

    void setType(String type) {
        this.type = type;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
 public class MasterConnection extends Connection{
    private static final Connection CONNECTION = new MasterConnection();
    static {
        CONNECTION.setIp(properties.getProperty("master.ip"));
        CONNECTION.setPort(properties.getProperty("master.port"));
        CONNECTION.setUsername(properties.getProperty("master.username"));
        CONNECTION.setPassword(properties.getProperty("master.password"));
        CONNECTION.setType(properties.getProperty("master.type"));
    }

    public static Connection getConnection() throws CloneNotSupportedException {
        return (Connection)CONNECTION.clone();
    }
}
 public class SlaveConnection extends Connection{
    private static final Connection CONNECTION = new SlaveConnection();
    static {
        CONNECTION.setIp(properties.getProperty("slave.ip"));
        CONNECTION.setPort(properties.getProperty("slave.port"));
        CONNECTION.setUsername(properties.getProperty("slave.username"));
        CONNECTION.setPassword(properties.getProperty("slave.password"));
        CONNECTION.setType(properties.getProperty("slave.type"));
    }

    public static Connection getConnection() throws CloneNotSupportedException {
        return (Connection)CONNECTION.clone();
    }
}

test

 public class PrototypeTest {
    @Test
    public void test(){
        try {
            Connection connection = MasterConnection.getConnection();
            System.out.println(connection.getPort());
            connection = SlaveConnection.getConnection();
            System.out.println(connection.getPort());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
=======结果=======
3306
3307

scenes to be used

  • Resource optimization scenario: a large number of external resources need to be initialized when creating objects
  • Complex dependency scenario: Object A that needs to be created depends on object B and object C, and object B depends on object D...
  • Scenarios where the same object will be used by multiple modifiers: a product may be modified for multiple services such as order, logistics, and membership
  • Scenarios that need to save the original state: like the Connection class above, we need to save the configuration loaded from the file

eacape
205 声望8 粉丝

JAVA 攻城狮


« 上一篇
工厂模式
下一篇 »
代理模式