Applying Software Design Principles3:26 with James Churchill
In this video, we’ll learn about software design principles and how we can use them to improve the overall code quality of our controller.
The “Separation of Concerns” and “Don’t Repeat Yourself (DRY)” design principles are just two of many design principles. Here are some additional design principles.
- Single Responsibility Principle
- Principle of Least Knowledge
- Minimize Large Design Upfront
- You Ain't Going to Need It
Here’s a page on MSDN that touches upon some of the key principles of software design.
SOLID is a mnemonic acronym that stands for the five basic design principles of object-oriented programming. This a fairly advanced concept, but it’s instructive on how these design principles can be used together.
As I mentioned at the end of the previous video, 0:00 something is amiss in our comic books controller class. 0:03 No worries, though. 0:07 In this video, we'll dissect the issue and propose a solution. 0:08 Here's our ComicsBooksController class. 0:13 Our controllers Detail action method is currently responsible for 0:15 creating the comic book model instance that we pass to our view. 0:19 Ideally, our controller wouldn't be concerned with that aspect of our app. 0:23 Why? 0:28 Let's take a look at a scenario. 0:29 Imagine that we had another method in another class that needed a comic book 0:31 model instance for Amazing Spider Man, number 700. 0:36 With our current implementation, we'd need to repeat this code in the second method. 0:41 Doing this would violate a design principle called DRY or 0:46 Don't Repeat Yourself. 0:50 So, how do we avoid repeating ourselves? 0:52 We move our code that creates a comic book model instance into its own method named 0:55 GetComicBook. 1:01 Now, any method in our website that needs an instance of that comic book model, 1:03 can call the GetComicBook method. 1:07 Eventually, we're going to need a method 1:10 that will return an array of comic books for our comic books list page. 1:13 We can put that method in a class along with the GetComicBook method. 1:18 We can think of this class as a central location, or repository, for 1:23 storing and managing our comic book model instances. 1:28 Let's name this class ComicBook Repository. 1:32 By organizing our data access code in this way, 1:36 we're implementing the Repository Design Pattern. 1:39 Creating a design where we don't repeat ourselves allows for 1:44 our classes to naturally be more tightly focused. 1:48 Each class should have a single responsibility, or concern, and 1:51 not overlap with any other classes on our website. 1:56 This is known as the separation of concerns design principle. 2:00 Separating concerns also helps make our website easier to maintain. 2:05 With our improved design, if we wanted how our comic book model instances are created 2:10 or retrieved, we'd only need to change the comic book repository class. 2:16 The MVC design pattern is also an example of 2:22 the Separation of Concerns Design Principle. 2:25 The model is concerned about our website's data and business logic. 2:28 The view is concerned about providing a visual representation of the model. 2:32 And the controller is concerned about processing requests and 2:38 preparing responses. 2:42 Separating our website's concerns using the MVC design pattern 2:44 makes our code easier to understand and more maintainable. 2:48 Software design principles represent a set of guidelines 2:53 that help us avoid running into problems with our code in the long run. 2:56 Across all software development platforms and languages, these principles have 3:01 originated from lots of developers solving the same problems over and over again. 3:06 There are other design principles, 3:13 aside from the ones that we've mentioned in this video. 3:14 See the teacher's notes for links to additional resources. 3:17 In the next video, let's add the comic book repository class to our project. 3:21
You need to sign up for Treehouse in order to download course files.Sign up