The Observer Pattern

In this chapter we discuss how you can use the Observer pattern to present data in several forms at once. In our new, more sophisticated windowing world, we often would like to display data in more than one form at the same time and have all of the displays reflect any changes in that data. For example, you might represent stock price changes both as a graph and as a table or list box. Each time the price changes, we'd expect both representations to change at once without any action on our...

String Tokenizer Class

A number of languages provide a simple method for taking strings apart into tokens, separated by a specified character. While C does not exactly provide a class for this feature, we can write one quite easily us ing the Split method of the string class. The goal of the Tokenizer class will be to pass in a string and obtain the successive string tokens back one at a time. For example, if we had the simple string our tokenizer should return four tokens The critical part of this class is that it...

Multiple Decorators

Now that we see how a single decorator works, what about multiple decorators It could be that we'd like to decorate our CoolButtons with another decoration say, a diagonal red line. This is only slightly more complicated, because we just need to enclose the CoolDecorator inside yet another decorator panel for more decoration to occur. The only real change is that we not only need the instance of the panel we are wrapping in another, but also the central object here a button being decorated,...

Filtered Iterators

While having a clearly defined method of moving through a collection is helpful, you can also define filtered Iterators that perform some computation on the data before returning it. For example, you could return the data ordered in some particular way or only those objects that match a particular criterion. Then, rather than have a lot of very similar interfaces for these filtered iterators, you simply provide a method that returns each type of enumeration with each one of these enumerations...

Simple Report Example

Let's consider a simplified report generator that can operate on five columns of data in a table and return various reports on these data. Suppose we have the following results from a swimming competition. The five columns are frname, lname, age, club and time. If we consider the complete race results of 51 swimmers, we realize that it might be convenient to sort these results by club, by last name, or by age. Since there are a number of useful reports we could produce from these data in which...

Building the Employee Tree

We start by creating a CEO Employee and then add his subordinates and their subordinates, as follows. prez new Boss CEO, 200000 marketVP new Boss Marketing VP, 100000 salesMgr new Boss Sales Mgr, 50000 advMgr new Boss Advt Mgr, 50000 marketVP.add salesMgr marketVP.add advMgr prodVP new Boss Production VP, 100000 advMgr.add Secy, 20000 add salesmen reporting to sales manager for int i 1 i lt 5 i salesMgr.add Sales i.ToString , rand_sal 30000 prodMgr new Boss Prod Mgr, 40000 shipMgr new Boss Ship...

Garden Maker Factory

Let's consider a practical example where you might want to use the abstract factory in your application. Suppose you are writing a program to plan the layout of gardens. These could be gardens consisting of annuals, vegetables, or perennials. However, no matter which kind of garden you are planning, you want to ask the same questions. 1. What are good border plants 2. What are good center plants 3.What plants do well in partial shade And probably a lot more plant questions that we won't get...

The Vis Rectangle and Vis Circle Classes

We can take some useful advantage of inheritance in designing our visRectangle and visCircle classes. We make visRectangle implement the Drawing interface and then have visCircle inherit from visRectangle. This allows us to reuse the setSelected, setFill, and move methods and the rects properties. In addition, we can split off the drawHandle method and use it in both classes. Our new visRectangle class looks like this. public class VisRectangle Drawing protected int x, y, w, h private const int...

Defining Design Patterns

We all talk about the way we do things in our jobs, hobbies, and home life, and we recognize repeating patterns all the time. Sticky buns are like dinner rolls, but I add brown sugar and nut filling to them. Her front garden is like mine, but I grow astilbe in my garden. This end table is constructed like that one, but in this one, there are doors instead of drawers. We see the same thing in programming when we tell a colleague how we accomplished a tricky bit of programming so he doesn't have...

The Boss Class

Class Diagram Salary Employee

Our Boss class is a subclass of Employee and allows us to store subordinate employees as well. We'll store them in an ArrayList called subordinates and return them through an enumeration. Thus, if a particular Boss has temporarily run out of Employees, the enumeration will just be empty. public Boss string name, float salary base name,salary ------ public override void add string nm, float salary AbstractEmployee emp new Employee nm,salary subordinates.Add emp public override void add...