Design Patterns in Python
- 2 hours on-demand video
- 10 downloadable resources
- Full lifetime access
- Access on mobile and TV
- Certificate of Completion
Get your team access to 4,000+ top Udemy courses anytime, anywhere.Try Udemy for Business
- Design Patterns in Python
- Common GOF Design Principles
- Quality Software Design Standards
- A Common Software Engineering Vocabulary
Factory Design Pattern
The Factory Pattern is a creational pattern that defines an Interface for creating an object and defers instantiation until runtime.
Used when you don't know how many or what type of objects will be needed until or during runtime.
Abstract Factory Design Pattern
The Abstract Factory Pattern adds an abstract layer over multiple factory method implementations.
The Abstract Factory contains or composites one or more than one factory method.
Builder Design Pattern
The Builder Pattern is a creational pattern whose intent is to separate the construction of a complex object from its representation so that you can use the same construction process to create different representations.
The Builder Pattern tries to solve,
How can a class create different representations of a complex object?
How can a class that includes creating a complex object be simplified?
The Builder and Factory patterns are very similar in the fact they both instantiate new objects at run time. The difference is when the process of creating the object is more complex, so rather than the Factory returning a new instance of ObjectA, it could call the builders director construct method ObjectA.construct(). Both return an Object.
Parts of the Builder Pattern
Product - The Product being built
Concrete Builder - Build the concrete product. Implements the IBuilder interface
Builder Interface - The Interface which the Concrete builder should implement
Director - Has a construct method which when called creates a customised product
Prototype Design Pattern
Prototype design pattern is good for when creating a new objects may require more resources than you want to use or have available, versus just making a new copy in memory. Eg, A file you've downloaded from a server may be large, but since it is already in memory, you could just clone it, and work on the new copy independently of the original.
In the prototype patterns interface, you create a static clone method that should be implemented by all classes that use the interface. How the clone method is implemented in the concrete class is up to you. You will need to decide whether a shallow or deep copy is required.
A shallow copy, copies and creates new references 1 level deep,
A deep copy, copies and creates new references for all levels.
In Python you have mutable objects such as Lists, Dictionaries, Sets and any custom Objects you have created. A shallow copy, will create new copies of the objects with new references in memory, but the underlying data will point to the same location as the original copy. Be sure to test your implementation that the copy method you use works as you expect.
Command Design Pattern
The command pattern is a behavioural design pattern, in which an abstraction exists between an object that invokes a command, and the object that performs it.
The components if the Command Design Pattern are,
Receiver - The Object that will receive and execute the command
Invoker - Which will send the command to the receiver
Command Object - Itself, which implements an execute, or action method, and contains all required information to execute it
Client - The application or component which is aware of the Receiver, Invoker and Commands
Eg, a button, will call the Invoker, which will call a pre registered Commands execute method, which the Receiver will perform.
A Concrete Class will delegate a request to a command object, instead of implementing the request directly. Using a command design pattern allows you to separate concerns a little easier and to solve problems of the concerns independently of each of the layers. eg, logging the execution of a command and it's outcome.
Uses: GUI Buttons, menus Macro recording Multi level undo/redo networking - send whole command objects across a network, even as a batch parallel processing or thread pools, transactional behaviour Wizards
Notes: The receiver object should manages it's own state, not the command object There can be one or more invokers which can execute the command at a later date.
Chain of Responsibility Design Pattern
Chain of responsibility pattern is a behavioural pattern used to achieve loose coupling in software design. In this example, a request from a client is passed to a chain of objects to process them. The objects in the chain will decide how to process them and/or pass them to the next in the chain. The objects can also modify the next in the chain if for example you wanted to run objects in a recursive manner.
An interface with next and has_next methods.
next returns the next object in the aggregate(collection, list)
has_next returns a value, usually a boolean indicating if the iterable is at the end of the list or not.
The benefits of using the Iterator pattern is that the client, can traverse an aggregate without needing to understand it's internal representation and data structures.
The mediator pattern is a behavioural pattern that defines an object that encapsulates how a set of objects interact.
With the mediator pattern, communication between objects is encapsulated within a mediator object.
Objects communicate through the mediator rather than directly with each other.
Adapter Design Pattern
The adapter design pattern solves these problems:
How can a class be reused that does not have an interface that a client requires?
How can classes that have incompatible interfaces work together?
How can an alternative interface be provided for a class?
In this lecture, I have 2 classes, they don't share the same interface. The client requires it's objects to use an already standardised interface.
So we need to create an adapter, that wraps the incompatible object, but implements the standardised interface.
Decorator Design Pattern
The decorator pattern is a structural pattern, that allows you to attach additional responsibilities to an object at run time.
The decorator pattern is used in both the Object Oriented and Functional paradigms.
The decorator pattern is different than the Python language feature of Python Decorators in it's syntax, but the application of it is the same, in the way that it is essentially a wrapper.
The Decorator pattern adds extensibility, without modifying the original function.
The Composite design pattern,
allows you to represent individual entities and groups of entities in the same manner.
is a structural design pattern that lets you compose objects into a tree.
is great if you need the option of swapping hierarchal relationships around.
makes it easier for you to add new kinds of components
provides flexibility of structure
conform to the Single Responsibility Principle in the way that it separates the aggregation of objects from the features of the object.
Examples of using the Composite Design Pattern can be seen in a filesystem directory structure, where you can swap the hierarchy of folders, and in a drawing program where you can group, un-group and transform objects, and multiple objects at the same time.
The proxy design pattern is a class functioning as an interface to another class or object.
A proxy could be for anything, such as a network connection, an object in memory, a file, or anything else you need to provide an abstraction between.
It is a wrapper called by a client to access the real underlying object.
Additional functionality can be provided at in the proxy abstraction if required. eg,
The proxy should implement the subject interface as much as practicable so that the proxy and subject appear identical to the client.
The Proxy Pattern may occasionally also be referred to as Monkey Patching or Object Augmentation.
- Basic Knowledge of Python 3
- An OS such as Windows 10, Mac OSX or Linux
- An IDE such as Visual Studio Code, PyCharm Community, Vim or Just the command line.
This course is about common GOF (Gang Of Four) Design Patterns implemented in Python.
A Design Pattern is a description or template that can be repeatedly applied to a commonly recurring problem in software design.
You will find a familiarity with Design Patterns very useful when planning, discussing, developing, managing and documenting your applications from now on and into the future.
You will learn these Design Patterns
Command - Undo/Redo
Chain of Responsibility
There are many downloadable resources attached to this course. I demonstrate and discuss all the code examples throughout the video lectures.
Thanks for joining my course on Design Patterns in Python.
- Python Developers
- Software Architects
- Software Engineers