Extending the Bridge

Now suppose that we need to make some changes in the way these lists display the data. For example, you might want to have the products displayed in alphabetical order. You might think you'd need to either modify or subclass both the list and table classes. This can quickly get to be a maintenance nightmare, especially if more than two such displays eventually are needed. Instead, we simply make the changes in the extended interface class, creating a new sortBridge class similar to the...

State Pattern in VBNET

The State pattern in VB7 is similar to that in VB6. We use the same interfaces for the Memento and Drawing classes. Sub setSelected ByVal b As Boolean Sub draw ByVal g As Graphics Sub move ByVal xpt As Integer, ByVal ypt As Integer Function contains ByVal x As Integer, _ ByVal y As Integer As Boolean Sub setFill ByVal b As Boolean Property rects As vbpatterns.Rectangle End Interface However, there is some advantage in creating a State class with empty methods and overriding only those that a...

Figure 34 Selecting the Class Initialize method A String Tokenizer Class

A number of languages provide a simple method for taking strings apart into tokens, separated by a specified character. While VB does not provide this feature, we can write one quite easily as a 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 holds the initial string and remembers which token is...

The Flyweight Pattern

In this chapter we take up the Flyweight pattern, which is used to avoid the overhead of large numbers of very similar classes. There are cases in programming where it seems that you need to generate a very large number of small class instances to represent data. Sometimes you can greatly reduce the number of different classes that you need to instantiate if you can recognize that the instances are fundamentally the same except for a few parameters. If you can move those variables outside the...

Writing a Prototype in VB7

In VB7, we can write more or less the same code. The major changes are that we will use ArrayLists and zero-based arrays, and that we can write a base SwimData class from which we can inherit a number of useful methods. We create the base SwimData class without a sort method and specify using MustInherit for the class and MustOverride for the method that you must provide an implementation of sort in the child classes. Protected Swimmers As ArrayList Private index As Integer 'constructor to be...

Handling the Undo List

Now, we should be able to undo each of the actions we carry out in this drawing program, and this means that we keep them in an undo list of some kind. The actions we can carry out and undo are 3. Moving a rectangle or circle 4. Filling a rectangle or circle In our discussion of the Memento pattern, we indicated that we would use a Memento object to store the state of the rectangle object and restore its position from that Memento as needed. This is generally true for both rectangles and...