Introduction and Lesson Goal

Today's mission involves using multiple Object-Oriented Programming (OOP) principles to tackle complex tasks. When principles like Encapsulation, Abstraction, Polymorphism, and Composition are blended, the resulting code becomes streamlined and easier to manage.

Our goal is to dissect two real-world examples, gaining insights into how these principles can seamlessly orchestrate solutions.

Real-life Example 1: Building an Online Library System

Let's design an online library system, as we aim to reinforce our understanding of Encapsulation and Polymorphism. Encapsulation will help us protect the attributes of books, members, and transactions, making sure they are accessible in a controlled manner. Polymorphism will demonstrate its power by enabling a single interface to represent different underlying forms, such as digital and print versions of books.

# Base class for different types of library users
class Member
  def initialize(name)
    @name = name
  end

  def check_out_book(book)
    puts "#{@name} checked out #{book.get_book_type} book #{book.title}."
  end
end

# Base class for different types of books
class Book
  attr_reader :title

  def initialize(title)
    @title = title
  end

  def get_book_type
    raise NotImplementedError, "Subclasses must implement the get_book_type method"
  end
end

# Inherits from Book, represents a digital book
class DigitalBook < Book
  def get_book_type
    "Digital"
  end
end

# Inherits from Book, represents a physical book
class PhysicalBook < Book
  def get_book_type
    "Physical"
  end
end

# Library class that manages members and books
class Library
  def initialize
    @members = []
    @books = []
  end

  def add_member(member)
    @members << member
  end

  def add_book(book)
    @books << book
  end
end

my_library = Library.new

alice = Member.new("Alice")
bob = Member.new("Bob")

my_library.add_member(alice)
my_library.add_member(bob)

digital_book = DigitalBook.new("The Ruby Handbook")
physical_book = PhysicalBook.new("Learning Ruby Design Patterns")

my_library.add_book(digital_book)
my_library.add_book(physical_book)

alice.check_out_book(digital_book)  # Prints: Alice checked out Digital book The Ruby Handbook.
bob.check_out_book(physical_book)   # Prints: Bob checked out Physical book Learning Ruby Design Patterns.

In this code snippet, Encapsulation is observed clearly through the class structures and the controlled access to their attributes. Polymorphism is vividly illustrated by how both DigitalBook and PhysicalBook classes inherit from the Book class but provide their own implementations of the get_book_type method. This setup allows objects of DigitalBook and PhysicalBook to be used interchangeably when a book's type needs to be identified, demonstrating Polymorphism's capability to work with objects of different classes through a common interface.

  • Encapsulation ensures that details about members and books are well-contained within their respective classes.
  • Polymorphism showcases flexibility by treating different book types uniformly, making the system more adaptive and scalable.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal