Home Artikelen Active-passive pattern

27

Dec

2009

Active-passive pattern
In domain modeling, Active Passive is a modeling pattern that can be summarized as follow: Active objects in the real world become passive in the domain and vice-versa.

Content

Intent

Even if we use bounded contexts our domain model can become really complex over time. The intent of our model is to represent our domain; that is reflecting the real world. A common error that's made is that we design our domain model too close to the real world. This is common because we tend to think that way. To avoid this we apply the Active Passive pattern. That is that every passive object in the real world becomes active in our model and every active object in the real world becomes passive in our model. Instead of creating a few huge and complex object, like we have in the real world, we'll create a huge amount of active objects that are working together to accomplish goals.

Example

Here are two examples of the Jacky tea story. One is a real world example and the other is an example of how it would look like in a virtual world. This story came up into my head when I was watching Disney's The beauty and the beast. This movie realy helped me by understanding this pattern.

Story in the real world

Jacky wants a cup of tea. She gets out of her chair, walks to the cupboard and grabs an empty cup. She puts the empty cup on the kitchen table, grabs the teapot and discovers it is empty. That means she has to make some tea first. She finds herself a kettle that is big enough for the amount of water. She uses the water tap to fill the kettle with water and makes sure she leaves enough room left so it won't boil over. Now she turns on the burner, sets the kettle on it and waits until it boils. After that she fills the teapot with the hot water from the kettle and adds a tea bag to it. After the tea is dark enough she removes the tea bag and fills the cup. Now she is ready to drink the tea.

Here you see that Jacky is a very complex object. She needs to know the whole process and has to use a large amount of object to get to her goal.

Story in the virtual world

Jacky wants a cup of tea. She tells to a cup that she wants a cup of tea. The cup goes to the teapot and asks it to fill him. The teapot discovers she is empty. The teapot asks the kettle for some boiled water. The kettle will ask the water tap to fill it up. After the kettle is filled it will put itself on the burner and waits until the water is boiling. Now it will fill the teapot with boiled water and the teapot adds a tea bag to it. After the tea is dark enough the teapot removes the tea bag and fills the cup with tea. Now the cup will offer itself to Jacky.

Instead of one complex object, Jacky now only has a dependency on the cup. The cup depends on the teapot. The teapot on the water tap and burner, and so forth. All object work together to accomplish a goal.

Result

The result of applying this pattern is a model that contains almost only small and simple objects that only have a few dependencies. Instead of ending up with a few huge and complex object with a lot of responsibility, you’ll end up with a higher amount of simple objects that all have their own responsibility, working together to complete goals and handle tasks. See it as an ant colony instead of one huge animal.

Useful tips

  • Use backward chaining to apply this pattern easier.
  • Remember that the goal is to end up with simple object with a single responsibility, not to create a minimal amount of objects.
  • Don't be afraid to introduce new objects.
  • Whenever you write procedure code in a method body of an object, try to introduce new objects to scatter the procedure over multiple objects.
Trackback(0)
Comments (1)Add Comment
0
...
written by Stefan Bookholt, december 28, 2009
Cool example, really useful. Another approach can be to introduce objects that represent the actions that can be done, like the "FillCup" action, which takes the cup and the teapot as parameters. Which one has the least disadvantages to your opinion?

Write comment

busy
 

Quote XNA presentatie

Bedankt voor je schitterende presentatie. Ik heb niets dan positieve geluiden gehoord.

Peter Criellaard
Microsoft

Quote Coding dilemma's

Eindelijk een presentatie waarmee ik ook iets mee kan in de praktijk!

Dennis Schuuring
Max BV

Quote C# fun!

Bedankt voor de presentatie, het was voor mij 1 van de betere momenten van die dag!

Henri Koppen
Arcenus