101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Python

    Receive aemail containing the next unit.
    • Refreshing Python Basics
      • 1.1Python Data Structures
      • 1.2Syntax and Semantics
      • 1.3Conditionals and Loops
    • Introduction to Object-Oriented Programming
      • 2.1Understanding Class and Objects
      • 2.2Design Patterns
      • 2.3Inheritance, Encapsulation, and Polymorphism
    • Python Libraries
      • 3.1Numpy and Matplotlib
      • 3.2Pandas and Seaborn
      • 3.3SciPy
    • Handling Files and Exception
      • 4.1Reading, writing and manipulating files
      • 4.2Introduction to Exceptions
      • 4.3Handling and raising Exceptions
    • Regular Expressions
      • 5.1Introduction to Regular Expressions
      • 5.2Python’s re module
      • 5.3Pattern Matching, Substitution, and Parsing
    • Databases and SQL
      • 6.1Introduction to Databases
      • 6.2Python and SQLite
      • 6.3Presentation of Data
    • Web Scraping with Python
      • 7.1Basics of HTML
      • 7.2Introduction to Beautiful Soup
      • 7.3Web Scraping Case Study
    • Python for Data Analysis
      • 8.1Data cleaning, Transformation, and Analysis using Pandas
      • 8.2Data visualization using Matplotlib and Seaborn
      • 8.3Real-world Data Analysis scenarios
    • Python for Machine Learning
      • 9.1Introduction to Machine Learning with Python
      • 9.2Scikit-learn basics
      • 9.3Supervised and Unsupervised Learning
    • Python for Deep Learning
      • 10.1Introduction to Neural Networks and TensorFlow
      • 10.2Deep Learning with Python
      • 10.3Real-world Deep Learning Applications
    • Advanced Python Concepts
      • 11.1Generators and Iterators
      • 11.2Decorators and Closures
      • 11.3Multithreading and Multiprocessing
    • Advanced Python Concepts
      • 12.1Generators and Iterators
      • 12.2Decorators and Closures
      • 12.3Multithreading and Multiprocessing
    • Python Project
      • 13.1Project Kick-off
      • 13.2Mentor Session
      • 13.3Project Presentation

    Introduction to Object-Oriented Programming

    Understanding Design Patterns in Python

    design pattern in object-oriented software development

    Design pattern in object-oriented software development.

    Design patterns are reusable solutions to common problems that occur in software design. They represent best practices and can speed up the development process by providing tested, proven development paradigms. In this unit, we will explore some of the most commonly used design patterns in Python.

    Singleton Design Pattern

    The Singleton pattern restricts the instantiation of a class to a single instance. It is used in logging, driver objects, caching, thread pools, and database connections.

    class Singleton: __instance = None @staticmethod def getInstance(): if Singleton.__instance == None: Singleton() return Singleton.__instance def __init__(self): if Singleton.__instance != None: raise Exception("This class is a singleton!") else: Singleton.__instance = self

    Factory Design Pattern

    The Factory pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

    class Dog: def __init__(self, name): self._name = name def speak(self): return "Woof!" class Cat: def __init__(self, name): self._name = name def speak(self): return "Meow!" def get_pet(pet="dog"): pets = dict(dog=Dog("Hope"), cat=Cat("Peace")) return pets[pet] d = get_pet("dog") print(d.speak()) c = get_pet("cat") print(c.speak())

    Prototype Design Pattern

    The Prototype pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.

    import copy class Prototype: def __init__(self): self._objects = {} def register_object(self, name, obj): self._objects[name] = obj def unregister_object(self, name): del self._objects[name] def clone(self, name, **attr): obj = copy.deepcopy(self._objects.get(name)) obj.__dict__.update(attr) return obj

    Builder Design Pattern

    The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations.

    class Director: __builder = None def setBuilder(self, builder): self.__builder = builder def getCar(self): car = Car() # First goes the body body = self.__builder.getBody() car.setBody(body) # Then engine engine = self.__builder.getEngine() car.setEngine(engine) # And four wheels i = 0 while i < 4: wheel = self.__builder.getWheel() car.attachWheel(wheel) i += 1 return car

    By understanding and implementing these design patterns, you can significantly improve the efficiency of your Python programming and the quality of your code.

    Test me
    Practical exercise
    Further reading

    Hey there, any questions I can help with?

    Sign in to chat
    Next up: Inheritance, Encapsulation, and Polymorphism