After this lesson you should be able to:
- Describe the 4 pillars of OOP (abstraction, encapsulation, inheritance, polymorphism)
- Name a few popular OOP design patterns
- Gather requirements for an OOP problem
- Design and diagram an OOP solution to an example (Tetris)
Object Oriented Programming - modeling some system or process by using objects or classes.
What objects exist in poker?
- Card
- Deck of cards
- Poker hand
- Poker game
- Player
- Bets
- Discard pile
What operations would be on each object? Which objects would use other objects?
- Creates an array of cards for a deck
- shuffle
- deal a card
- deal a hand
- Encapsulation
- Abstraction
- Composition
- Inheritance
- Polymorphism
- Data and processes should be owned by a class
- Data that other classes don't need to see, shouldn't be made available to other classes
Abstraction is the process of creating an interface to your class. The interface should be at a high enough level so that data that is owned by the object is hidden.
When a class child class takes functionality from a parent class. Usually the relationship can be stated this way: child is a type of parent.
- Parent - vehicle
- Children - motorcycle, boat, car, plane
motorcycle is a vehicle, boat is a vehicle, etc
Composition is when a class uses other classes to solve a problem. Typically, many problems can be solved using composition rather than inheritance.
An example of composition is our Deck class which uses an array of 52 Card instances to create the deck.
The idea that a group of child classes can be treated as a parent class and the implementation of the methods that are called will change based on the type of the base class.
Design patterns are a way to solve a problem in software that comes up often.
- Singleton - I want a single instance of a class (even if I ask for the class many times, the same one should be returned)
- Observer - When a part of an application changes, and other parts of the application need to be notified of the change
- Factory Pattern - Create an instance of a class. Often used if class creation is complicated or polymorphism is involved
- MVC - Model View Controller: the foundation for most modern web frameworks
To study more on design patterns, Addy Osmani has an excellent book called Learning JavaScript Design Patterns
- Practice Practice Practice (Build things using OOP)
- Prefer composition over inheritance
- Define the problem before jumping into code
- Ask lots of questions about requirements
- How should the app work in a certain case (think about edge cases)
- What data is given?
- Can I make an assumption about X or Y?
- Will data be given in a certain format?
- How would I test this app?
- List all the requirements (don't worry about grouping things into classes or functions yet)
- Break the problem down! Group requirements into related functionality
- Decide what classes should exist, what data the classes own, and what operations the classes need
- To help organize your thoughts, diagram the classes. Make notes of inheritance and composition
- Code!!!
Let's use the above guide to figure out how to implement tetris.
- tim@rithmschool.com
- @timgarcia0