Object-oriented programming is built on three sturdy pillars: encapsulation, specialization, and polymorphism.
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 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 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." 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.
For example, you might have a public method 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; if a client supplies a dollar amount and a number of years, you must return the net present value. You might implement that initially by keeping a table of values. You might change that at a later time to compute the value using the appropriate algebra. That is your business, and it does not affect the client. As long as you don't change the public interface (i.e., 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 while you change the implementation.
The second pillar, specialization, is implemented in C# by declaring that a new class derives from an existing class. When you do so, 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.)
Specialization allows you to create a family of objects. In Windows a button is a control. A list box 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, you do not have to update code in every derived type; they inherit the changes.
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.
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 type, for example a collection of Controls, but you do not know (or care) what the specific subtype each of your controls is (one may be a button, another a list box, etc.). The important thing is that you know they all inherit shared abilities (e.g., 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, this is implemented properly on a per-control basis (i.e., 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, list boxes, etc.) 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.
Object-Oriented Analysis and Design:
Analysis :Analysisis the process of understanding and detailing the problem you are trying to solve.
Design :Design is the actual planning of your solution.
With trivial problems (such as computing the Fibonacci series[1]), you may not need an extensive analysis period, but with complex business problems the analysis process can take weeks, or even months. One powerful analysis technique is to create what are called use-case scenarios, in which you describe in some detail how the system will be used. Among the other considerations in the analysis period are determining your success factors (how do you know if your program works) and writing a specification of your program's requirements.
[1] The Fibonacci series is the values 0,1,1,2,3,5,8,13.... The series is named for Fibonacci, who in 1202 investigated how fast rabbits could breed in ideal circumstances. The series works by adding the previous two numbers to get the next (thus 8 is the sum of 5+3).
Once you've analyzed the problem, you design the solution. Imagining the classes you will use and their inter-relationships is key to the design process. You might design a simple program on the fly, without this careful planning; but in any serious business application, you will want to take some time to think through the issues.
There are many powerful design techniques, you might use. One interesting controversy that has arisen recently is between traditional object-oriented design on the one hand[2] and eXtreme programming on the other.
related links :
VISUAL STUDIO INTRODUCTIONC SHARP INTRODUCTION
C SHARP OUT LOOK
DOT NET AND C SHARP
C SHARP APPLICATION STRICTURE
OOPS INTRODUCTION
OOPS AND C SHARP
IDE AND C SHARP
INSTANTIATING OBJECTS IN C SHARP
CLASSES AND OBJECTS IN C SHARP
OPERATORS IN C SHARP
SWITCH AND ITERATION IN C SHARP
BRANCHING IN C SHARP
CONSTANTS AND STRING
STATIC AND INSTANCE MEMBERS IN DOT NET
No comments:
Post a Comment