1b)対象となる要素:ConcreteElement
Visitor::ConcreteElement では、Visitor::Element で規定されたプロトコルに従いながら、対象とする要素に固有の特性を実現します。
## -------------------- # Visitor::ConcreteElement
class EllipseShape(XShape):
def __init__(self):
self.shape = Ellipse(
Stroke=Brushes.Blue,
StrokeThickness=2,
Width=100,
Height=50,
)
対象とする要素のひとつが、ここで取り上げた Ellipse です。
## -------------------- # Visitor::ConcreteElement
class PolygonShape(XShape):
def __init__(self):
points = PointCollection()
vertices = "0,28 80,28 12,80 40,0 68,80"
for e in vertices.split(" "):
x, y = eval(e)
points.Add(Point(x, y))
self.shape = Polygon(
Stroke=Brushes.Blue,
StrokeThickness=2,
Points=points,
)
対象とする要素のひとつが、ここで取り上げた Polygon です。
《Tips》 この事例では、ひとつのモジュールを扱っていますが、実際の開発では、個別にモジュールを用意します。要求仕様の変更に伴い、新たな対象を扱いたいときには(Open)モジュールを追加するだけです。このとき(Closed)既存のモジュールはその影響を受けません。すると、開放閉鎖原則(OCL: Open-Closed Principle)に沿ったアプローチが可能になります。□