Chapter 5. Aspect-Oriented Programming and Components

Many times in programming, we see pieces of source code that are repeated in different methods. In some cases, we could refactor our code and move them to separate modules. Sometimes, however, this is not possible. Some notable examples include logging and verification. Aspect-oriented programming is helpful in such cases, and we will get an understanding of it by the end of this chapter.

Components are reusable pieces of code that provide a number of services and have some requirements. They are extremely useful for avoiding code duplication and of course, for promoting code reuse. Here, we will see how to build components and how Scala makes the writing and use of components easier than other languages.

While getting familiar with aspect-oriented programming and components, we will go through the following top-level topics:

  • Aspect-oriented programming
  • Components in Scala

Aspect-oriented programming

Aspect-oriented programming, or AOP, addresses a common functionality, which spans across an application, but cannot be otherwise abstracted in a single module using traditional object-oriented techniques. This repeated functionality is often referred to as cross-cutting concerns. A common example is logging— normally, loggers are created within classes and then their methods are called inside the methods of the classes. This helps with the debugging and tracing of events in an application but it is not really related to the actual functionality in any way.

AOP recommends that cross-cutting concerns are abstracted and encapsulated in their own modules. In the next few subsections, we will look into how AOP improves code and also makes cross-cutting concerns easily extendible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.80.209