7.2. Separating Concerns

The Model-View-Controller pattern that emerged from Smalltalk is similar to the batch-processing model of Input-Process-Output. Separating the concerns of how input is handled and validated from the way it is processed and then reported or displayed makes a program easier to maintain. This same separation of concerns is applicable to other facets of the system.

SEPARATE CONCERNS TO MAKE SMALLER CONCERNS

Split responsibilities among multiple methods and multiple classes to simplify each method and class.

Sam's system needs to create a rental contract. Contract production can be separated into two steps: preparing the contract and printing the contract. The printed rental contract is a report. The steps for almost every report include the following:[*]

[*] Some report-writing tools, such as Crystal Reports, combine steps 1 and 2.

  1. Calculate all values needed for the report, including counts and totals.

  2. Create an output representation of the report using the appropriate formatting language (plain text, HTML, PostScript, etc.).

  3. Display the output representation on the desired device (display screen, printer, browser window, etc.).

To clarify the distinction of the three steps, we create three separate methods.[†] Let us call the functions:

[†] Some operating systems tie the creation of the output representation and its display together. For example, when you prepare output for a particular printer, the operating system automatically passes that output to the ...

Get Prefactoring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.