Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Python Design Patterns - Second Edition

You're reading from   Learning Python Design Patterns - Second Edition

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher
ISBN-13 9781785888038
Length 164 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Gennadiy Zlobin Gennadiy Zlobin
Author Profile Icon Gennadiy Zlobin
Gennadiy Zlobin
Chetan Giridhar Chetan Giridhar
Author Profile Icon Chetan Giridhar
Chetan Giridhar
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introduction to Design Patterns 2. The Singleton Design Pattern FREE CHAPTER 3. The Factory Pattern – Building Factories to Create Objects 4. The Façade Pattern – Being Adaptive with Façade 5. The Proxy Pattern – Controlling Object Access 6. The Observer Pattern – Keeping Objects in the Know 7. The Command Pattern – Encapsulating Invocation 8. The Template Method Pattern – Encapsulating Algorithm 9. Model-View-Controller – Compound Patterns 10. The State Design Pattern 11. AntiPatterns Index

Major aspects of object-oriented programming

Now that we have understood the basics of object-oriented programming, let's dive into the major aspects of OOP.

Encapsulation

The key features of encapsulation are as follows:

  • An object's behavior is kept hidden from the outside world or objects keep their state information private.
  • 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 get and set.
  • 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

The major features of polymorphism are as follows:

  • Polymorphism can be of two types:
    • An object provides different implementations of the method based on input parameters
    • The same interface can be used by objects of different 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

In the following example, strings, tuples, or lists can all be accessed with an integer index. This shows how Python demonstrates polymorphism in built-in types:

a = "John"
b = (1,2,3)
c = [3,4,6,8,9]
print(a[1], b[0], c[2])

Inheritance

The following points help us understand the inheritance process better:

  • 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 B:

class A:
    def a1(self):
        print("a1")

class B(A):
    def b(self):
        print("b")


b = B()
b.a1()

Abstraction

The key features of abstraction are as follows:

  • 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 add() method:

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)

Composition

Composition refers to the following points:

  • 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 B:

class A(object):
    def a1(self):
        print("a1")

class B(object):
    def b(self):
        print("b")
        A().a1()


objectB = B()
objectB.b()
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image