The Prototype Pattern is a creational design pattern that enables object
cloning, allowing new objects to be created by copying existing ones. This
pattern is
particularly useful when object creation is resource-intensive or when objects
need to be customized without altering their structure.
typePrototype={clone():Prototype;setProperty(property: string):void;logProperty():void;};classConcretePrototype1implementsPrototype{property: string|undefined;// Make property optional
clone():ConcretePrototype1{constcloned=newConcretePrototype1();cloned.property=this.property;// Deep copy of property
returncloned;}setProperty(property: string):void{this.property=property;}logProperty():void{console.log(this.property??"-");}}classConcretePrototype2implementsPrototype{property: string|undefined;clone():ConcretePrototype2{constcloned=newConcretePrototype2();cloned.property=this.property;returncloned;}setProperty(property: string):void{this.property=property;}logProperty():void{console.log(this.property||"-");}}classClient{operation(prototype: Prototype):Prototype{returnprototype.clone();}}constexample={run():void{constclient=newClient();constcp1=newConcretePrototype1();constcp1Prototype=client.operation(cp1);cp1.setProperty("original1");cp1Prototype.setProperty("clone1");cp1.logProperty();cp1Prototype.logProperty();constcp2=newConcretePrototype2();constcp2Prototype=client.operation(cp2);cp2.setProperty("original2");cp2Prototype.setProperty("clone2");cp2.logProperty();cp2Prototype.logProperty();}};example.run();