Section 6.5. The Three Pillars of Object-Oriented Programming


6.5. The Three Pillars of Object-Oriented Programming

Object-oriented programming is built on three pillars: encapsulation , specialization , and polymorphism .

Each class should be fully encapsulated; that is, it should fully define the state and responsibilities of that type. Specialization allows you to establish hierarchical relationships among your classes. Polymorphism allows you to treat a group of hierarchically related objects in a similar way and have the objects sort out how to implement the programming instructions.

6.5.1. Encapsulation

The first pillar of object-oriented programming is encapsulation. The idea behind encapsulation is that you want to keep each type or class discreet and self-contained, so that you can change the implementation of one class without affecting any other class.

A class that provides a method that other classes can use is called a server . A class that uses that method is called a client . Encapsulation allows you to change the details of how a server does its work without breaking anything in the implementation of the client.

This is accomplished by drawing a bright and shining line between the public interface of a class and its private implementation . The public interface is a contract issued by your class that consists of two parts . The first part says, "I promise to be able to do this work." Specifically , you'll see that a public interface says, "call this method, with these parameters, and I'll do this work, and return this value." The second part says "You are allowed to access these values (and no others)." C# implements this second part of the interface through properties (discussed in Chapter 7).

A client can rely on a public interface not to change. If the public interface does change, then the client must be recompiled and perhaps redesigned.

On the other hand, the private implementation is, as its name implies, private to the server. The designer of the server class is free to change how it does the work promised in the public interface, so long as it continues to fulfill the terms of its implicit contract: it must take the given parameters, do the promised work, and return the promised value and allow access to the public properties.

For example, you might have a public method NetPresentValue( ) that promises as follows : "Give me a dollar amount and a number of years, and I'll return the net present value." How you compute that amount is your business; as long as you return the net present value given a dollar amount and number of years , the client doesn't care if you look it up in a table, compute the value, or ask your friend who is really good at math.

You might implement your Net Present Value interface initially by keeping a table of values. Some time later, you might change your program to compute the net present value using the appropriate algebra. That is encapsulated within your class, and it does not affect the client. As long as you don't change the public interface (that is, as long as you don't change the number or type of parameters expected, or change the type of the return value), your clients will not break when you change the implementation.

6.5.2. Specialization

The second pillar of object-oriented programming , specialization , is implemented in C# through inheritance ; specifically by declaring that a new class derives from an existing class. The specialized class inherits the characteristics of the more general class. The specialized class is called a derived class, while the more general class is known as a base class.

The specialization relationship is referred to as the is-a relationship. A dog is a mammal; a car is a vehicle. (Dog would be derived from the base class Mammal and Car from the base class Vehicle.)

For example, a Manager is a special type of Employee. The Manager adds new capabilities (hiring, firing, rewarding , praising) and a new state (annual objectives, management level, etc.). The Manager, however, also inherits the characteristics and capabilities common to all Employees. Thus, a Manager has an address, a name, and an employee ID, and Managers can be given raises, can be laid off, and so forth.

Specialization allows you to create a family of objects. In Windows, a button is a control. A listbox is a control. Controls have certain characteristics ( color , size , location) and certain abilities (can be drawn, can be selected). These characteristics and abilities are inherited by all of their derived types, which allows for a very powerful form of reuse. Rather than cutting and pasting code from one type to another, the derived type inherits the shared fields and methods . If you change how a shared ability is implemented in the base class, you do not have to update code in every derived type; they inherit the changes.

You'll see specialization at work in Chapter 11.

6.5.3. Polymorphism

Polymorphism , the third pillar of object-oriented programming , is closely related to inheritance. The prefix poly means "many;" morph means "form." Thus, polymorphism refers to the ability of a single type or class to take many forms.

There are times that you will know you have a collection of a general typefor example, a collection of controlsbut you do not know (or care) what the specific subtype each of your controls is (one may be a button, another a listbox). The important thing is that you know they all inherit shared abilities (such as the draw method) and that you can treat them all as controls. If you write a programming instruction that tells each control to draw itself, the Draw( ) method is implemented properly on a per-control basis (buttons draw as buttons , listboxes draw as listboxes). You do not need to know how each subtype accomplishes this; you only need to know that each type is defined to be able to draw.

Polymorphism allows you to treat a collection of disparate derived types (buttons, listboxes) as a group. You treat the general group of controls the same way, and each individual control does the right thing according to its specific type. Chapter 11 provides details and examples.



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net