Design pattern combination pattern:
from abc import ABCMeta,abstractmethod # --------------------- Construct a standard class class Draw(metaclass= ABCMeta): @abstractmethod def draw(self): pass @abstractmethod def add(self,draw): pass @abstractmethod def getchildren(self): pass #Note that this environment is implemented in a two-dimensional environment, other environments can be implemented by themselves # ---------------------Construct a point class Point(Draw): # Point, a point on the plane is represented by a coordinate def __init__ (self, x, y): self.x = x self.y = y def draw(self): print (self) def add(self,draw): #The coordinates of the point are unique raise TypeError def getchildren(self): return [self.x,self.y] def __str__ (self): return ' dot: (%s, %s) ' % (self.x,self.y) # -------------------- Construct a line class Line(Draw): #Line , the line is represented by two points def __init__ (self, point1, point2): self.ponit1 = point1 self.ponit2 = point2 self.k = (self.ponit2.y - self.ponit1.y) / (self.ponit2.x - self.ponit1.x) def draw(self): print(self) def add(self,draw): raise TypeError def getchildren(self): return [self.ponit1,self.ponit2] def __str__(self): return '线:[%s, %s]'%(self.ponit1,self.ponit2) # -------------------- Construct a surface class Surface(Draw): def __init__ (self): self.children = dict() def add(self,draw): self.children[draw] = draw def getchildren(self): return self.children def draw(self): print(self) def __str__(self): return str({str(v) for v in self.children.values() }) #Construct a surface surface = Surface() p1 = Point(1,2) p2 = Point(0,0) p3 = Point(3,2) p4 = Point(2,6) line1 = Line(p1,p2) line2 = Line(p3,p4) surface.add(line1) surface.add(line2) surface.draw() # line1.draw()