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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 9-33. Drawing Some Roofs sketch<br />

There is nothing new in this sketch, <strong>and</strong> I now have all the House component classes built.<br />

The next thing to do is put them all together. Again, it is worthwhile considering this problem<br />

in the context of the real world. If you were really building a house, you would be at<br />

the stage at which you have identified your door, window, <strong>and</strong> roof suppliers, <strong>and</strong> have<br />

established an agreed upon set of properties <strong>and</strong> methods. In a sense, you have developed<br />

a contractual agreement with your suppliers. This is precisely how OOP works.<br />

House class<br />

The public properties <strong>and</strong> methods of a class are referred to as the public interface to the<br />

class. If you know the public interface to a class, you don’t even need to know what happens<br />

within the class. The interface, in a sense, enforces the contract. Classes should be as<br />

modular <strong>and</strong> self-contained as possible. Thus, each of the classes takes care of calculating<br />

<strong>and</strong> drawing itself. The House class is no exception. The House class, besides drawing some<br />

type of external structure, will need to orchestrate how the Door, Window, <strong>and</strong> Roof component<br />

classes are used in its own creation. To make this happen, you need to somehow<br />

include these classes within the House class. You also want to be able to customize these<br />

component classes. There is a classic OOP way to solve this problem, which is called composition.<br />

I’ll actually include variables of the respective component types within the House<br />

class. In other words, I’ll include reference variables of the Door, Window, <strong>and</strong> Roof data<br />

types within the House class definition. When I instantiate the House class, I’ll actually pass<br />

in Door, Window, <strong>and</strong> Roof object references as arguments, which, you’ll remember, will<br />

need to be received by parameters of the same data type within a House constructor.<br />

Here’s the House class. Please note that the following code is not a finished sketch yet, but<br />

only the House class. Shortly, I’ll put all the classes (Window, Door, Roof, <strong>and</strong> House)<br />

together into a sketch to draw a little neighborhood.<br />

class House{<br />

//house properties<br />

int x;<br />

int y;<br />

int w;<br />

int h;<br />

SHAPES<br />

391<br />

9

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

Saved successfully!

Ooh no, something went wrong!