Skip to content

Instantly share code, notes, and snippets.



Last active Jan 8, 2020
What would you like to do?
Notes on oop design

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?

  • 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?

Deck of Cards

  • Creates an array of cards for a deck
  • shuffle
  • deal a card
  • deal a hand

OOP Concepts

  1. Encapsulation
  2. Abstraction
  3. Composition
  4. Inheritance
  5. 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.

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

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

OOP Advice

  1. Practice Practice Practice (Build things using OOP)
  2. Prefer composition over inheritance
  3. Define the problem before jumping into code

Defining the Problem

  1. Ask lots of questions about requirements
    1. How should the app work in a certain case (think about edge cases)
    2. What data is given?
    3. Can I make an assumption about X or Y?
    4. Will data be given in a certain format?
    5. How would I test this app?
  2. List all the requirements (don't worry about grouping things into classes or functions yet)
  3. Break the problem down! Group requirements into related functionality
  4. Decide what classes should exist, what data the classes own, and what operations the classes need
  5. To help organize your thoughts, diagram the classes. Make notes of inheritance and composition
  6. Code!!!

OOP Problem - Tetris

Let's use the above guide to figure out how to implement tetris.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment