Description
Prototype mode is concerned with the creation of a large number of identical objects or similar objects, with the intention of obtaining a new instance by copying an existing instance to avoid the overhead associated with creating such instances repeatedly. The copied instance is the prototype, which is customizable.
Instance:
In the software of graphic design such as Photoshop, the concept of layer is put forward, which makes the operation of design and graphic modification more convenient. Designers can modify and draw the current image object, but also retain other image objects, logic clear, and can be timely feedback. In the example, a layer is the protagonist that describes the prototype pattern.
fromCopyImportCopy, DeepcopyclassSimplelayer:"""design a Layer object, use background to represent the background of RGBA, simple content representation, in addition to direct painting, you can also set the transparency. """background=[0,0,0,0] Content="Blank" defgetcontent (self):returnself.contentdefGetbackground (self):returnSelf.backgrounddefPaint (self,painting): Self.content=paintingdefsetParent (self,p): self.background[3]=PdefFillbackground (self,back): Self.background= BackdefClone (self):returncopy (self)defDeep_clone (self):returndeepcopy (self)if __name__=="__main__": Dog_layer=Simplelayer () dog_layer.paint ("Dog") Dog_layer.fillbackground ([0,0,255, 0]) Print("Background:", Dog_layer.getbackground ())Print("Painting:", Dog_layer.getcontent ()) Another_dog_layer=Dog_layer.clone ()#This is done by copying (clone) to draw an identical dog. Print("Background:", Another_dog_layer.getbackground ())Print("Painting:", Another_dog_layer.getcontent ())
The result of the operation is:
Background: [0, 0, 255, 0]
Painting:dog
Background: [0, 0, 255, 0]
Painting:dog
Depth copy:
A shallow copy copies a reference to the object's contents and its contents, or a reference to a child object, but does not copy the referenced content and the child object itself, whereas a deep copy copies not only the reference to the object and the content, but also the referenced content. Therefore, the general deep copy is more complete than the shallow copy, but it also occupies more resources (including time and space resources).
#浅拷贝
If __name__=="__main__": Dog_layer=Simplelayer () dog_layer.paint ("Dog") Dog_layer.fillbackground ([0,0,255, 0]) Print("Original Background:", Dog_layer.getbackground ())Print("Original Painting:", Dog_layer.getcontent ()) Another_dog_layer=Dog_layer.clone ()#Another_dog_layer=dog_layer.deep_clone ()Another_dog_layer.setparent (128) Another_dog_layer.paint ("Puppy") Print("Original Background:", Dog_layer.getbackground ())Print("Original Painting:", Dog_layer.getcontent ())Print("Copy Background:", Another_dog_layer.getbackground ())Print("Copy Painting:", Another_dog_layer.getcontent ())
Operation Result:
Original Background: [0, 0, 255, 0]
Original Painting:dog
Original Background: [0, 0, 255, 128]
Original Painting:dog
Copy Background: [0, 0, 255, 128]
Copy Painting:puppy
It can be seen that the SetParent () method in a shallow copy is not executed two times in class Another_dog_layer, but is copied directly from the In-Memory Dog_layer (the class Dog_layer).
#Deep Copyif __name__=="__main__": Dog_layer=Simplelayer () dog_layer.paint ("Dog") Dog_layer.fillbackground ([0,0,255, 0]) Print("Original Background:", Dog_layer.getbackground ())Print("Original Painting:", Dog_layer.getcontent ())#Another_dog_layer=dog_layer.clone ()Another_dog_layer=Dog_layer.deep_clone () another_dog_layer.setparent (128) Another_dog_layer.paint ("Puppy") Print("Original Background:", Dog_layer.getbackground ())Print("Original Painting:", Dog_layer.getcontent ())Print("Copy Background:", Another_dog_layer.getbackground ())Print("Copy Painting:", Another_dog_layer.getcontent ())
Execution Result:
Original Background: [0, 0, 255, 0]
Original Painting:dog
Original Background: [0, 0, 255, 0]
Original Painting:dog
Copy Background: [0, 0, 255, 128]
Copy Painting:puppy
It can be seen that all the methods of Another_dog_layer are built on their own when deep copying.
Advantages:
Prototype patterns are used to create complex or time-consuming instances: Copying an Existing instance makes the program run more efficiently. For Factory mode, prototype mode reduces the construction of subclasses.
Use
1. When a system should be independent of the creation, composition, and presentation of his products;
2, when the instantiated class is specified at run time, for example, by dynamic loading;
3, in order to avoid the creation of a product class level parallel to the plant class level;
Disadvantages:
Each product class must be configured with a clone method, and the cloning method needs to consider the functionality of the class as a whole. C
Reference Link: Https://yq.aliyun.com/articles/70451?spm=a2c4e.11155435.0.0.c3e038dajAofdY
python-prototype mode