04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

324<br />

Composition can solve this problem elegantly. Although I can’t say that a window is a type<br />

of house, I can say that a house has a window. This is the difference between inheritance<br />

<strong>and</strong> composition. Inheritance involves an is-a relationship <strong>and</strong> composition involves a<br />

has-a relationship. Which approach would work with the following class groupings?<br />

Mammal, Cat<br />

<strong>Art</strong>ist, <strong>Creative</strong>Coder<br />

Monkey, Tail<br />

Foot, Finger<br />

EatingTool, Spoon, Fork<br />

Well, a cat is a mammal, <strong>and</strong> a creative coder is an artist—these are both examples of<br />

inheritance. A monkey has a tail, which is an example of composition. A foot, however,<br />

doesn’t have a finger; nor is a finger a type of foot—so neither relationship makes sense—<br />

this is an annoying tricky one. Lastly, a spoon <strong>and</strong> a fork are both eating tools, which is<br />

another example of inheritance.<br />

Following is some class code illustrating how to apply the composition relationship.<br />

class Window {<br />

/* declare static properties: the final keyword ensures<br />

that these constant values can't be changed. */<br />

static final int RECTANGLUAR = 1;<br />

static final int ELLIPTICAL = 2;<br />

static final int OCTAGONAL = 3;<br />

// instance properties<br />

int panes;<br />

boolean isDoublePaned;<br />

int configuration;<br />

// constructor<br />

Window (int panes, boolean isDoublePaned, int configuration){<br />

this.panes = panes;<br />

this.isDoublePaned = isDoublePaned;<br />

this.configuration = configuration;<br />

}<br />

// other Window methods would go here<br />

}<br />

class House {<br />

// instance properties<br />

/* The House class contains a property<br />

of type Window */<br />

Window win;<br />

// constructor<br />

/* The House constructor expects

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!