1 // Base prototype.
2 abstract class Shape is
3 field X: int
4 field Y: int
5 field color: string
6
7 // A regular constructor.
8 constructor Shape() is
9 // ...
10
11 // The prototype constructor. A fresh object is initialized
12 // with values from the existing object.
13 constructor Shape(source: Shape) is
14 this()
15 this.X = source.X
16 this.Y = source.Y
17 this.color = source.color
18
19 // The clone operation returns one of the Shape subclasses.
20 abstract method clone():Shape
21
22
23 // Concrete prototype. The cloning method creates a new object
24 // and passes it to the constructor. Until the constructor is
25 // finished, it has a reference to a fresh clone. Therefore,
26 // nobody has access to a partly-built clone. This keeps the
27 // cloning result consistent.
28 class Rectangle extends Shape is
29 field width: int
30 field height: int
31
32 constructor Rectangle(source: Rectangle) is
33 // A parent constructor call is needed to copy private
34 // fields defined in the parent class.
35 super(source)
36 this.width = source.width
37 this.height = source.height
38
39 method clone():Shape is
40 return new Rectangle(this)
41
42
43 class Circle extends Shape is
44 field radius: int
45
46 constructor Circle(source: Circle) is
47 super(source)
48 this.radius = source.radius
49
50 method clone():Shape is
51 return new Circle(this)
52
53
54 // Somewhere in the client code.
55 class Application is
56 field shapes: array of Shape
57
58 constructor Application() is
59 Circle circle = new Circle()
60 circle.X = 10
61 circle.Y = 10
62 circle.radius = 20
63 shapes.add(circle)
64
65 Circle anotherCircle = circle.clone()
66 shapes.add(anotherCircle)
67 // The `anotherCircle` variable contains an exact copy
68 // of the `circle` object.
69
70 Rectangle rectangle = new Rectangle()
71 rectangle.width = 10
72 rectangle.height = 20
73 shapes.add(rectangle)
74
75 method businessLogic() is
76 // Prototype rocks because it lets you produce a copy of
77 // an object without knowing anything about its type.
78 Array shapesCopy = new Array of Shapes.
79
80 // For instance, we don't know the exact elements in the
81 // shapes array. All we know is that they are all
82 // shapes. But thanks to polymorphism, when we call the
83 // `clone` method on a shape the program checks its real
84 // class and runs the appropriate clone method defined
85 // in that class. That's why we get proper clones
86 // instead of a set of simple Shape objects.
87 foreach (s in shapes) do
88 shapesCopy.add(s.clone())
89
90 // The `shapesCopy` array contains exact copies of the
91 // `shape` array's children.
创建型设计模式Prototype伪码
最新推荐文章于 2024-08-20 11:15:34 发布