Partilhar via


Visual Basic Concepts

Do I Need an Object Model?

You don’t have to create an elaborate object model for your component. A control component (.ocx file) might contain three UserControl objects, and no class modules at all. A code component meant to be used as a simple library of functions might have one global object with a zillion methods.

Again, you might create a code component named Finance with three classes in it, each class representing a self-contained business rule. If the rules are independent of each other, there’s no reason to link them into a hierarchy. A client application that uses these rules simply creates one or more objects of each class, as needed.

Each class module in such a component would have its Instancing property set to MultiUse, so that client applications could create objects from the class, and so that the component could handle multiple objects from each class. Figure 6.5 shows such an object model.

Figure 6.5   A flat object model with several externally creatable objects

Object Models and Interfaces

As the functionality of an object increases, so does the complexity of its interface. This can make the object hard to use.

You can reduce the complexity of an object’s default interface by factoring out groups of related functions, and defining an interface for each group. A client can work with only those interfaces that provide needed features.

By defining standard interfaces in this fashion, you can implement interfaces on other objects, gaining the benefits of polymorphism. This approach to software design in discussed in "Providing Polymorphism by Implementing Interfaces," earlier in this chapter.

Sometimes an object is too large even when its features are factored into separate interfaces. When an object becomes very complex, as for example the TreeView and Toolbar controls, breaking pieces of it off as separate objects may make sense.

Once the whole is divided, you need a way of organizing its constituent parts. Splitting the Node object off from the TreeView control gains you nothing if you can’t show the relationship between them. Object models make it easy to provide this organization to the user of your component.

For More Information   Topics relating to object models are listed in "Organizing Objects: The Object Model." The use of multiple interfaces is covered in "Providing Polymorphism by Implementing Interfaces."