![]() ![]() passing the current object as the constructor's argument. in one go by calling the constructor of the current class and The clone operation returns one of the Shape subclasses. A subclass may call the parent’s cloning method before copying its own field values to the resulting object. In this example, the Prototype pattern lets you produce exact copies of geometric objects, without coupling the code to their classes.Ĭloning a set of objects that belong to a class hierarchy.Īll shape classes follow the same interface, which provides a cloning method. ![]() When you need an object like the one you’ve configured, you just clone a prototype instead of constructing a new object from scratch. Here’s how it works: you create a set of objects, configured in various ways. Pre-built prototypes can be an alternative to subclassing. When your objects have dozens of fields and hundreds of possible configurations, cloning them might serve as an alternative to subclassing. You can even copy private fields because most programming languages let objects access private fields of other objects that belong to the same class.Īn object that supports cloning is called a prototype. The method creates an object of the current class and carries over all of the field values of the old object into the new one. ![]() The implementation of the clone method is very similar in all classes. Usually, such an interface contains just a single clone method. This interface lets you clone an object without coupling your code to the class of that object. The pattern declares a common interface for all objects that support cloning. The Prototype pattern delegates the cloning process to the actual objects that are being cloned. ![]()
0 Comments
Leave a Reply. |