Log in / Register
Home arrow Computer Science arrow Data Structures and Algorithms with Python
< Prev   CONTENTS   Next >

1.11 A Container Class

To further enhance our drawing program we will first create a data structure to hold all of our drawing commands. This is our first example of defining our own class in this text so we'll go slow and provide a lot of detail about what is happening and why. To begin let's figure out what we want to do with this container class.

Our program will begin by creating an empty container. To do this, we'll write a line like this.

graphicsCommands = PyList()

Then, we will want to add graphics commands to our list using an append method like this.

command = GotoCommand(x, y, width, color) graphicsCommands.append(command)

We would also like to be able to iterate over the commands in our list.

for command in graphicsCommands:

# draw each command on the screen using the turtle called t.


At this point, our container class looks a lot like a list. We are defining our own list class to illustrate a first data structure and to motivate discussion of how lists can be implemented efficiently in this and the next chapter.

1.12 Polymorphism

One important concept in Object-Oriented Programming is called polymorphism. The word polymorphic literally means many forms. As this concept is applied to computer programming, the idea is that there can be many ways that a particular behavior might be implemented. In relationship to our PyList container class that we are building, the idea is that each type of graphics command will know how to draw itself correctly. For instance, one type of graphics command is the GoToCommand. When a GoToCommand is drawn it draws a line on the screen from the current point to some new (x,y) coordinate. But, when a CircleCommand is drawn, it draws a circle on the screen with a particular radius. This polymorphic behavior can be defined by creating a class and draw method for each different type of behavior. The code in Sect. 1.12.1 is a collection of classes that define the polymorphic behavior of the different graphics draw methods. There is one class for each drawing command that will be processed by the program.

1.12.1 Graphics Command Classes

1 # Each of the command classes below hold information for one of the

2 # types of commands found in a graphics file. For each command there must

3 # be a draw method that is given a turtle and uses the turtle to draw

4 # the object. By having a draw method for each class, we can

5 # polymorphically call the right draw method when traversing a sequence of

6 # these commands. Polymorphism occurs when the "right" draw method gets

7 # called without having to know which graphics command it is being called on.

8 class GoToCommand:

9 # Here the constructor is defined with default values for width and color.

10 # This means we can construct a GoToCommand objects as GoToCommand(10,20),

11 # or GoToCommand(10,20,5), or GoToCommand(10,20,5,"yellow").

12 def init (self,x,y,width=1,color="black"):

13 self.x = x

14 self.y = y

15 self.color = color

16 self.width = width


18 def draw(self,turtle):

19 turtle.width(self.width)

20 turtle.pencolor(self.color)

21 turtle.goto(self.x,self.y)


23 class CircleCommand:

24 def init (self,radius,,color="black"):

25 self.radius = radius

26 self.width = width

27 self.color = color


29 def draw(self,turtle):

30 turtle.width(self.width)

31 turtle.pencolor(self.color)



34 class BeginFillCommand:

35 def init (self,color):

36 self.color = color


38 def draw(self,turtle):

39 turtle.fillcolor(self.color)

40 turtle.begin_fill()


42 class EndFillCommand:

43 def init (self):

44 # pass is a statement placeholder and does nothing. We have nothing

45 # to initialize in this class because all we want is the polymorphic

46 # behavior of the draw method.

47 pass


49 def draw(self,turtle):

50 turtle.end_fill()


52 class PenUpCommand:

53 def init (self):

54 pass


56 def draw(self,turtle):

57 turtle.penup()


59 class PenDownCommand:

60 def init (self):

61 pass


63 def draw(self,turtle):

64 turtle.pendown()

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science