Programming Paradigm : Part 2


Event Driven Programming

When you click on remote button TV channel changes. When some event occurs (pressing button here) action takes place (Channel change). Event driven programming is based on same concept.

Definition
Event driven programming is a programming paradigm in which flow of program is determined by events.

How event driven program works 


  • Central system of event driven system is Scheduler.
  •  Scheduler receives stream of events and it passes each event to relevant event handler.
  • Scheduler remains active until it receives event that will terminate application.
  • Sometimes, a scheduler can come across event which is not handled. Then based on nature of event it will either ignore error or raise exception.
  • Event can be recognized by an event ID or Object ID which is affected by error. Developer can use own strategy too.

Supporting Languages: GUI (Graphical User Interface) based like Javascript support event driven programming such as onClick() event handler for button.


Logic and Constraint Programming
  
Logic programming and constraint programming are 2 programming paradigms.

  • Programs are built up by setting up relations that specify facts and inference rules and asking whether or not something is true. (i.e. specifying goals)


Consider simple prolog example.
Sample clauses
likes (Lily, swimming)
likes (Lily, Singing)
likes (Harry, Lily)
likes (Harry, Swimming)

The following queries yield specified answers.
? - likes (Lily, Swimming) => Yes
?- likes (Harry, Singing) => No
?- likes (Harry, Swimming) => Yes

Supporting language: Prolog

Aspect oriented programming

Sometimes situation comes that there is some code which is repeated in different methods. So you think of creating separate module, but the code (repetitive code) can’t be completely refractor into its own module. For example, logging. To create separate logging module will be foolish idea as it is irrelevant to your actual target application. Such features like logging are called as cross cutting concern, as it’s a behavior that “cuts” across multiple points in your object models, yet distinctly different. As a solution AOP says that encapsulate the cross cutting concern, write them out of main code but not as separate module. Let’s see how.

void mainCode(){
    //some code
}

aspect logging{
    before (mainCode is called):
    {
         log.write(“entering mainCode”);
    }
after (mainCode is called):
    {
         log.write(“exiting mainCode”);
    }
}

Then aspect waiver is used to compile code into this:
void mainCode(){
    log.write(“entering mainCode”);
//some code
}
Supporting language: AspectJ (AOP modification of Java)



Object Oriented Programming

First of all we will try to find what drawbacks in earlier paradigms we studied were. This will be useful while learning OOP concepts in further tutorials.
We studied imperative programming paradigm which proposed concept of writing sequence of statements to specify how to perform a task. As improvement to this approach we saw procedural approach where statements are grouped to form procedures (functions).

What if in college there is a single lecture hall for student from all faculties and simultaneous lectures are going on? It’s not a good idea right? So colleges formed separate departments to make system well organized. Similar applicable to coding, if write all instructions in a single procedure then code will lose its readability. So, program should be made of modules. In C, which is a procedural language, these modules are procedures (functions), where procedures are a sequence of imperative statements such as assignments, loops, sub function calls etc. For example, if we are writing code for dog then out modules will be bark, run etc. Foe each module we will write function as:

void bark(){
//some code
}
void run(){
//some code
}

In procedural programming, you start with a problem (main procedure) and then systematically break down problem into sub-problems (sub-procedures). This is called as functional decomposition, which continues until sub-problem is straight forward enough to be solved by corresponding sub-procedure. This type of approach where developer starts with main problem and then breaks it down to sub-problems is called as top-down approach.

What if changes are made to main procedure? Then these changes may cascade to sub-procedures of main procedure. And then to sub-sub-procedure and so on. Changes may impact all procedures in a pyramid.

Do you know : Top-down and bottom-up design

Top-down design:
  • Mom says I want new refrigerator.
  •  Dad says I want new television.
  • You say I want new laptop.
  • You go to shop and buy all at a time.

Bottom-up design:
  •  Mom says I want new refrigerator.
  • Dad says I want new television.
  • You say I want new laptop.
  •  But budget constraint. So you buy things one by one.

In software industry, developer think bottom up, worrying about which language, platform will be ideal. 


This gives rise to birth of object oriented programming. In OOP, main modules in programming are classes rather than procedures. OOP lets you create basic building blocks of program like classes and objects first. Then you can assemble them together to get required output. This approach of designing basic blocks first and them assemble them together to reach expected result is called as bottom-up approach.


Now, you are clear about top down and bottom approach.

Moving forward, Procedural programming consists of list of instructions on how to do a task. That means procedural programming focuses on doing things (algorithm), not on data. Also, as there are multiple functions and they need to data to perform operations. To make data available to multiple functions, it needs to be declared as global. Global data is more vulnerable to changes and it is difficult to find which function and exactly which line has introduced discrepancy. OOP ties data more closely to functions which work on it. It protects data to be accessed by other functions. From this paragraph we can conclude 2 points:

  • Procedural programming focuses more on procedure (algorithm) whereas OOP focuses on data.
  • The data moves around the system from function to function. Thus it is more vulnerable to modifications. In OOP, data is hidden and cannot be accessed by external functions.

Characteristics of object oriented programming

  • Emphasis on data instead of procedure.
  • Follows top-down approach.
  • Supports modularity at class level.
  • Programs are divided into small instance called Objects.
  •  Data is hidden and inaccessible by external functions.
  • Objects can communicate with each other with the help of functions.
  • Functions that operate on data object are tied together in a data structure called class.
  • New functions and data can be added whenever necessary.
Supporting languages: Java, Python etc.
 




No comments:

Post a Comment

Operators in Java

Operator is a symbol which is used to perform operation on data. Eg. +,-,/,* etc. Do you know : Operator and operan...

Popular