Tuesday 11 January 2011

Basic terminology of software design

Here are the cryptic definitions and what they mean in your code?

Abstraction

A concept or idea not associated with any specific instance. It is an emphasis on the idea, qualities and properties rather than the particulars (a suppression of detail), It is simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
Abstraction can be implemented via Abstract Class/Interface. In both cases, abstract behavior needs to be implemented by the extending/implementing classes. However the catch is we shouldn’t use the implementing class name anywhere in the code except in creation. If the extending/implementing class used as parameter to methods, variable in other classes then note that you are not abstracting it. I will be posting various examples in later, please pay attention to this.

Abstract class x
{
method();
}

Interface x
{
method();
}

Class Y: (extends/implements) x
{
Method()
{
Do something;
}
}

Do not refer Y anywhere in the code except while creation.
Always
X x = new Y();
x.method();

If there is code like below then Y is not abstracted.

Class Z
{
Y y;
Or
OtherMethod(Y y)
Or
Y OtherMethod()
}

Let us continue ...


Composition and Aggregation

Composited (composed) objects are often referred to as having a "has a" or “whole-part” relationship. A real-world example of composition may be seen in an automobile: the objects wheel, steering wheel, seat, gearbox and engine may have no functionality by themselves, but an object called automobile containing all of those objects would serve a higher function, greater than the sum of its parts.
Aggregation differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.

Composition/Aggregation can be implemented by keeping the one object in another. Composition case this object should have valid value in the life cycle of holding object, while in aggregation case this can be null.

Class x
{
Y y;
X()
{
}
X(Y y)
{
X.y =y;
}
}

Class Y
{
}

In the life cycle of X, if y value is valid (not null) then it is composition otherwise it is aggregation.

Generalization

The concept of generalization has broad application in many related disciplines, sometimes having a specialized context-specific meaning.
Of any two related concepts, such as A and B, A is considered a "generalization" of concept B if and only if:
• Every instance of concept B is also an instance of concept A; and
• There are instances of concept A which are not instances of concept B.
Generalization objects are often referred to as having a "is a" relationship.
This can be implemented by class inheritance where both the classes are concrete.

Class X
{
}

Class Y: X (extends)
{
}

Inheritance

In object-oriented programming (OOP), inheritance is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects. In classical inheritance where objects are defined by classes, classes can inherit other classes. The new classes, known as subclasses (or derived classes), inherit attributes and behavior of the pre-existing classes, which are referred to as super classes (or ancestor classes). The inheritance relationship of classes gives rise to a hierarchy. In prototype-based programming, objects can be defined directly from other objects without the need to define any classes.
This can be implemented by extending abstract/concrete class or implementing interface. Same is as above.

Encapsulation

Encapsulation conceals the functional details of a class from objects that send messages to it. The encapsulation is the inclusion within a program object of all the resources need for the object to function - basically, the methods and the data. Encapsulation conceals the functional details of a class from objects that send messages to it. The encapsulation is the inclusion within a program object of all the resources need for the object to function - basically, the methods and the data.
This can be implemented by defining a class and using the variable only by methods inside the class.

Class x
{
private int i;
private string s;
private Y y;

Method()
{
Use y, i and s;
}
}

Using setter/getter methods is a violation of encapsulation. It is common practice to use them to limit no of constrictors in the code.

No comments:

Post a Comment