原型模式表示克隆一个现有对象而不是创建一个新的对象,并且可以根据需要进行定制

如果创建一个新对象的成本昂贵且资源密集,则应遵循此模式。

原型模式的优点

原型模式的主要优点如下:

  • 它减少了子类化的需要。
  • 它隐藏了创建对象的复杂性。
  • 客户端可以获得新对象而无需知道将是什么类型的对象。
  • 它允许你在运行时添加或删除对象。

原型模式的使用场景

  • 当类在运行时被实例化时。
  • 当创建一个对象的成本昂贵或复杂时。
  • 当你想在应用程序中保持类的数量最少时。
  • 当客户端应用程序需要不了解对象的创建和表示时。

原型模式的 UML

4-1.jpg

  • 我们将创建一个包含 getClone() 方法的 Prototype 接口
  • 然后,我们将创建一个实现 Prototype 接口的具体类 EmployeeRecord,它负责克隆 EmployeeRecord 对象。
  • PrototypeDemo 类将使用这个具体类EmployeeRecord

原型设计模式的示例

让我们看看原型设计模式的示例。

文件:Prototype.java

interface Prototype { 

    public Prototype getClone(); 

} // Prototype 接口结束

文件:EmployeeRecord.java

class EmployeeRecord implements Prototype { 

    private int id; 
    private String name, designation; 
    private double salary; 
    private String address; 
    
    public EmployeeRecord() { 
        System.out.println("  Employee Records of Oracle Corporation "); 
        System.out.println("---------------------------------------------"); 
        System.out.println("Eid" + "\t" + "Ename" + "\t" + "Edesignation" + "\t" + "Esalary" + "\t\t" + "Eaddress"); 
    } 

    public EmployeeRecord(int id, String name, String designation, double salary, String address) { 
        this(); 
        this.id = id; 
        this.name = name; 
        this.designation = designation; 
        this.salary = salary; 
        this.address = address; 
    } 

    public void showRecord() { 
        System.out.println(id + "\t" + name + "\t" + designation + "\t" + salary + "\t" + address); 
    } 

    @Override 
    public Prototype getClone() { 
        return new EmployeeRecord(id, name, designation, salary, address); 
    } 
} // EmployeeRecord 类结束

文件:PrototypeDemo.java

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

class PrototypeDemo { 
    public static void main(String[] args) throws IOException { 
         
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
        System.out.print("Enter Employee Id: "); 
        int eid = Integer.parseInt(br.readLine()); 
        System.out.print("\n"); 
         
        System.out.print("Enter Employee Name: "); 
        String ename = br.readLine(); 
        System.out.print("\n"); 
         
        System.out.print("Enter Employee Designation: "); 
        String edesignation = br.readLine(); 
        System.out.print("\n"); 
         
        System.out.print("Enter Employee Address: "); 
        String eaddress = br.readLine(); 
        System.out.print("\n"); 
         
        System.out.print("Enter Employee Salary: "); 
        double esalary = Double.parseDouble(br.readLine()); 
        System.out.print("\n"); 
          
        EmployeeRecord e1 = new EmployeeRecord(eid, ename, edesignation, esalary, eaddress); 
         
        e1.showRecord(); 
        System.out.println("\n"); 
        EmployeeRecord e2 = (EmployeeRecord) e1.getClone(); 
        e2.showRecord(); 
    }   
} // PrototypeDemo 类结束

输出

4-2.jpg

标签: Design Patterns, Design Patterns教程, 设计模式, 软件设计, 结构型模式, 行为型模式, 单例模式, 工厂模式, 观察者模式, 中介者模式, 访问者模式