Applying Factory Method and Adapter Patterns for Smart Home Devices

This course is focused on integrating the design patterns we've studied into a practical project: building a smart home system. Throughout this course, you'll learn how to create and adapt various smart home devices using the Factory Method and Adapter patterns. By the end, you will have a solid understanding of how these design patterns can make your smart home system more efficient, modular, and easier to maintain.

In this unit, we explore two essential design patterns: Factory Method and Adapter. These patterns help us create and adapt devices within a smart home system. To effectively implement these patterns, we will build the devices using the Factory Method and then adapt these devices to interact with other parts of the system using the Adapter pattern.

Quick Summary
  1. Factory Method Pattern:

    • Purpose: Encapsulates the creation of objects, making it easier to introduce new object types without altering existing code.
    • Steps:
      • Define a base class (Device).
      • Create specific device classes (Light, Fan) inheriting from the base class.
      • Implement a factory class (DeviceFactory) to generate instances of these devices.
  2. Adapter Pattern:

    • Purpose: Makes incompatible interfaces compatible. Allows objects from different classes to work together.
    • Steps:
      • Define an adapter interface (USPlugInterface).
      • Create adapter classes (LightAdapter, FanAdapter) that implement this interface and adapt the devices (Light, Fan) to the required interface.
Creating Devices with Factory Method Pattern

To begin with, we use the Factory Method pattern in Python to define a base class for our devices, derive specific device classes from this base class, and finally create a factory class responsible for generating instances of these device classes.

Code Implementation

Here is the complete code for implementing the Factory Method pattern:

# Define the Device base class
class Device:
    def operate(self):
        pass

# Define the specific Light and Fan device classes
class Light(Device):
    def operate(self):
        return "Light is turned on"

class Fan(Device):
    def operate(self):
        return "Fan is spinning"

# DeviceFactory class to generate device instances
class DeviceFactory:
    def create_device(self, device_type):
        if device_type == "light":
            return Light()
        elif device_type == "fan":
            return Fan()
        else:
            raise ValueError("Unknown device type")

# Example of using the factory to create devices
factory = DeviceFactory()

device1 = factory.create_device("light")
print(device1.operate())  # Expected Output: Light is turned on

device2 = factory.create_device("fan")
print(device2.operate())  # Expected Output: Fan is spinning

The factory object is created using the DeviceFactory class. We then use the create_device method to create instances for "light" and "fan". The respective operate methods are called, displaying "Light is turned on" and "Fan is spinning" as the outputs.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal