Design Patterns
- Purpose
- To prevent reinventing the wheel
- To avoid ad hoc programming
- When you read code, you would be able to identify the design pattern being used and easily work with it
- Types
- Creational
- Structural
- Behavioral
- Concurrency
- Template Method
- This is a behavioral pattern that relies heavily on inheritance
- If you see multiple if/else statements in your code, then it might be time to use this pattern
- Example: You are building a TicTacToe game between human and computer
- You have multiple snippets like this in you code -
if human do this else do that - You have an abstract class
Player - Then you create subclasses
HumanandAIwith details of implementation - The generic Player class contains Template methods which defer specific logic to subclasses
- Strategy
- Lets behavior to be selected at run-time
- Used in authentication, twitter, facebook
- Strategy
- Context
- Runtime Flexibility
- Singleton
- A class has only a single object
- You cannot instantiate a new object since the
newmethod is private - You cannot access the object of the class using
Klass.instance - To use this pattern do a
include Singletonin your class - Example: Rails controllers use the singleton pattern. You cannot instantiate a new controller instance
- Factory
- The factory class determines which class to be instantiated
-
Categories
-
Database
-
Programming
-
Workflow
-
Devops
-
Architecture
-
Ui
-
Frameworks
-
Blogging