AOP vs OOP
AOP (Aspect-oriented programming) and OOP (Object-oriented programming) are two programming paradigms. A programming paradigm is a fundamental style of computer programming. Programming paradigms differ in how each element of the programs is represented and how each step is defined for solving problems. As the name suggests, OOP focuses on representing problems using real-world objects and their behavior, while AOP deals with breaking down the programs in to separate crosscutting concerns.
What is AOP?
AOP is a programming paradigm, which deals with breaking down a program in to cohesive areas of functionality (called concerns) that cut across multiple areas, in order to increase modularity. Support for abstractions (such as classes, methods, etc.) to group and encapsulate concerns in to unique entities is provided in many other programming paradigms. But concerns (such as “Logging”) are examples of crosscutting concerns, because every logged part of the system is affected by the strategy used for logging. The main focus of all AOP implementations is to have suitable crosscutting expressions to capture all concerns in a single location.
What is OOP?
In OOP, the focus is on thinking about the problem to be solved in terms of real-world elements, and representing the problem in terms of objects and their behavior. Classes depict the abstract representations of real world objects. Classes are like blueprints or templates, which gather similar items or things that can be grouped together. Classes have properties called attributes. Attributes are implemented as global and instance variables. Methods in the classes represent or define the behavior of these classes. Methods and attributes of classes are called the members of the class. An instance of a class is called an object. Therefore, an object is a data structure that closely resembles some real-world object.
There are several important OOP concepts such as Data abstraction, Encapsulation, Polymorphism, Messaging, Modularity and Inheritance. Typically, encapsulation is achieved by making the attributes private, while creating public methods that can be used to access those attributes. Inheritance allows the user to extend classes (called sub classes) from other classes (called super classes). Polymorphism allows the programmer to substitute an object of a class in place of an object of its super class. Typically, the nouns found in the problem definition directly become classes in the program. And similarly, verbs become methods. Some of the most popular OOP languages are Java and C#.
What is the difference between AOP and OOP?
The key difference between OOP and AOP is that the focus of OOP is to break down the programming task in to objects, which encapsulate data and methods, while the focus of AOP is to break down the program in to crosscutting concerns. In fact, AOP is not a competitor for OOP, because it emerged out of OOP paradigm. AOP extends OOP by addressing few of its problems. AOP introduces neat ways to implement crosscutting concerns (which might have been scattered over several places in the corresponding OOP implementation) in a single place. Therefore, AOP makes the program cleaner and more loosely coupled.