Domain-Driven Design: Tackling Complexity in the Heart of Software by Eric Evans

Domain-Driven Design: Tackling Complexity in the Heart of Software by Eric Evans

Author:Eric Evans [Evans, Eric]
Language: eng
Format: epub, pdf
Tags: Computers, Programming, Object Oriented, Software Development & Engineering, General, Systems Analysis & Design
ISBN: 9780321125217
Google: xColAAPGubgC
Amazon: 0321125215
Publisher: Addison-Wesley Professional
Published: 2003-08-30T00:00:00+00:00


Example

Refactoring: A Paint-Mixing Application

A program for paint stores can show a customer the result of mixing standard paints. Here is the initial design, which has a single domain class.

Figure 10.2.

The only way to even guess what the paint(Paint) method does is to read the code.

public void paint(Paint paint) { v = v + paint.getV(); //After mixing, volume is summed // Omitted many lines of complicated color mixing logic // ending with the assignment of new r, b, and y values. }

OK, so it looks like this method combines two Paints together, the result having a larger volume and a mixed color.

To shift our perspective, let's write a test for this method. (This code is based on the JUnit test framework.)

public void testPaint() { // Create a pure yellow paint with volume=100 Paint yellow = new Paint(100.0, 0, 50, 0); // Create a pure blue paint with volume=100 Paint blue = new Paint(100.0, 0, 0, 50); // Mix the blue into the yellow yellow.paint(blue); // Result should be volume of 200.0 of green paint assertEquals(200.0, yellow.getV(), 0.01); assertEquals(25, yellow.getB()); assertEquals(25, yellow.getY()); assertEquals(0, yellow.getR()); }

The passing test is the starting point. It is unsatisfying at this point because the code in the test doesn't tell us what it is doing. Let's rewrite the test to reflect the way we would like to use the Paint objects if we were writing a client application. Initially, this test will fail. In fact, it won't even compile. We are writing it to explore the interface design of the Paint object from the client developer's point of view.

public void testPaint() { // Start with a pure yellow paint with volume=100 Paint ourPaint = new Paint(100.0, 0, 50, 0); // Take a pure blue paint with volume=100 Paint blue = new Paint(100.0, 0, 0, 50); // Mix the blue into the yellow ourPaint.mixIn(blue); // Result should be volume of 200.0 of green paint assertEquals(200.0, ourPaint.getVolume(), 0.01); assertEquals(25, ourPaint.getBlue()); assertEquals(25, ourPaint.getYellow()); assertEquals(0, ourPaint.getRed()); }

We should take our time to write a test that reflects the way we would like to talk to these objects. After that, we refactor the Paint class to make the test pass.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.