In this chapter, we will go through the basics of object-oriented programming and discuss the object-oriented design principles in detail. This will get us prepared for the advanced topics covered later in the book. This chapter will also give a brief introduction to the concept of design patterns so that you will be able to appreciate the context and application of design patterns in software development. Here we also classify the design patterns under three main aspects—creational, structural, and Behavioral patterns. So, essentially, we will cover the following topics in this chapter:
Understanding object-oriented programming
Discussing object-oriented design principles
Understanding the concept of design patterns and their taxonomy and context
Discussing patterns for dynamic languages
Classifying patterns—creational pattern, structural pattern, and behavioral pattern
Before you start learning about design patterns, it's always good to cover the basics and go through object-oriented paradigms in Python. The object-oriented world presents the concept of objects that have attributes (data members) and procedures (member functions). These functions are responsible for manipulating the attributes. For instance, take an example of the
Car object. The
Car object will have attributes such as
steering wheel and
coordinates, and the methods would be
accelerate() to increase the speed and
takeLeft() to make the car turn left. Python has been an object-oriented language since it was first released. As they say, everything in Python is an object. Each class instance or variable has its own memory address or identity. Objects, which are instances of classes, interact among each other to serve the purpose of an application under development. Understanding the core concepts of object-oriented programming involves understanding the concepts of objects, classes, and methods.
They represent entities in your application under development.
Entities interact among themselves to solve real-world problems.
For example, Person is an entity and Car is an entity. Person drives Car to move from one location to the other.
Classes define objects in attributes and behaviors. Attributes are data members and behaviors are manifested by the member functions
Classes consist of constructors that provide the initial state for these objects
Classes are like templates and hence can be easily reused
For example, class
Person will have attributes
age and member function
gotoOffice() that defines his behavior for travelling to office for work.
They represent the behavior of the object
Methods work on attributes and also implement the desired functionality
A good example of a class and object created in Python v3.5 is given here:
class Person(object): def __init__(self, name, age): #constructor self.name = name #data members/ attributes self.age = age def get_person(self,): # member function return "<Person (%s, %s)>" % (self.name, self.age) p = Person("John", 32) # p is an object of type Person print("Type of Object:", type(p), "Memory Address:", id(p))
Clients can't change the object's internal state by directly acting on them; rather, clients request the object by sending messages. Based on the type of requests, objects may respond by changing their internal state using special member functions such as
In Python, the concept of encapsulation (data and method hiding) is not implicit, as it doesn't have keywords such as public, private, and protected (in languages such as C++ or Java) that are required to support encapsulation. Of course, accessibility can be made private by prefixing
__in the variable or function name.
Polymorphism can be of two types:
In Python, polymorphism is a feature built-in for the language. For example, the
+operator can act on two integers to add them or can work with strings to concatenate them
a = "John" b = (1,2,3) c = [3,4,6,8,9] print(a, b, c)
Inheritance indicates that one class derives (most of its) functionality from the parent class.
Inheritance is described as an option to reuse functionality defined in the base class and allow independent extensions of the original software implementation.
Inheritance creates hierarchy via the relationships among objects of different classes. Python, unlike Java, supports multiple inheritance (inheriting from multiple base classes).
In the following code example,
class A is the base class and
class B derives its features from
class A. So, the methods of
class A can be accessed by the object of
class A: def a1(self): print("a1") class B(A): def b(self): print("b") b = B() b.a1()
It provides you with a simple interface to the clients, where the clients can interact with class objects and call methods defined in the interface
It abstracts the complexity of internal classes with an interface so that the client need not be aware of internal implementations
In the following example, internal details of the
Adder class are abstracted with the
class Adder: def __init__(self): self.sum = 0 def add(self, value): self.sum += value acc = Adder() for i in range(99): acc.add(i) print(acc.sum)
It is a way to combine objects or classes into more complex data structures or software implementations
In composition, an object is used to call member functions in other modules thereby making base functionality available across modules without inheritance
In the following example, the object of
class A is composited under
class A(object): def a1(self): print("a1") class B(object): def b(self): print("b") A().a1() objectB = B() objectB.b()
Now, let's talk about another set of concepts that are going to be crucial for us. These are nothing but the object-oriented design principles that will act as a toolbox for us while learning design patterns in detail.
What this means in simple language is, when you develop your software application, make sure that you write your classes or modules in a generic way so that whenever you feel the need to extend the behavior of the class or object, then you shouldn't have to change the class itself. Rather, a simple extension of the class should help you build the new behavior.
For example, the open/close principle is manifested in a case where a user has to create a class implementation by extending the abstract base class to implement the required behavior instead of changing the abstract class.
Existing classes are not changed and hence the chances of regression are less
It also helps maintain backward compatibility for the previous code
The inversion of control principle states that high-level modules shouldn't be dependent on low-level modules; they should both be dependent on abstractions. Details should depend on abstractions and not the other way round.
This principle suggests that any two modules shouldn't be dependent on each other in a tight way. In fact, the base module and dependent module should be decoupled with an abstraction layer in between.
This principle also suggests that the details of your class should represent the abstractions. In some cases, the philosophy gets inverted and implementation details itself decide the abstraction, which should be avoided.
This principle talks about software developers writing their interfaces well. For instance, it reminds the developers/architects to develop methods that relate to the functionality. If there is any method that is not related to the interface, the class dependent on the interface has to implement it unnecessarily.
For example, a
Pizza interface shouldn't have a method called
Veg Pizza class based on the
Pizza interface shouldn't be forced to implement this method.
It forces developers to write thin interfaces and have methods that are specific to the interface
It helps you not to populate interfaces by adding unintentional methods
This principle says that when we develop classes, it should cater to the given functionality well. If a class is taking care of two functionalities, it is better to split them. It refers to functionality as a reason to change. For example, a class can undergo changes because of the difference in behavior expected from it, but if a class is getting changed for two reasons (basically, changes in two functionalities), then the class should be definitely split.
Advantages of this design principle are as follows:
This principle is pretty straightforward in the sense that it says when application developers write derived classes, they should extend the base classes. It also suggests that the derived class should be as close to the base class as possible so much so that the derived class itself should replace the base class without any code changes.
Design patterns were first introduced by GoF (Gang of Four), where they mentioned them as being solutions to given problems. If you would like to know more, GoF refers to the four authors of the book, Design Patterns: Elements of Reusable Object-Oriented Software. The book's authors are Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword by Grady Booch. This book covers software engineering solutions to the commonly occurring problems in software design. There were 23 design patterns first identified, and the first implementation was done with respect to the Java program language. Design patterns are discoveries and not an invention in themselves.
They are language-neutral and can be implemented across multiple languages
They are dynamic, as new patterns get introduced every now and then
They are open for customization and hence useful for developers
Initially, when you hear about design patterns, you may feel the following:
It's a panacea to all the design problems that you've had so far
It's an extraordinary, specially clever way of solving a problem
Many experts in software development world agree to these solutions
There's something repeatable about the design, hence the word pattern
You too must have attempted to solve the problems that a design patterns intends to, but maybe your solution was incomplete, and the completeness that we're looking for is inherent or implicit in the design pattern. When we say completeness, it can refer to many factors such as the design, scalability, reuse, memory utilization, and others. Essentially, a design pattern is about learning from others' successes rather than your own failures!
Interestingly, design patterns are solutions to known issues. So they can be very much used in analysis or design, and as expected, in the development phase because of the direct relation in the application code.
They are reusable across multiple projects
The architectural level of problems can be solved
They are time-tested and well-proven, which is the experience of developers and architects
They have reliability and dependence
Not every piece of code or design can be termed as a design pattern. For example, a programming construct or data structure that solves one problem can't be termed as a pattern. Let's understand terms in a very simplistic way below:
Types or classes are objects at runtime.
Variables can have type as a value and can be modified at runtime. For example,
a = 5and
a = "John", the
avariable is assigned at runtime and type also gets changed.
Dynamic languages have more flexibility in terms of class restrictions.
For example, in Python, polymorphism is built into the language, there are no keywords such as
protectedand everything is public by default.
Represents a case where design patterns can be easily implemented in dynamic languages.
The classification of patterns is done based primarily on how the objects get created, how classes and objects are structured in a software application, and also covers the way objects interact among themselves. Let's talk about each of the categories in detail in this section.
They work on the basis of how objects can be created
They isolate the details of object creation
Code is independent of the type of object to be created
An example of a creational pattern is the Singleton pattern.
They design the structure of objects and classes so that they can compose to achieve larger results
The focus is on simplifying the structure and identifying the relationship between classes and objects
They focus on class inheritance and composition
An example of a behavior pattern is the Adapter pattern.
In this chapter, you learned about the basic concepts of object-oriented programming, such as objects, classes, variables, and features such as polymorphism, inheritance, and abstraction with code examples.
We are also now aware of object-oriented design principles that we, as developers/architects, should consider while designing an application.
Finally, we went on to explore more about design patterns and their applications and context in which they can be applied and also discussed their classifications.
At the end of this chapter, we're now ready to take the next step and study design patterns in detail.