Fundamentals 12 min read

Object‑Oriented Modeling: Concepts, Code Examples, and Design Principles

This article explains the principles of object‑oriented modeling, contrasting object and data models, illustrating with a simple account example, discussing composition versus aggregation, and providing Java code snippets for modeling processes such as eating, demonstrating how to design and implement domain objects and services.

Architecture Digest
Architecture Digest
Architecture Digest
Object‑Oriented Modeling: Concepts, Code Examples, and Design Principles

Object‑oriented (OO) is a way of understanding and abstracting the world by treating entities as objects that combine attributes and behaviors.

The article defines objects, things, and attributes, explaining that an object is an abstraction of a thing’s properties and actions.

Attributes and Operations

The core of OO is that objects consist of attributes and methods; when designing, one should consider the appropriate combination of attributes and methods.

Object Modeling

In database systems, only attributes are persisted, leading to a data model that may be incomplete compared to a full object model. An example of a simple financial account is given, with its attributes (id, balance, status) and operations (open, close, credit, debit).

create table account (
    id integer,
    balance integer,
    status integer
);
@Getter
@Setter
public class Account {
    private int id;
    private int balance;
    private AccountStatus status;
}
public interface Account {
    int getId();
    int getBalance();
    AccountStatus getStatus();
    void open();
    void close();
    void credit(int amount);
    void debit(int amount);
}

When the data model is converted to Java objects, it yields a data‑model‑oriented class rather than a true object model.

Application Service

The article shows an AccountService that coordinates business logic, illustrating the difference between procedural code (data‑model‑driven) and true OO design (object‑model‑driven).

public class AccountService {
    private final AccountRepository accountRepository;
    public AccountService(AccountRepository accountRepository) {
        this.accountRepository = accountRepository;
    }
    public Account creditAccount(int accountId, int amount) {
        var account = this.accountRepository.findById(accountId)
                .orElseThrow(() -> new AccountException("The Account was not found"));
        if (AccountStatus.OPENED != account.getStatus()) {
            throw new AccountException("The Account is not open");
        }
        account.setBalance(account.getBalance() + amount);
        return this.accountRepository.save(account);
    }
}

It explains that such a service is an application service that orchestrates operations without containing core business rules.

Composition and Aggregation

The distinction between composition (strong whole‑part dependency) and aggregation (weaker, replaceable part) is discussed with examples such as a car‑wheel relationship and a human digestive system.

Eating Process Example

A Java model of a person eating is presented, showing how the whole (Person) coordinates parts (Mouth, Esophagus, Stomach, Intestine) to illustrate composition.

// Mouth
public class Mouth {
    public Object chew(Object food) { return food; }
}
// Esophagus
public class Esophagus {
    public Object transfer(Object paste) { return paste; }
}
// Stomach
public class Stomach {
    public Object fill(Object paste) { return paste; }
}
// Intestine
public class Intestine {
    public void absorb(Object chyme) { /* absorbing... */ }
}
public class Person {
    private final Mouth mouth = new Mouth();
    private final Esophagus esophagus = new Esophagus();
    private final Stomach stomach = new Stomach();
    private final Intestine intestine = new Intestine();
    public void eat(Object food) {
        var paste = mouth.chew(food);
        paste = esophagus.transfer(paste);
        var chyme = stomach.fill(paste);
        intestine.absorb(chyme);
    }
}

The example demonstrates how composition ties the lifecycle of parts to the whole.

Open‑Source E‑Commerce Platform

The article briefly mentions “Mallfoundry”, an open‑source Spring Boot multi‑tenant e‑commerce platform that follows domain‑driven design and OO principles.

Summary

Object modeling, contrasting object and data models, composition versus aggregation, and practical Java examples together illustrate the need for an object‑model mindset in software design.

JavaDomain-Driven Designmodelingobject-orientedAggregationcomposition
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.