Widespread use of personal computers has generated demand for more and more complex applications which contain hundreds of thousands and sometimes millions lines of code. This code needs to be tested, supported, and updated. Once updated, it needs to be tested again which results in new support issues. A modern programmer spends only about 30% of her time developing applications, while the rest of the time is taken by maintenance. Productivity of a single programmer varies wildly from person to person and employers are desperately trying to come up with ways to measure and improve programmer's productivity. Productivity of a single programmer is very low compared to a worker of another profession.
While a milkman can milk dozens of cows during his workday and a doctor can take care of dozens of patients, an average programmer may write 100 or so lines of code, which constitute a negligible fraction of any useful application. So, to simplify and expedite software development, newer programming methodologies are developed to facilitate programming tasks. The idea of an object oriented programming is very simple: Structure your program in logical units called objects and understand two paradigms for object construction: inheritance and polymorphism.
Inheritance: Let a more specialized object inherit from a more general object.
using System;
class Demo
{
public class animal
{
int weight;
string name;
public void show()
{
Console.WriteLine
("{0} has weight
{1}", name, weight);
}
public void
my_set (int k, string z)
{
weight=k;
name=z;
}
}
public class tiger:animal
{
public tiger()
{
my_set(100,"tiger");
show();
}
}
public class lion:animal
{
public lion()
{
my_set(200,"lion");
show();
}
}
public static void Main()
{
tiger Mike = new tiger();
lion Bob = new lion();
}
}
In the program above, classes lion: and tiger are derived from class animal. Class animal is called a base class. Derivation is considered a good idea if a derived class satisfies "is a " relationship with respect to the base class. In our case, tiger and lion are animals, so this relationship is satisfied. Another key concept of Object Oriented Programming is Polymorphism. Polymorphism allows subclasses to mofidy methods of the base class. Polymorphism is widely used because it is often the easiest way to add or update functionality of existing project. Try to give additional examples of Object-Oriented Programming in C#. Good Luck! |
Once you write a few C# applications, you may start to notice that objects you have used form certain patterns, and you could have saved yourself a lot of time by identifying the patterns beforehand and simply copying and pasting code from one project to another. Understanding object patterns also makes you into a more efficient programmer by helping your debug any issues in the pattern that you had happened to use previously. Finally, design patterns simplify your communications with other programmers: Instead of going over thousands of lines of code, you may simply describe what design patterns were implemented in your project. In 1995 Gamma, Helm, Johnson and Vlissides published a book where they classify several dozens design patterns and their relationships. |