Creational Design Patterns

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/35

encourage image

There's no tags or description

Looks like no tags are added yet.

36 Terms

1
New cards

creational patterns

deal with object creation, making a system independent of how its objects are created, composed, and represented

2
New cards

singleton

ensures a class only has one instance and provides a global access point to that instance

3
New cards

singleton in use

_instance = None

def __new__(cls):
     if not cls._instance:
          cls._instance = super().__new__(cls)
     return cls._instance


4
New cards

singleton example

logging system, database connection

5
New cards

pros of singleton

  • ensures a class only has a single instance

  • ensures the instance has a global access point


6
New cards

cons of singleton

  • violates Single Responsibility principle

  • can mask bad design

  • unit testing may be difficult


7
New cards
<p>Which pattern is this a UML diagram of?</p>

Which pattern is this a UML diagram of?

singleton

8
New cards

applicability of singleton

use when a class in your program should have just a single instance available to all clients

9
New cards

factory

creates objects without specifying their concrete classes

10
New cards

factory in use

class Animal:
     speak()

class Dog(Animal):
     speak()

class Cat(Animal):
     speak()

class AnimalFactory:
     @staticmethod
     create_animal()


11
New cards

factory example

GUI components, database connection factory

12
New cards

pros of factory

  • avoids tight coupling between the creator and concrete products

  • follows single responsibility and open-closed principles


13
New cards

con of factory

requires lots of new subclasses, which can make code complicated

14
New cards
<p>Which pattern is this a UML diagram of?</p>

Which pattern is this a UML diagram of?

factory

15
New cards

applicability of factory

use when you want to save system resources by reusing existing objects instead of rebuilding them each time

16
New cards

abstract factory

extension of factory pattern that provides an interface for creating families of related objects without specifying their concrete classes

17
New cards

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()


18
New cards

abstract factory examples

cross-platform GUI frameworks, automobile manufacturing

19
New cards

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


20
New cards

con of abstract factory

many new interfaces and classes may complicate the code

21
New cards
<p>What pattern is this a UML diagram of?</p>

What pattern is this a UML diagram of?

abstract factory

22
New cards

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

23
New cards

builder

separates object construction from its representation, allowing step-by-step creation of complex objects

24
New cards

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)


25
New cards

builder examples

complex objects, meal ordering systems

26
New cards

pros of builder

  • can construct objects step-by-step

  • can reuse construction code

  • follows single responsibility principle


27
New cards

con of builder

new classes increase complexity

28
New cards
<p>Which pattern is this a UML diagram of?</p>

Which pattern is this a UML diagram of?

builder

29
New cards

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)

30
New cards

prototype

creates a copy of an existing object, rather than instantiating new ones

31
New cards

prototype in use

import copy

class Car:
     def __init__(self, brand, model)
     
     def clone(self)
	return copy.deepcopy(self)


32
New cards

prototype examples

game development, document templates

33
New cards

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


34
New cards

con of prototype

harder to clone complex objects that have circular references

35
New cards
<p>Which pattern is this a UML diagram of?</p>

Which pattern is this a UML diagram of?

prototype

36
New cards

applicability of prototype

use when your code shouldn’t depend on the concrete classes of objects that you need to copy