Object Oriented Design

OOPs is one of the most renowned programming paradigm used for development, many programmers prefer OOPS because of code re-usability, the ‘A PIE’ concepts and many more reasons.So what is OOPs?  Before we begin let’s look back and understand where did we come from.

Long time ago in the stone-age of computers, say way back 1930’s the first computer came into existence. So this was after Charles Babbage’s  ‘Difference Machine’ in 1822, the name of the computer was Z1 created by Konrad Zuse , so before Z1 it was mostly assembly language(like a low level programming language with mnemonics ), with Z1 came the high level language. With HLL, rather than dealing with registers, memory addresses and call stacks, high-level languages deal with variables, arrays, objects, complex arithmetic or boolean expressions, subroutines and functions, loops, threads, locks, and other abstract computer science concepts, with a focus on usability over optimal program efficiency. With the advent of HLL came in FORTRAN, COBOL, LISP and many others, these were all Procedural Programming languages.

Procedural Programming languages

Think about a recipe of baking a cake. You should follow certain steps, starting with

  • Pre-heating the oven
  •  Oiling the Baking pan
  • Mixing the Ingredients … and so on

T-S-1625-8-Step-Sequencing-Cards-Making-A-Cake_ver_1.jpg

We need to follow all these step and the outcome is a cake. That is what is Procedural Language.

PPL is derived from structural programming, Procedures also called as routines contains steps to be performed. A Procedural program contains data and of-course  procedures, these procedures operate on data.

Untitled

So if i have to write a piece of code for baking cake how will it look like:

bake_my_cake():

oven_temp = 200

cake = cake_mix + eggs + milk +butter +water

oven.bake()

The bake_my_cake() follows steps one by one and bakes the cake. Si there is this one function holding bulk of information, and any changes will cause consecutive changes in the entire code.

With PPL came its own benefits like, easier to read and maintain. PPL  followed a Top down approach, where problem was divided into sub problems, this is functional decomposition which continues until the sub problem is solved. Maintenance with this approach was an issue, so if there is an edit on the top corresponding low levels need to be changed. Then came OOPs, this approach was near to real world app development.

Writing code for baking a cake with OOPs. Remember OOP’s is not a programming language its a paradigm.

#!/usr/bin/env python
class Oven:
    def __init__(self, timer):
        self.timer = timer
    @staticmethod
    def pre_heat():
        print("Pre-Heating the oven")
    @staticmethod
    def bake():
        print("Baking the cake")

    def set_time(self):
        return self.timer


class Mixer:
    def __init__(self, rounds):
        self.rounds = rounds

    def mix(self):
        print("Mixing content")

    def rounds_mix(self):
        return self.rounds

class Pan:
    def add(self):
        print(" Content added to pan")

#Inheritance
class BakeCake(Oven, Mixer, Pan):


    def __init__(self):
        timer=200
        rounds = 50
        Oven.__init__(self,timer)
        Mixer.__init__(self,rounds)

    def cake_bake(self):
        set_timer = self.set_time()
        print("Timer set to: {} C".format(set_timer))
        self.pre_heat()
        rounds = self.rounds_mix()
        print("Rounds for mixing :{}".format(rounds))
        self.mix()
        self.bake()

#objects
vanilla_cake = BakeCake()
vanilla_cake.cake_bake()

Output:

Timer set to: 200 C
Pre-Heating the oven
Rounds for mixing : 50
Mixing content
Baking the cake

Both the techniques bake the cake but with oops we can do it better, Over, Mixer, Pan are separate classes which I can use for baking Muffins or cookies, I would reuse the same code and maybe override few things and that would work.

Object Oriented Programming is a paradigm, a set of ideas that support many languages.

Advantages

  • Modular structure
  • Reused
  • Easier to maintain
  • Faster development.

In the next post will go deeper with OOP’s 🙂

Github

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s