Aspect Oriented Programming – Part 1

Today, I’m going to start new series of tutorials on a new topic. i.e. Aspect-Oriented Programming, in which you will learn about the new programming paradigm (though it’s too old), its basics, the core concept, comparison with current programming paradigms like Object Oriented Programming (OOA) and finally implementation for any real-world problem or scenario.

I will post 3, 4 articles for this topic, dividing this into the following possible sections

  • Object-oriented programming approach and some flaws
  • The core theme of Aspect-Oriented Programming
  • Solutions provided by AOP against OOP flaws
  • Important terms used in AOP talk
  • A programming example with OOP and AOP
  • Introduction of AspectJ, a java implementation of the aspect-oriented approach.

Object-Oriented Programming Approach

Computer software or programs are automation of real-world scenarios, a transformation of manual work into computer-operated systems. Many approaches are in practice to design and develop more usable, user-friendly, easily evolvable and maintainable computer systems. Different approaches handle the under-considerate domain with their own style. The object-oriented approach focuses on data, takes anything in the real world as data having some attributes (data fields) and some operations (methods). This approach evolves in the sixth decade of the last century when the computer system becomes much more complex and the current programming paradigms were not much capable to cope with required features to facilitate both developers and end-users. At that moment researchers made on some new style of programming and then introduced a “data-focused” approach. i.e. object-oriented programming where everything in real life was considered an object, in other words, every noun is an object. We design systems based on these nouns in real-world like Pay, Account, Employee, Product, PurchaseOrder, Invoice etc

The object-oriented approach was at its peak in the mid-’80s and late ’90s, even still a great number of computer systems are being developed using the object-oriented approach, but from its birth, it is faced many critics. Though the implementer of this approach is introducing many new features day by day in their respective languages compilers but only adding feature after feature into something don’t make it better. Polymorphism, encapsulation, abstraction no doubt are building blocks of any mature programming language and should be available implicitly, yet still, there are few areas where developers need more ease and re-usability of their efforts and code. Those areas were perceived and made research and then a new approach was presented to design and develop software modules with more ease in re-usability of existing code, more ease in extendability and maintainability. This approach was AOP.

Did you ever feel that while developing in an object-oriented world, you have you rewrite the same chunk of code in different classes/methods to achieve the same goal? like (as a very simple example) in the data access layer, you have to open a database connection, maintain its session, write a query, call to the database to fetch data and at the end, close the database connections and sessions. Like following

method Fetch_Employee_DATA{

open database session;
create a query to fetch data;
call to the database to fetch data;
close database session;
return results;

}

here the bold text is those lines of code you have to write in each method you call to fetch data from the database, other lines are method-specific and depends upon the business logic.

Like the other example, if you have to log each and every database call. i.e. the query to the database, its parameter, the returned results etc, then you have to write the same line of codes every time in each method that is going to interact with the database. The above method will look like

method Fetch_Employee_DATA{

log the start of method;
open database session;
create a query to fetch data;
log query and its parameters;
call to the database to fetch data;
close database session;
log returned data from the database;
log end of method;
return results;

}

Again the bold lines are those lines of code you are replicating in each method to achieve the same goal. Don’t you think you’re spending a lot of time writing the same bits of code when it comes to implementing the individual methods? These things like logging, security checking, transaction management are such aspects of the systems that are orthogonal to the specific application. Every organization need them and every developer has to implement them in their respected module, classes and methods. In short, the method hasn’t concern with these replicated lines, or the core concern of the method is to fetch employees, not to log database transactions, session maintenance etc. Every class or method is doing something extra time and again that doesn’t concern their core requirement.

For small projects for small level organizations, it may work. But what if you are going to design and develop a banking system, an ERP for a large scale company, or a payroll system for a big industry … where the modules are interlinked cohesively, the domain is very large, covering all business scenarios of under-considerate organization. In such a system, you can’t afford such a framework and design that is difficult to extend, maintain and reuse. You can’t let your developers spend time and resources over rewriting the same bit of code in every class and method, instead of focusing core business logic of that class. In other words, you can’t let everyone focus on those things that are not concerned with their core task.

Consider the above example again. You are working on a project (let’s suppose inventory management). While developing different features or modules, you have to call the database to fetch and insert data as per business logic and domain requirement. We have seen that in every method you write, you are writing the code of opening the database session and closing it properly. In a medium and large scale project, the number of methods doing the same things could be hundreds. Anyhow, you complete your task and deploy the application at the client site. Next time you are asked to maintain logs for each database interaction (as we discussed above) you instantly recall the number of classes and methods where you have to write code related to logging …. you have more than 40, 50 classes (let’s suppose) and there are hundreds of methods interacting with database. You have to open each method’s body and has to write logging code there

“Sir it will take atleast 4,5 days to complete this task”

“4, 5 days, but way, its really a simple task”

“indeed its simple but i have review each and every package, class and method to embed logging code there. To make sure the sustainability of application, i have to test it thoroughly to check that our existing application are new changes are running fine”

“hmmm ok, add someone else with you and try to complete it within 2,3 day, there are lot of other things too to do”

“sure i will”

This is the expected dialog between a developer and his manager/team lead while incorporation a simple change in running application. See, the actual lines that has to write in every method are very simple, just 2, 3 lines. But when it span over entire application with hundreds of packages/classes/methods then it becomes worst, consuming your more resources in term of human effort, time and budge.

Think again … is the logging concerned to method which was written to fetch data from database? Should a developer concern with database issues like opening a session and closing it properly who want to focus his attention to design an optimal query to fetch data from database?

In next post i will introduce Aspect Oriented Programming (AOP) and will discuss how AOP provides solutions for problems we discussed earlier.

Share on Facebook


Comments 53

  1. Ans, its really a good post. you keep your style simple and understandable. I’m waiting for next post … plz be quick

  2. This design is spectacular! You definitely know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job.
    I really loved what you had to say, and more than that,
    how you presented it. Too cool!

    Feel free to visit my blog; Allura Fresh Reviews

  3. I’ve been exploring for a little bit for any high quality articles or blog posts on this kind of space .
    Exploring in Yahoo I at last stumbled upon this website.
    Reading this info So i am glad to express that I’ve a very just right uncanny feeling
    I found out just what I needed. I such a lot
    indisputably will make certain to do not forget this site and give it a look
    regularly.

    Feel free to visit my site :: alineconaway564.mywibes.com

  4. Do you mind if I quote a few of your posts as long
    as I provide credit and sources back to your website? My blog site is in the exact same area of interest
    as yours and my visitors would definitely benefit from some of the information you
    provide here. Please let me know if this okay with you. Thanks a
    lot!

  5. It’s a shame you don’t have a donate button! I’d definitely donate to this excellent blog!
    I suppose for now i’ll settle for bookmarking and adding your RSS feed
    to my Google account. I look forward to brand new updates and will talk
    about this site with my Facebook group. Chat soon!

  6. Greetings from Carolina! I’m bored to death at work
    so I decided to browse your website on my iphone during lunch break.
    I enjoy the information you provide here and can’t wait to take a look
    when I get home. I’m surprised at how fast your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyhow, superb site!

  7. I like what you guys tend to be up too. This kind of clever work and coverage!
    Keep up the fantastic works guys I’ve included you guys to
    my own blogroll.

  8. Heya! I understand this is sort of off-topic
    but I had to ask. Does running a well-established blog like yours take a massive amount work?
    I am completely new to operating a blog however I do write in my diary daily.
    I’d like to start a blog so I will be able to share my own experience and views online.
    Please let me know if you have any suggestions or tips for brand new aspiring blog owners.
    Thankyou!

    Feel free to visit my web page – Leaf Max CBD Oil

  9. I feel that is among the such a lot vital info for me.
    And i’m satisfied reading your article. But want to
    observation on few normal issues, The web site taste is perfect, the articles is truly excellent : D.
    Excellent activity, cheers

  10. Hello! This is my first visit to your blog! We are a group of volunteers and starting
    a new project in a community in the same niche.
    Your blog provided us beneficial information to
    work on. You have done a wonderful job!

    My website – Keto BHB Plus

  11. My wife and i ended up being very happy Chris could finish up his
    analysis through the precious recommendations he received through
    your web page. It’s not at all simplistic just to find yourself
    offering information and facts which usually the others might
    have been trying to sell. We understand we now have the writer to thank for this.

    The type of illustrations you made, the simple web site navigation, the relationships you can give support to instill – it’s mostly
    unbelievable, and it’s really letting our son in addition to the family reason why
    the situation is entertaining, and that is wonderfully
    serious. Thank you for the whole lot!

    my web page – Kurtis

  12. I have to thank you for the efforts you have put in writing this site.
    I really hope to view the same high-grade blog posts by you later on as well.
    In truth, your creative writing abilities has inspired me to get my own website now 😉

    My site – http://www.diigo.com

  13. You’ve made some good points there. I checked on the internet to
    find out more about the issue and found most individuals will go along with your views on this website.

  14. Everything is very open with a precise description of the challenges.
    It was truly informative. Your website is very useful.
    Thank you for sharing!

  15. This is very interesting, You’re a very skilled
    blogger. I have joined your rss feed and look forward to seeking more of your fantastic post.

    Also, I have shared your site in my social networks!

  16. Definitely consider that that you stated. Your favourite reason appeared
    to be at the web the simplest thing to take into accout of.
    I say to you, I certainly get annoyed while folks consider issues that
    they plainly don’t know about. You managed to hit the nail upon the top
    and defined out the whole thing with no need side effect ,
    other people could take a signal. Will probably be
    again to get more. Thank you!

    Visit my webpage; Vigor Max (Garnet)

  17. Excellent post. I was checking continuously this blog and I’m impressed!

    Extremely useful information particularly the last part
    🙂 I care for such info much. I was looking for this certain info for a very long time.
    Thank you and best of luck.

    My homepage – Renown CBD Oil

  18. Hey! I know this is somewhat off topic but I was wondering which blog
    platform are you using for this website? I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking at
    alternatives for another platform. I would be fantastic if you could point me in the direction of a good platform.

  19. Today, while I was at work, my sister stole my iPad
    and tested to see if it can survive a twenty
    five foot drop, just so she can be a youtube sensation. My iPad
    is now broken and she has 83 views. I know this is completely off topic but I had to share it
    with someone!

  20. You actually make it seem really easy with your presentation but I to find this matter to be actually something which I feel I might never understand.
    It kind of feels too complex and extremely vast for me.
    I’m taking a look ahead on your next put up, I’ll attempt to get the hold of it!

  21. Pretty nice post. I just stumbled upon your
    blog and wanted to say that I’ve truly enjoyed surfing around your blog posts.

    After all I’ll be subscribing to your feed and I hope you write again very soon!

    My web site: Zenzi CBD

  22. Hello! I’ve been following your weblog for some time now and finally got the bravery
    to go ahead and give you a shout out from Porter Tx!
    Just wanted to say keep up the great job!

  23. You actually make these kinds of seem simple with
    your exhibition but Not long ago i find this kind of matter internet marketing actually a thing that
    I think I might in person never understand. It seems as well complex
    and very broad to me. I am looking forward on your coming post, Sick try to get
    the hang of it!

Leave a Reply

Your email address will not be published.