Determine When to Use Design Patterns Flashcards
What is a Design Pattern?
A Design Pattern is like a standard that is universally agreed upon to help streamline code development. This helps you to write code that will accomplish exactly what you want it to accomplish.
Design patterns are to programming like the “Hero’s Journey” is to stories/tales; an archetype or series of plot points that your program will go through.
Is a Design Pattern code?
NO - A design pattern is NOT code… a Design Pattern is a set of rules about how you can write code.
You can apply a design pattern to any language that you want to program in.
What is a Singleton Design Pattern?
What is an example?
A pattern that restricts classes to only being instantiated ONE TIME during the execution of the program.
Each class creates a SIGNLE instance/object.. If you need to create new ones, it will just refer back to the first object that was created from the class.
○ Ex - program values that you need to execute one time when the program boots up, and then once running the code just uses that instance from now on
○ Ex - static values you want referenced throughout your program
What is an Observer Design Pattern?
What is an example?
A pattern that allows multiple/different things (Observers) to receive updates or notifications when a single source (Subject) changes.
Very popular in UI systems and/or Event Updates.
EX - you have text boxes on one side and a chart on the other. Each time one of the text boxes is updated (Subject Object) the chart on the other side updates (Observer Object).
What are the 2 Classes that are defined by an Observer Pattern?
Subject Class and Observer Class
Subject Class - maintains a list of observers and includes methods for attaching, detaching, and notifying observers.
Observer Class - method to receive updates from the subject.
Is an Observer to Subject Class relationship 1:1?
NO - an Observer can be subscribed to many different Subjects; loosely coupled.
What is a Model View Design Pattern?
Very popular design patter that’s come about in recent years (since 2005); it’s really good for web development
A high-level abstraction where responsibilities are divided up into 3 loosely coupled components – the Model, the View, and the Controller.
○ Each component has different responsibilities – you can work on the different components at different times or in parallel
What is the “Model” in the MVC Design Pattern?
The component that stores or structures data.
EX - If you have a list of users in your application and those users need to be displayed on screen, the model is the Class that represents those users inside of the program.
What is the “View” in the MVC Design Pattern?
The component that displays the data; responsible for showing the data on the users screen.
What is the “Controller” in the MVC Design Pattern?
The component that handles the logic flow, handles any user interactions, and directs the Models and Views.
○ Ex - if a user clicks the search button on a View, the controller receives that event and it will direct the Model and the View to perform the search and update with the new data that resulted from the search
Basically:
○ Responds to user events/inputs
○ Tells the other 2 components (Model and View) what to do
Design Pattern Summary
→ Design patterns represent:
○ Tried and tested solutions within your code
○ Apply framework aka “scaffolding” for new code i.e code doesn’t have to be written from scratch each time
○ Supports communication between developers – if you understand the universal/accepted design pattern behind code, it will help you quickly understand what that code is supposed to do
→ Design patterns can communicate the intent of code to someone reading it for the first time