Object Oriented Programming
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)
What is OOP?
Object Oriented Programming - modeling some system or process by using objects or classes.
OOP Example - Poker
What objects exist in poker?
- Deck of cards
- Poker hand
- Poker game
- Discard pile
What operations would be on each object? Which objects would use other objects?
Deck of Cards
- Creates an array of cards for a deck
- deal a card
- deal a hand
- 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.
Inheritance Example - Vehicle
- 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
- Practice Practice Practice (Build things using OOP)
- Prefer composition over inheritance
- Define the problem before jumping into code
Defining the Problem
- 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
OOP Problem - Tetris
Let's use the above guide to figure out how to implement tetris.