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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

318<br />

Finally, when I created the third BurritoRecipe object, I passed in a bunch of stuff:<br />

String[]tpgs = {"tomato", "lettuce", "corn"};<br />

BurritoRecipe b3 = new BurritoRecipe("whole wheat", "pinto", ➥<br />

"beef", tpgs , 8);<br />

The printRecipe() method outputs the following:<br />

Burrito Recipe:<br />

---------------<br />

Steam or lightly pan heat a whole wheat tortilla.<br />

Sauté fresh onions, garlic, cilantro, slowly mixing in pinto beans ➥<br />

<strong>and</strong> white wine.<br />

Grill beef along with fresh green pepper, jalapeno pepper, ➥<br />

chile pepper, onions <strong>and</strong> garlic.<br />

Add tomato.<br />

Add lettuce.<br />

Add corn.<br />

Finish off with a generous spritz of 8 alarm salsa.<br />

Hopefully, this example has given you a clear sense of how OOP is implemented in<br />

<strong>Processing</strong>. However, I don’t think my (albeit very tasty) example properly illustrated the<br />

potential power of OOP. One simple way OOP is useful is in sheer efficiency. It may not<br />

seem worth it to generate a whole BurritoRecipe class just to generate a few burritos, but<br />

what if you needed to generate thous<strong>and</strong>s of different burrito recipes. Clearly, the time<br />

spent creating the class begins to pay off big-time. Another way OOP is useful is in organizing<br />

the coding process. Let’s say I decide to construct an interactive recipe book of 100<br />

favorite dishes from around the world, <strong>and</strong> of course I want to include my burrito recipe.<br />

My little BurritoRecipe class included 5 properties <strong>and</strong> 13 methods, <strong>and</strong> the other 99<br />

world recipes would each contain around the same level of detail. If I don’t use classes, I<br />

might try to code all the hundreds of properties as global <strong>and</strong> local variables. I’d then need<br />

to use lots <strong>and</strong> lots of functions to sort all this stuff out—pretty much a nightmare scenario.<br />

Besides this being a royal pain, I would also lose the connection of each property to its<br />

respective recipe. Instead, if I use classes, each recipe will enclose, or encapsulate, its<br />

respective properties <strong>and</strong> methods, shielding users of the class from much of the internal<br />

procedural complexity <strong>and</strong> organizing all the hundreds of properties into logical conceptual<br />

structures (classes). Remember, OOP was developed based on the real world <strong>and</strong> our<br />

brains’ predilections for thinking of things as discrete units. Going one step further, it<br />

would even be possible to create a RecipeBook class that could encapsulate the individual<br />

recipe objects within the larger organizing class. The bottom line is, as coding problems<br />

get more complex, the benefits gained from OOP begin to far outweigh the costs of implementing<br />

it.

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

Saved successfully!

Ooh no, something went wrong!