The initial change made in this recipe is to check whether the plugin is running on a single site or network installation of WordPress and associate a callback function with the appropriate action hook depending on the result. As you can see, we use the same callback function in both cases, since the widget registering function should call wp_add_dashboard_widget to register a widget with the system in either case.
The widget rendering function is also shared between both modes. When running in a network installation, the rendering code first gets a list of all sites using the get_sites function, then cycles through the list of sites and uses the switch_to_blog function to access data from each site's database tables. Once all the blogs have been processed, we use the restore_current_blog function to go back to the original site that is configured as the top-level site in the network.
When running on a single site installation of WordPress, we create a dummy list of sites that contains a single entry to allow us to use the same foreach loop control structure. We then avoid calling the functions that are related to network site operations when WordPress is not configured in the multisite mode. Otherwise, the actual code that queries how many book reviews are present and displays them is identical between both versions of the widget.