Abstraction

Abstraction is a way of dealing with things in a general way as much as possible, separate from specific details, implementations and instances.

According to wikipedia:

Abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time.

In this blog post, I will show to you simple ways how to achieve abstraction. Some of you implemented this concept but you’re unaware of it.

Abstraction via Functions

  • Processes can be grouped into functions
  • One function can call other function
  • You don’t have to understand each line, just what the function does
  • You can hide data to be accessible only within the function – local variables

double areaOfCircle(int radius) {

	return PI * radius * radius;

}

boolean isFirstBiggerThanSecond(int radius1, int radius2) {
	double area1, area2;
	area1 = areaOfCircle(radius1);
	area2 = areaOfCircle(radius2);
	return area1 > area2;
}

The areaOfCircle() method computes the area of the circle. The isFirstBiggerThanSecond() method compares the size of the circle if the first circle is bigger than the second one given the radius of the two. The isFirstBiggerThanSecond() called the areaOfCircle(). The one who writes the code of the isFirstBiggerThanSecond() method does not need to know how the implementation of the areaOfCircle() method. What he need to do is just to know what the areaOfCircle() does. The implementation on how to compute the area of the circle is separated and factored out so that who will ever use that method does not need to know how the computation is done, only what the function does.

Abstraction via Objects

Combining complex data types and the functions that operates on them give you objects.

public class Rectangle {
    private int length;
    private int width;

    public Rectangle (int length, int width) {
        this.length = length;
        this.width width;
    }

    public int area() {
        return length * width;
    }

    public boolean isBiggerThan(Rectangle other) {
        return this.area() > other.area();
    }   
}

Rectangle rect1 = new Rectangle(3, 4);
Rectangle rect2 = new Rectangle(5, 6);

if (rect1.isBiggerThan(rect2)) {
    buyPaint(rect2.area());
} else {
    buyPaint(rect1.area());
}

Objects are mini-programs, each with its own data and operations.

In Unix, we have a lot of little programs (grep, ls, cat…) that do just one thing. We can combine the little programs into bigger programs by having the output of one become the input of another.

OOP is the same. We make small, simple, specialized programs, and then make bigger programs by combining little programs (“Composition”).

“Think of an OO system as a bunch of intelligent animals (the objects) inside your machine, talking to each other by sending messages to one another.” – Allen Holub

OOP is a way to make programs easier to understand, by allowing bigger programs to be composed of smaller programs, and those smaller programs to be composed of even smaller programs, ad infinitum.

Those are only simple concepts that relates to abstraction. I found an interesting article which discusses abstraction using interface and abstract class in java.

Object Oriented Design Principles

OOP Design Principles

In this blog post, I’ll share to you different object oriented design principles that every programmer or software developer has to master. There are already so many articles about this on the internet but I’ll try to explain it in my own way to expand my understanding about this topic.

Design Principles is an essential way of designing or writing code to make that code more maintainable, flexible or extensible.

These design principles are proven and tested by the great programmers and authors who has many years of experience in software development. It is good when you know these design principles to avoid problems when maintaining your code.

I created a list below.

For each design principle, I will create a blog posts explaining the design principle and provide a code that violates that principle and code that conforms with that principle. And here, I will update the link for each principle. I cannot publish all design principle for one day, but I intend to published it all this month.

If you want to keep updated with my posts, don’t forget to subscribe.