Object‑Oriented Modeling: From Data Model to Object Model, Composition, Aggregation and Practical Java Examples
This article explains the fundamentals of object‑oriented modeling, contrasting data‑model and object‑model designs, illustrating attribute‑method composition, composition versus aggregation, and providing concrete Java and SQL examples such as a simple Account domain, an eating‑process model, and an open‑source e‑commerce reference.
Object‑Oriented Basics
Object‑oriented (OO) is a way of understanding and abstracting the world; an object represents an abstraction of a thing (事物) by capturing its attributes and behaviors.
In OO, a thing consists of events (事) and objects (物). Understanding the relationship between attributes and methods is essential for proper modeling.
Attributes and Operations
The core of OO is that an object is composed of attributes and methods . When analyzing, designing, or coding, you should first think about the combination of attributes and methods that form the object.
When do you need an object with both attributes and methods?
When do you need an object that only has attributes?
When do you need an object that only has methods?
Object Modeling vs. Data Modeling
In a database system, only the attributes of a thing are persisted, so the data model is often incomplete compared with a full object model.
Example – a simple financial account:
create table account
(
id integer,
balance integer,
status integer
);Converted to a Java class (data‑model view):
@Getter
@Setter
public class Account {
private int id;
private int balance;
private AccountStatus status;
}Using a true object‑model mindset, the interface would expose behavior as well:
public interface Account {
int getId();
int getBalance();
AccountStatus getStatus();
void open();
void close();
void credit(int amount);
void debit(int amount);
}An implementation that follows the object model:
public class Account implements Account {
private int id;
private int balance;
private AccountStatus status;
public void open() { this.status = AccountStatus.OPENED; }
public void close() { this.status = AccountStatus.CLOSED; }
public void credit(int amount) { this.balance += amount; }
public void debit(int amount) { this.balance -= amount; }
}The service layer (Application Service) coordinates the workflow without containing business logic:
public class AccountService {
private final AccountRepository accountRepository;
public AccountService(AccountRepository repo) { this.accountRepository = repo; }
public Account creditAccount(int id, int amount) {
var account = accountRepository.findById(id)
.orElseThrow(() -> new AccountException("Account not found"));
if (account.getStatus() != AccountStatus.OPENED) {
throw new AccountException("Account not open");
}
account.setBalance(account.getBalance() + amount);
return accountRepository.save(account);
}
}Composition and Aggregation
Composition describes a strong whole‑part relationship where the part cannot exist without the whole (e.g., a car and its wheels). Aggregation is a weaker relationship where parts can be shared or replaced (e.g., a car can replace its wheels).
To illustrate, a human eating process is modeled with objects representing mouth, esophagus, stomach, and intestine. The Person object composes these parts, and the eat method coordinates their interaction:
// 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);
}
}Open‑Source E‑Commerce Example
Mallfoundry is a fully open‑source multi‑tenant e‑commerce platform built with Spring Boot, demonstrating domain‑driven design (DDD) and OO principles.
Conclusion
Object modeling highlights the need for an object‑oriented mindset rather than a pure data‑model approach.
Practical account‑management code shows how attributes and behaviors combine in OO design.
Composition vs. aggregation clarifies whole‑part relationships in object models.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.