Allows to better understand a concept by breaking it down into a simplified description that ignores unimportant details.
Removes details that are not essential.
Should be relative to some context.
Should/may have some attributes.
Should describe a concept’s basic behaviors.
Abstraction class-diagram representation
2. ENCAPSULATION
Bundle attributes values or data or functions that manipulate those values together into a self-contained object.
Can expose certain data and functions of that object, which can be accessed from other objects.
Can restrict access to certain data and functions to only within that object.
Reduces complexity for the user of the class.
Keeps software modular and easier to work with.
Classes behavior behaves like a black boxes.
Refactoring is easier with proper encapsulation.
Encapsulation class-diagram representation
3. DECOMPOSITION
Taking a whole thing and dividing it up into different parts.
Allows you to further break down problems into pieces that are easier to understand and solve.
Can have static and dynamic parts, for example if consider a car:
static: steering wheel, engine, tires;
dynamic: passengers, luggage.
Can have different lifetime, if consider a car:
closely-related: frame, engine;
not closely-related: tires, fuel.
Sharing.
Has 3 types of relationships:
Association – there is a loose relationship between two objects. These objects may interact with each other for some time.
Aggregation – is a “has a” relationship where a whole has parts that belongs to it. There may be sharing of parts among the wholes in this relationship. From a whole to the parts is considered weak, what this means is although parts can belong to the wholes, they can also exist independently.
Composition – has a strong “has a” relationship. Means is that the whole cannot exist without its parts. If the whole is destroyed then all of its parts are destroyed too. Usually it’s possible to access the parts only through its whole.
Association. Person associated with 0 or more airline objects and airline can be associated with 0 or more persons,
// Association example
public class Student
{
public void Play(Sport sport)
{
//...
}
}
Aggregation. The empty diamond (symbol of aggregation) denotes which object is considered the whole and not the part in the relationship.
// Aggregation example
public class Airliner
{
private ArrayList<CrewMember> crew;
public Airliner()
{
crew = new ArrayList<CrewMember>();
}
public void Add(CrewMember crewMember)
{
//...
}
}
Composition. The House object has 1 or more Room objects. The diamond is filled in which means that has-a relationship is strong (composition). The house is the whole. The 2 related objects cannot exist without each other,
// Composition example
public class Human
{
private Brain brain;
public Human()
{
brain = new Brain();
}
}
4. GENERALIZATION
Helps to reduce the amount of the redundancy when solving problems.
For example its methods, which are generalize behavior.
Generalization piece of code.
Another generalization is inheritance, where can be shared a common characteristics in the superclass. For example: Animal is superclass and cat/dog/squirrel as subclasses (Super class is generalized and subclass is specialized).
Code can be reused which means we do not need to re-implement behaviors that already exists.
D.R.Y. (Don’t Repeat Yourself).
Helps build software that is easier to expand and apply changes.
2 thoughts on “Four object-oriented design principles”