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.

gle), <strong>and</strong> so on. These unique attributes would be defined within the specific Rectangle,<br />

Circle, <strong>and</strong> Triangle classes.<br />

The ability for classes to extend other classes is a powerful feature of OOP, allowing<br />

libraries of classes to be developed for reuse. For example, if I create a simple drawing<br />

program, I could incorporate the existing Rectangle, Circle, <strong>and</strong> Triangle classes just<br />

discussed, rather than rebuilding from scratch. Most programming languages come with<br />

extensive libraries of classes, greatly simplifying the development process. <strong>Processing</strong> takes<br />

this concept one step further by creating a procedural front-end (allowing you to simply<br />

call functions) on top of the classes it relies on. The Java language, which you’ll work with<br />

toward the end of the book, is a great example of a mature object-oriented language with<br />

thous<strong>and</strong>s of prewritten classes at your disposal.<br />

Now these are a lot of new concepts to absorb without any pictures (or at least code snippets),<br />

so let’s get to work with some code. Following is an OOP example, based on the<br />

creation (<strong>and</strong> hopefully consumption) of a Burrito class.<br />

BurritoRecipe class<br />

OBJECT-ORIENTED PROGRAMMING<br />

Classes are often used to model specific objects or processes in the real world. I like burritos<br />

quite a bit—so let’s make a BurritoRecipe class. Since a class is composed of properties<br />

<strong>and</strong> methods, you need to think about what properties or characteristics a burrito<br />

recipe has <strong>and</strong> what methods or actions would be associated with a burrito recipe. I know<br />

this sounds very simplistic, but this really is classic object-oriented thinking.<br />

In OOP, you create an abstraction (the class), internally grouping the class’s properties with<br />

methods that act upon these properties, raising program design issues to a higher level<br />

where you can think like a person (albeit a hungry person), rather than like a machine. For<br />

example, once you create your BurritoRecipe class, you could use it by writing something<br />

like burritoRecipe1 = new BurritoRecipe(). If you wanted to select pinto beans for your<br />

recipe, you could then write something like burritoRecipe1.setBeans("pinto"). Classes<br />

are often also thought of as blueprints. You use the blueprints to make objects (also<br />

known as instances) of the class. For example, the aforementioned burritoRecipe1 is an<br />

object of the BurritoRecipe class. Each object created from a class has access to the same<br />

structure (the class’s internal properties <strong>and</strong> methods), but each object can also choose to<br />

express itself differently from other objects derived from the same class. Thus, I can use<br />

the BurritoRecipe class to make a bean burrito recipe, <strong>and</strong> you can use it to make a<br />

chicken burrito recipe (or better yet, a chicken <strong>and</strong> bean burrito recipe).<br />

Here are some BurritoRecipe properties: size, tortillaFlavor, meatType, beanType,<br />

toppings, salsaTemperature.<br />

Here are some Burrito methods: getSize(), setSize(), getTortillaFlavor(),<br />

setTortillaFlavor(), setMeatType(), getMeatType(), setBeanType(), getBeanType(),<br />

getToppings(), setToppings(), setSalsaTemperature(), getSalsaTemperature(), <strong>and</strong><br />

printRecipe(). It is the convention, where applicable, to create get <strong>and</strong> set methods for<br />

the class’s properties. In the preceding list of methods, all of the methods except<br />

printRecipe() either get or set properties of the class.<br />

303<br />

8

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

Saved successfully!

Ooh no, something went wrong!