interface Prototype {
Prototype clone();
}
class Person implements Prototype {
private String name;
private int age;
private String address;
public Person(
String name, int age, String address
) {
this.name = name;
this.age = age;
this.address = address;
}
public Person(Person other) {
this.name = other.name;
this.age = other.age;
this.address = other.address;
}
@Override
public Person clone() {
return new Person(this);
}
public void setAddress(String newAddress) {
this.address = newAddress;
}
public void displayInfo() {
System.out.println(
"Name: " + name + ", Age: " + age
+ ", Address: " + address);
}
}
public class Main {
public static void main(String[] args) {
Person original = new Person("John", 30, "123 Main St");
original.displayInfo();
Person cloned = original.clone();
cloned.setAddress("456 Clone St");
System.out.println("\\nAfter cloning and modifying the clone:");
original.displayInfo();
cloned.displayInfo();
}
}
Name: John, Age: 30, Address: 123 Main St
After cloning and modifying the clone:
Name: John, Age: 30, Address: 123 Main St
Name: John, Age: 30, Address: 456 Clone St
// Simple Prototype interface
interface Prototype {
Prototype clone();
}
// Document interface extending Prototype
interface Document extends Prototype {
void setContent(String content);
String getContent();
}
// Concrete document class
class TextDocument implements Document {
private String content;
public TextDocument(String content) {
this.content = content;
}
@Override
public Document clone() {
return new TextDocument(this.content);
}
@Override
public void setContent(String content) {
this.content = content;
}
@Override
public String getContent() {
return content;
}
}
import java.util.HashMap;
import java.util.Map;
// Template manager
class DocumentTemplateManager {
private static final Map<String, Document> templates
= new HashMap<>();
public static void addTemplate(String name, Document doc) {
templates.put(name, doc);
}
public static Document createDocument(String templateName) {
Document template = templates.get(templateName);
if (template == null) {
throw new IllegalArgumentException(
"Template not found: " + templateName);
}
return (Document) template.clone();
}
}
// Usage example
public class Main {
public static void main(String[] args) {
DocumentTemplateManager.addTemplate(
"welcome",
new TextDocument("Welcome, {name}!"));
DocumentTemplateManager.addTemplate(
"meeting",
new TextDocument(
"Meeting scheduled on {date} at {time}"));
Document welcomeDoc = DocumentTemplateManager
.createDocument("welcome");
welcomeDoc.setContent(
welcomeDoc
.getContent()
.replace("{name}", "John Doe"));
System.out.println(
"Welcome document: " + welcomeDoc.getContent());
Document meetingDoc = DocumentTemplateManager
.createDocument("meeting");
meetingDoc.setContent(meetingDoc.getContent()
.replace("{date}", "2024-10-01")
.replace("{time}", "14:00"));
System.out.println(
"Meeting document: " + meetingDoc.getContent());
}
}
Welcome document: Welcome, John Doe!
Meeting document: Meeting scheduled on 2024-10-01 at 14:00