Visual Basic Design Patterns VB 6.0 and VB.NET

James W. Cooper




This is a practical tutorial to writing Visual Basic (VB6 and VB.NET) programs using some of the most common design patterns. This book also provides a convenient way for VB6 programmers to migrate to VB.NET and use its more powerful object-oriented features. Organized as a series of short chapters that each describe a design pattern, Visual Basic Design Patterns provides one or more complete working visual examples of programs using that pattern, along with UML diagrams illustrating how the classes interact. Each example is a visual program that students can run and study on the companion CD making the pattern as concrete as possible.

Table of Contents



1. What Are Design Patterns?

Defining Design Patterns.
The Learning Process.
Studying Design Patterns.
Notes on Object Oriented Approaches.
VB Design Patterns.
How this Book is Organized.

2. UML Diagrams.

WithClass UML Diagrams.
Visual Basic Project Files.

3. Using Classes and Objects in VB.

A Simple Temperature Conversion Program.
Building a Temperature Class.
Converting to Kelvin.

Putting the Decisions into the Temperature Class.
Using Classes for Format and Value Conversion.
Handling Unreasonable Values.

A String Tokenizer Class.
Classes as Objects.
Class Containment.

Class Initialization.
Classes and Properties.
Another Interface Example—The Voltmeter.
A vbFile Class.
Programming Style in Visual Basic.

4. Object-Oriented Programming.

Building VB Objects.
Creating Instances of Objects.
A VB Measurement Program.
Methods Inside Objects.
Passing Arguments by Reference and by Value.
Object Oriented Jargon.

5. Building Your Own VB Control.

A Highlighted Text Field.
Resizing a User Control.

Testing Your HiText Controls.
Adding Properties and Methods to User Controls.
Compiling a User Control.
Program on the CD-ROM.

6. Inheritance and Interfaces.

An Investment Simulator.
Writing the Simulator.
Indicators for Using an Interface.
Reusing Common Methods.
Hidden Interfaces.
Programs on the CD-ROM.

7. Introduction to VB.NET.

Syntax Differences in VB.NET.
Improved Function Syntax.

Variable Declarations and Scoping.
Objects in VB.NET.

Compiler Options.
Numbers in VB.NET.

Properties in VB6 and VB.NET.
Shorthand Equals Syntax.
Managed Languages and Garbage Collection.
Classes in VB.NET.
Building a VB7 Application.
The Simplest Window Program in VB.NET.
Drawing and Graphics in VB.NET.
Tooltips and Cursors.
Creating a Square from a Rectangle.

Public, Private, and Protected.
Overriding Methods in Derived Classes.
Overloading and Shadowing.
Overriding Windows Controls.
Programs on the CD-ROM.

8. Arrays, Files and Exceptions in VB.Net.

Collection Objects.

Multiple Exceptions.
Throwing Exceptions.
File Handling.
The File Object.
Reading a Text File.
Writing a Text File.

Exceptions in File Handling.
Testing for End of File.
The FileInfo Class.
A vbFile Class.
Programs on the CD-ROM.


9. The Simple Factory Pattern.

How a Simple Factory Works.
Sample Code.
The Two Derived Classe.
Building the Simple Factory.
Using the Factory.

Writing the Factory Pattern in VB.NET.
Factory Patterns in Math Computation.
Programs on the CD-ROM.

10. The Factory Method.

The Swimmer class.
The Events Classes.
Straight Seeding.
Circle Seeding.

Our Seeding Program.
Other Factories.
The Seeding Program in VB7.
When to Use a Factory Method.
Programs on the CD-ROM.

11. The Abstract Factory Pattern.

A GardenMaker Factory.
How the User Interface Works.
Creating an Abstract Factory Using VB7.
The Picture Box.
Handling the RadioButton and Button Events.

Adding More Classes.
Consequences of Abstract Factory.
Programs on the CD-ROM.

12. The Singleton Pattern.

Creating Singleton Using a Static Method.
Catching the Error.
Providing a Global Point of Access to a Singleton.
The MSComm Control as a Singleton.
Available Ports.

Writing a Singleton in VB.NET.
Using a Private Constructor.
Error Handling in Our Singleton.

A VB.NET SpoolDemo Program.
The Global Point of Access.
Other Consequences of the Singleton Pattern.
Programs on Your CD-ROM.

13. The Builder Pattern.

An Investment Tracker.
Calling the Builders.
The List Box Builder.
The Checkbox Builder.
Writing a Builder in VB.NET.
The Stock Factory.
The CheckChoice Class.
The ListboxChoice Class.

Using the Items Collection in the ListBox Control.
The Final Choice.

Consequences of the Builder Pattern.
Programs on the CD-ROM.

14. The Prototype Pattern.

Cloning in Visual Basic.
Using the Prototype.
Using the Prototype Pattern.
Additional Methods in Subclasses.
Dissimilar Classes with the Same Interface.

Prototype Managers.
Writing a Prototype in VB7.
Consequences of the Prototype Pattern.
Programs on the CD-ROM.
Summary of Creational Patterns.

III. Structural Patterns.

15. The Adapter Pattern.

Moving Data between Lists.
Using the MSFlexGrid.
Using a TreeView.
The Object Adapter.

Two Way Adapters.
Using Adapters in VB7.
TreeView Adapters for VB.NET.
Adapting a DataGrid.
The Class Adapter.
Two-Way Adapters.
Object versus Class Adapters in VB.NET.
Pluggable Adapters.
Adapters in VB.
Programs on the CD-ROM.

16. The Bridge Pattern.

The visList Classes.
The Class Diagram.
Extending the Bridge.
ActiveX Controls as Bridges.
The Bridge Pattern in VB.NET.
The ListBox VisList Class.
The Grid VisList Class.
Loading the Data.

Changing the Data Side of the Bridge.
Consequences of the Bridge Pattern.
Programs on your CD-ROM.

17. The Composite Pattern.

An Implementation of a Composite.
Computing Salaries.
The Employee Classes.
The Subords Class.
The Boss Class.
Building the Employee Tree.
Doubly Linked Lists.
Consequences of the Composite Pattern.
A Simple Composite.
Composites in VB.
The Composite in VB.NET.
The Enumerator.
Multiple Boss Constructors.

Other Implementation Issues.
Programs on the CD-ROM.

18. The Decorator Pattern.

Decorating a CoolButton.
Using a Decorator.
Using ActiveX Controls as Decorators.
A Decorator In VB.NET.
Nonvisual Decorators.
Decorators, Adapters and Composites.
Consequences of the Decorator Pattern.
Programs on the CD-ROM.

19. The Facade Pattern.

What is a Database?
Getting Data out of Databases.
Kinds of Databases.
Microsoft Database Connection Strategies.
Database Structure.
The DBase Class.

Building the Facade Classes.
The Stores Class.

Building the Stores and Foods Tables.
Building the Price Table.
Building the Price Query.

Summary of the Facade Pattern.
ADO Database Access in VB6.
The ADO Connection.
Adding and Seeking Rows Table Rows.
Using the ADO Extensions.

The ADO Dbase Class.
Database Access in VB.NET.
Using ADO.NET.
Connecting to a Database.
Reading Data from a Database Table.
Executing a Query.
Deleting the Contents of a Table.

Adding Rows to Database Tables Using ADO.NET.
Making the VB.NET ADO Facade.
The DBTable class.

Creating Classes for Each Table.
Storing the Prices.
Loading the Database Tables.
The Final Application.
What Constitutes the Facade?
Consequences of the Facade.
Programs on the CD-ROM.

20. The Flyweight Pattern.

Example Code.
The Class Diagram.
Selecting A Folder.

Writing a Flyweight Folder in VB.NET.
Flyweight Uses in VB.
Sharable Objects.
Copy-on-Write Objects.
Programs on the CD-ROM.

21. The Proxy Pattern.

Sample Code.
Writing a Proxy in VB.Net.
Proxies in VB.
Comparison with Related Patterns.
Programs on the CD-ROM.
Summary of Structural Patterns.


22. Chain of Responsibility.

Sample Code.
The List Boxes.
Programming a Help System.
Receiving the Help Command.

A Chain or a Tree?
Chain of Responsibility in VB.NET.
Kinds of Requests.
Examples in VB.
Consequences of the Chain of Responsibility.
Programs on the CD-ROM.

23. The Command Pattern.

Command Objects.
Building Command Objects.
Arrays of Commands.
Consequences of the Command Pattern.
Providing Undo.
The Command Pattern in VB.NET.
The CommandHolder Interface.
Handling Undo Commands in VB.NET.
The Command Pattern in the VB Language.
Programs on the CD-ROM.

24. The Interpreter Pattern.

A Simple Report Example.
Interpreting the Language.
Objects Used in Parsing.
Reducing the Parsed Stack.
Implementing the Interpreter Pattern.
The Syntax Tree.

Building an Interpreter in VB.
The Parse Objects.
Consequences of the Interpreter Pattern.
Programs on the CD-ROM.

25. The Iterator Pattern.

Sample VB6 Code.
Fetching an Iterator.

Filtered Iterators.
The Filtered Iterator.

Iterators in VB.NET.
Consequences of the Iterator Pattern.
Programs on the CD-ROM.

26. The Mediator Pattern.

An Example System.
Interactions between Controls.
Sample Code.
Initialization of the System.

Mediators and Command Objects.
The Mediator in VB.Net.
Handling the Events for the New Controls.

Consequences of the Mediator Pattern.
Single Interface Mediators.
Implementation Issues.
Programs on the CD-ROM.

27. The Memento Pattern.

Sample Code.
A Cautionary Note.

Command Objects in the User Interface.
Handling Mouse and Paint Events.
Writing a Memento in VB.NET.
Consequences of the Memento.
Programs on the CD-ROM.

28. The Observer Pattern.

Watching Colors Change.
Writing an Observer in VB.NET.
The Message to the Media.
Consequences of the Observer Pattern.
Programs on the CD-ROM.

29. The State Pattern.

Sample Code.
Switching Between States.
How the Mediator Interacts with the State Manager.
Handling the Fill State.
Handling the Undo List.
Filling Circles in VB.

A State Pattern in VB.NET.
Mediators and the God Class.
Consequences of the State Pattern.
State Transitions.
Programs on the CD-ROM.

30. The Strategy Pattern.

Sample Code.
The Context.
The Program Commands.
The Line and Bar Graph Strategies.
Drawing Plots in VB.
A Strategy Pattern in VB.NET.
Consequences of the Strategy Pattern.
Programs on the CD-ROM.

31. The Template Method Pattern.

Kinds of Methods in a Template Class.
Sample Code.
Drawing a Standard Triangle.
Drawing an Isosceles Triangle.

The Triangle Drawing Program.
Templates and Callbacks.
Summary and Consequences.
Programs on the CD-ROM.

32. The Visitor Pattern.

When to Use the Visitor Pattern.
Sample Code.
Visiting the Classes.
Visiting Several Classes.
Bosses are Employees Too.
Catch-All Operations with Visitors.
Double Dispatching.
Why Are We Doing This?
Traversing a Series of Classes.
Writing a Visitor in VB.
Consequences of the Visitor Pattern.
Programs on the CD-ROM.