15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

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

244<br />

Design Patterns<br />

Three examples of different design patterns (and their unusual names)<br />

include<br />

✦ Interface pattern<br />

✦ Flyweight pattern<br />

✦ Memento pattern<br />

An interface pattern defines an object that simplifies access to something<br />

else. For example, suppose someone has written a library of useful subprograms.<br />

Rather than let other programmers access these subprograms<br />

directly, an interface pattern defines an object to provide access to these<br />

subprograms instead. By doing this, an interface pattern keeps your program<br />

focused on using object-oriented features.<br />

So this library might contain subprograms <strong>for</strong> displaying graphics and calculating<br />

mathematical equations. You could use the interface pattern to define<br />

one object <strong>for</strong> accessing the graphics subprograms and a second object <strong>for</strong><br />

accessing the mathematical equations subprograms, as shown in Figure 7-10.<br />

Library of subprograms<br />

Figure 7-10:<br />

An interface<br />

pattern<br />

provides an<br />

objectoriented<br />

interface<br />

to a nonobjectoriented<br />

item, such<br />

as a library<br />

of subprograms.<br />

Object One<br />

Graphics subprograms<br />

Math subprograms<br />

Object Two<br />

Subprogram Graphics 1<br />

Subprogram Graphics 2<br />

Subprogram Graphics 3<br />

Subprogram Graphics 4<br />

Subprogram Math 1<br />

Subprogram Math 2<br />

Subprogram Math 3<br />

A flyweight pattern is used to create multiple objects. For example, you could<br />

create a word processor with every character defined as an object that contains<br />

<strong>for</strong>matting in<strong>for</strong>mation, such as font, font size, underlining, and so on.<br />

However, a typical word processor document would contain thousands of<br />

objects (characters), and because each object gobbles up memory, creating<br />

so many objects would likely swamp the computer’s memory.

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

Saved successfully!

Ooh no, something went wrong!