01 Object Oriented Design Notes
01 Object Oriented Design Notes
Basics of OOP
Object-oriented programming is a paradigm based on the con-
cept of wrapping pieces of data, and behavior related to that
data, into special bundles called objects, which are construct-
ed from a set of “blueprints”, deXned by a programmer, called
classes.
Objects, classes
Do you like cats? I hope you do because I’ll try to explain the
OOP concepts using various cat examples.
This is a UML class diagram. You’ll see a lot of such diagrams in the book.
9 Introduction to OOP / Basics of OOP
All cats also behave similarly: they breathe, eat, run, sleep and
meow. These are the class’s methods. Collectively, Xelds and
methods can be referenced as the members of their class.
Class hierarchies
Everything Xne and dandy when we talk about one class. Nat-
urally, a real program contains more than a single class. Some
of these classes might be organized into class hierarchies. Let’s
Xnd out what that means.
Say your neighbor has a dog called Fido. It turns out, dogs
and cats have a lot in common: name, sex, age, and color are
attributes of both dogs and cats. Dogs can breathe, sleep and
run the same way cats do. So it seems that we can deXne the
base Animal class that would list the common attributes and
behaviors.
UML diagram of a class hierarchy. All classes in this diagram are part of
the Animal class hierarchy.
Pillars of OOP
Object-oriented programming is based on four pillars, con-
cepts that differentiate it from other programming paradigms.
Abstraction
Most of the time when you’re creating a program with OOP,
you shape objects of the program based on real-world objects.
However, objects of the program don’t represent the origi-
nals with 100% accuracy (and it’s rarely required that they do).
Instead, your objects only model attributes and behaviors of
real objects in a speciXc context, ignoring the rest.
Encapsulation
To start a car engine, you only need to turn a key or press a
button. You don’t need to connect wires under the hood, rotate
the crankshaft and cylinders, and initiate the power cycle of
the engine. These details are hidden under the hood of the
car. You have only a simple interface: a start switch, a steering
wheel and some pedals. This illustrates how each object has
an interface—a public part of an object, open to interactions
with other objects.
16 Introduction to OOP / Pillars of OOP
Inheritance
Inheritance is the ability to build new classes on top of exist-
ing ones. The main beneXt of inheritance is code reuse. If you
want to create a class that’s slightly different from an existing
one, there’s no need to duplicate code. Instead, you extend the
existing class and put the extra functionality into a resulting
subclass, which inherits Xelds and methods of the superclass.
Polymorphism
Let’s look at some animal examples. Most Animals can make
sounds. We can anticipate that all subclasses will need to over-
ride the base makeSound method so each subclass can emit
the correct sound; therefore we can declare it abstract right
away. This lets us omit any default implementation of the
method in the superclass, but force all subclasses to come up
with their own.
Imagine that we’ve put several cats and dogs into a large bag.
Then, with closed eyes, we take the animals one-by-one out of
19 Introduction to OOP / Pillars of OOP
the bag. After taking an animal from the bag, we don’t know
for sure what it is. However, if we cuddle it hard enough, the
animal will emit a speciXc sound of joy, depending on its con-
crete class.
The program doesn’t know the concrete type of the object con-
tained inside the a variable; but, thanks to the special mech-
anism called polymorphism, the program can trace down the
subclass of the object whose method is being executed and
run the appropriate behavior.
You can’t just Xnd a pattern and copy it into your program,
the way you can with off-the-shelf functions or libraries. The
pattern is not a speciXc piece of code, but a general concept
for solving a particular problem. You can follow the pattern
details and implement a solution that suits the realities of your
own program.
Classi@cation of patterns
Design patterns differ by their complexity, level of detail and
scale of applicability to the entire system being designed. I like
the analogy to road construction: you can make an intersec-
tion safer by either installing some trafXc lights or building an
entire multi-level interchange with underground passages for
pedestrians.
26 Introduction to Design Patterns / What’s a Design Pattern?
The most basic and low-level patterns are often called idioms.
They usually apply only to a single programming language.