1/35
Looks like no tags are added yet.
creational patterns
deal with object creation, making a system independent of how its objects are created, composed, and represented
singleton
ensures a class only has one instance and provides a global access point to that instance
singleton in use
_instance = None
def __new__(cls):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instancesingleton example
logging system, database connection
pros of singleton
ensures a class only has a single instance
ensures the instance has a global access point
cons of singleton
violates Single Responsibility principle
can mask bad design
unit testing may be difficult

Which pattern is this a UML diagram of?
singleton
applicability of singleton
use when a class in your program should have just a single instance available to all clients
factory
creates objects without specifying their concrete classes
factory in use
class Animal:
speak()
class Dog(Animal):
speak()
class Cat(Animal):
speak()
class AnimalFactory:
@staticmethod
create_animal()factory example
GUI components, database connection factory
pros of factory
avoids tight coupling between the creator and concrete products
follows single responsibility and open-closed principles
con of factory
requires lots of new subclasses, which can make code complicated

Which pattern is this a UML diagram of?
factory
applicability of factory
use when you want to save system resources by reusing existing objects instead of rebuilding them each time
abstract factory
extension of factory pattern that provides an interface for creating families of related objects without specifying their concrete classes
abstract factory in use
class Dog():
speak()
class Cat():
speak()
class PetFactory:
@staticmethod
get_pet()
class FoodFactory:
@staticmethod
get_food()
class AbstractFactory:
@staticmethod
get_factory()abstract factory examples
cross-platform GUI frameworks, automobile manufacturing
pros of abstract factory
ensures products from factory are compatible with each other
avoid tight coupling between the creator and concrete products
follows single responsibility and open-closed principles
con of abstract factory
many new interfaces and classes may complicate the code

What pattern is this a UML diagram of?
abstract factory
applicability of abstract factory
use when your code needs to work with various families of related products, but you don’t want it to depend on the concrete classes of those products
builder
separates object construction from its representation, allowing step-by-step creation of complex objects
builder in use
class Computer:
def __init__(self)
class ComputerBuilder:
def __init__(self)
def set_cpu(self, cpu)
def set_memory(self, memory)
def build(self)builder examples
complex objects, meal ordering systems
pros of builder
can construct objects step-by-step
can reuse construction code
follows single responsibility principle
con of builder
new classes increase complexity

Which pattern is this a UML diagram of?
builder
applicability of builder
use when you want your code to be able to create different representations of some product (for example, stone and wooden houses)
prototype
creates a copy of an existing object, rather than instantiating new ones
prototype in use
import copy
class Car:
def __init__(self, brand, model)
def clone(self)
return copy.deepcopy(self)prototype examples
game development, document templates
pros of prototype
can clone objects without coupling to their concrete classes
can get rid of repeated initialization code
can produce complex objects more conveniently
provides alternative to inheritance
con of prototype
harder to clone complex objects that have circular references

Which pattern is this a UML diagram of?
prototype
applicability of prototype
use when your code shouldn’t depend on the concrete classes of objects that you need to copy