The eve of Domain Driven Design: object oriented thinking

Time:2021-3-9

object-oriented

Object oriented technology is a kind of technology to solve the problemworldUnderstanding and abstract methods. thatobjectWhat is it?
Object is the understanding and abstraction of the world, and the world is also called objectcreation. Understanding the world is more complicated, but the world is made up ofthingIt’s made up of.
It is such a relationship that understanding things is extremely important. So what is itthingWhat about it?
Things: bymatterandmatterIt consists of two aspects. Things are things, things are objects. What is a thing? What is an object?

  • The act of willIt’s for business.
  • Everything that exists is for objects, and objects are composed of attributes.

A thing is a combination of attributes and behaviors. becauseobjectThat’s rightthingTherefore, object is the understanding and abstraction of the properties and behaviors of a thing. It is such a relationship,object-orientedIt’s about understanding somethingAttributes and behaviorsUnderstanding and abstract methods.
To understand objects and abstract “objects” is to understand and abstract the properties and behaviors of things.

Properties and actions

The core of object-oriented isobject, the object is created byattributeandmethodIt’s a combination. When using object-oriented analysis, design and coding, you canfirstIt should be thought thatattributeandmethodObjects formed by combination. When you need to combine, you should not have objects that only contain attributes or methods.

  • When do I need an object that combines properties and methods?
  • When do I need only objects that contain attributes?
  • When do I need only objects that contain methods?

Things are made up of things and objects. Things are actions, objects are attributes.

  • When youneedAbstract the meaning of a thingThings and objectsYou need a combination of properties and methods.
  • When you onlyneedAbstract the meaning of a thingobjectYou just need toattribute
  • When you onlyneedAbstract the meaning of a thingthingYou just need tomethod

Object modeling

In the database system, what they care about is the information in thingsobjectSo when abstracting things, they only abstract the properties of things. In application systems, they are concerned with three ways to express things (combination of attributes and methods, only attributes and only methods), so when abstracting things, you need to think about which way you need.
As long as you need to abstract the properties of things (things and objects), that is, this part of the object, it may need to be persistent. As long as persistence is needed, it is usually saved in a relational database. Tables in a relational database basically correspond to the attributes of objects in an object-oriented database.
Because the table in the database only abstracts the attributes in the object, it may be incomplete. As far as the properties of abstract things are concerned, there are still two kinds: only the properties of abstract things, the combination of properties and methods of abstract things.
It’s in the databasesurfaceThis kind of abstraction forms the data model, which is incomplete compared with the object model, so we must use object (object) abstraction instead of data (attribute, object) abstraction in object-oriented analysis (OOA).
for instance:
Simple financial account
The attributes are: ID, balance and status
The operations are: open, close, credit and debit.
The data model only needs to design fields and association relations, so the following SQL is basically completed.

create table account
(
 id      integer, balance integer, status  integer);

If the above SQL is transformed into Java object, it will be a data model designed with object-oriented rather than a complete object model. This kind of model is very common in java development, which is the result of data model thinking.

@Getter
@Setter
public class Account {
 private int id; private int balance; private AccountStatus status;}

If you use the thinking of object model to design a model, from the perspective of interface, it should be like this:

public interface Account {
 int getId();
 int getBalance();
 AccountStatus getStatus();
 void open();
 void close();
 void credit(int amount);
 void debit(int amount);}

If the account interface conforms to the design of financial account, the simplest implementation of account should be as follows:

@Getter
public class 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; }}

This is to compare the differences between the object model and the data model from two modeling perspectives. Next, we will also compare the complete execution process.

Account Credit

The first is to use the sequence diagram designed by the data model. Because the account under the data model does not contain business logic, all business logic is in the accountservice, so it is usually called business logic service (layer) or transaction script. As shown below:
The eve of Domain Driven Design: object oriented thinking
Implementation using java code:

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); }}

Now we need to use the thinking of object model to design the sequence diagram
The eve of Domain Driven Design: object oriented thinking
Implementation using java code:

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")); account.debit(amount); return this.accountRepository.save(account); }}

There is no business code in the creditaccount method of accountservice, and it is more about coordinating the call execution process. For this kind of service object which is only used to implement the execution process and does not contain business logic, they are called application service.
For example, a domestic service company is similar to accountservice
For example, if you want to ask a cleaning aunt to do cleaning work for your home, first you call the housekeeping service company and say that you want to do cleaning work for your home. Then the housekeeping company arranges a cleaning aunt to come to your home to help complete the cleaning work. In this process, the housekeeping company mainly does reception, coordination, arrangement, and finally may include some cleaning aunt’s performance A series of work. The above accountservice is doing the same thing. Therefore, in the object model, accountservice only needs to do the coordination work like that of a domestic company. Specifically, the cleaning Auntie is responsible for the work, and the cleaning Auntie here is equivalent to the account object.
From the comparison of the two aspects, data model modeling with business logic services is more like procedural programming, just using object-oriented language to write procedural code. The way of using object model with application service is in line with object-oriented programming.

Combination and aggregation

In most business development, Association (one-to-one, one to many, many to many) and inheritance generalization are generally mentioned, and composition and aggregation are seldom concerned, but composition and aggregation are very important in object-oriented.
Combination and aggregation is to explore the relationship between the whole and the part, which is stronger than the relationship between the whole and the part. For example: car and tire, the car is a whole, tire is a part of the car. If the car has no tires, then it can not constitute the integrity of the car, so when discussing the relationship between the whole and the part, we should pay special attentionThe dependence of the whole on the part rather than the dependence of the part on the whole
First, we consider the dependency relationship between the whole and the part through the use case of a person’s eating process, and then illustrate the difference and connection between composition and aggregation in the example.
This feeding process requires the cooperation of multiple human organs. First of all, the food is sent into the oral cavity through a way, chewing by teeth and stirring by tongue, then swallowing by throat, entering the stomach from esophagus, digesting initially through the stomach, turning the food into chyme, and then entering the small intestine. Under the action of the spleen, the fine matter is absorbed.
be careful: the execution process from mouth to stomach is not an input / output mode, but a stream mode, followed by a connection. From this point of view, the mouth is just the entrance to the stream, but this use case is mainly to illustrate the connection between the whole and the parts, so each part of this connection is modified to the input / output call mode.
Model this eating process! Firstly, the key object models are identified as person, mouth, esophagus, stomach and intestines. The code is as follows:

//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; }}
//Gut
public class Intestine {
 public void absorb(Object chyme) { // absorbing... }}
public class Person {
 private final Mouth mouth; private final Esophagus esophagus; private final Stomach stomach; private final Intestine intestine;
 public Person() { this.mouth = new Mouth(); this.esophagus = new Esophagus(); this.stomach = new Stomach(); this.intestine = new Intestine(); }
 Public void eat (object food) {// eating.
 var paste =  this.mouth.chew (food); // chew to form paste.
 paste =  this.esophagus.transfer (paste); // transport food through the esophagus.
 var chyme =  this.stomach.fill (paste); // fill the stomach to form chyme.
 this.intestine.absorb (chyme); // absorb nutrients in the intestine.
 //In constipation
 }}
public class PersonTests {
 public static void main(String[] args) { new Person().eat("chips"); } }

In the whole process of eating, it starts with the action of eating done by a person, and then it is completed by a number of participating parts in the human body. This is the relationship between the whole and the parts. Person is a whole, while mouth, esophagus, stomach and intestine are parts of the whole. Then consider whether these partial objects are attached to the whole object, such as whether the mouth is independent of the human body and cannot survive, and exists and dies with the existence of human beings.This kind of creation, existence and extinction of partial objects are all together with the whole object, which is called combination. The aggregation is not as strong as the relationship between the whole and the parts of the combination. For example, the relationship between the car and the tire is a relationship between the whole and the parts, and the car can’t run without the tire. But the car can change the tire, this can change the relationship is not as strong as the combination relationship. In addition to replacement, there are still some missing items. For example, the crab has eight legs. Generally speaking, the crab can’t walk without legs, but it can walk without one or two. It may be difficult to walk.Such a relationship that can be replaced after initialization or does not need to be enforced is called aggregation
As time goes on and space expands, there will still be changes in the combination and aggregation. For example, in the future, people can change their mouths, the feeding process does not need the participation of their mouths, and for example, disposable cars can not be repaired or replaced after leaving the factory. Therefore, when discussing the relationship between combination and aggregation, we should discuss it within a certain limit.

summary

  • Object modeling, through the comparison of object model and data model to illustrate the need for an object model thinking.
  • Object modeling application, through the account deposit business to briefly explain how to use the object model.
  • Composition and aggregation, by focusing on the combination and aggregation, let it discuss the relationship between the whole and the part on the basis of the object model.