Chapter 15. Views: displaying data
This chapter covers
- Presenting data with views
- Removing display code from constructors
- Passing the same model to multiple views
The console isn’t the only place where you can display information for your users; I’m pretty sure you’re keen to see how to output data on a web page! There are also desktop and phone applications, emails, and print documents to consider. Even on the console you may want the flexibility of a number of different formats for your output; maybe you want a simple text version and a fancy version with boxes and borders. You don’t want to have to rewrite large portions of your programs to change the way they present information.
Views are focused modules of code that concentrate on displaying information. They take data and create visual output based on it. They may include controls like buttons and text boxes, but you’ll leave that for part 3. Moving your display code into views lets you switch the type of output you want or display data in multiple ways without having to change other parts of your code, like the constructor functions for models.
In this chapter, you create views in the context of two examples, the fitness app and The Crypt. For the fitness app, you build a simple console view and then an enhanced version using the spacer namespace for formatting. For The Crypt, you separate out the display code that’s currently in the Player and Place constructor functions, moving it into new player and place views.