Is OOP Compatible With an Enterprise Context?

Is OOP Compatible With an Enterprise Context?

This look at object-oriented programming considers whether it holds up in an enterprise context with a simple examine using Spring, Kotlin, and Java.

This week, during a workshop related to a Java course I give at a higher education school, I noticed the code produced by the students was mostly ok — entirely procedural. In fact, though the Java language touts itself as an Object-Oriented language, it’s not uncommon to find such code developed by professional developers in enterprises. For example, the JavaBean specification is in direct contradiction of one of OOP’s main principles, encapsulation.

Another example is the widespread controller, service, and DAO architecture found equally in Java EE and Spring applications. In that context, entities are generally anemic, while all business logic is located in the service layer. While this is not bad per se, this design separates between state and behavior and sits at the opposite end of true OOP.

Both Java EE and the Spring framework enforce this layered design. For example, in Spring, there’s one annotation for every such layer: @Controller@Service, and @Repository. In the Java EE world, only @EJB instances — the service layer — can be made transactional.

This post aims to try to reconcile both the OOP paradigm and the layered architecture. I’ll be using the Spring framework to highlight my point because I’m more familiar with it, but I believe the same approach could be used for pure Java EE apps.

A Simple Use Case

Let’s have a simple use-case: from an IBAN number, find the associated account with the relevant balance. Within a standard design, this could look like:

@RestController
class ClassicAccountController(private val service: AccountService) {
​
    @GetMapping("/classicaccount/{iban}")
    fun getAccount(@PathVariable("iban") iban: String) = service.findAccount(iban)
}
​
@Service
class AccountService(private val repository: ClassicAccountRepository) {
    fun findAccount(iban: String) = repository.findOne(iban)
}
​
interface ClassicAccountRepository : CrudRepository<ClassicAccount, String>
​
@Entity
@Table(name = "ACCOUNT")
class ClassicAccount(@Id var iban: String = "", var balance: BigDecimal = BigDecimal.ZERO)

There are a couple of issues there:

  1. The JPA specifications mandates for a no-arg constructor. Hence, it’s possible to create ClassicalAccount instances with an empty IBAN.
  2. There’s no validation of the IBAN. A full round-trip to the database is required to check if an IBAN is valid.

Note: No, there’s no currency. It’s a simple example, remember?

java kotlin

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Kotlin vs Java | Kotlin or Java For Android Development | Kotlin And Java Difference

Kotlin and Java have showcased the cut-throat competition in the lastest Android Development. There are a lot of ambiguities amongst developers to choose one, from both. This video will help you to understand the strengths and weaknesses of both with a head to head comparison.

Kotlin vs Java | Java or Kotlin For Android Development

Kotlin vs Java will help you to understand the differences between Kotlin and Java. Time and again, many have misconceptions about both the languages in android development.

Java Online Training | Java Online Course | ITGuru

Our Java Online Training provide you to learn about Java programming and its different features with realty. Our Java Online Course includes live sessions, live projects

Java’s Records vs Kotlin’s Data Classes

How Record classes in Java’s most recent release compares to Kotlin’s data classes.