Lesson 1
Applying Factory Method and Adapter Patterns for Smart Home Device
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:

Python
1# Define the Device base class 2class Device: 3 def operate(self): 4 pass 5 6# Define the specific Light and Fan device classes 7class Light(Device): 8 def operate(self): 9 return "Light is turned on" 10 11class Fan(Device): 12 def operate(self): 13 return "Fan is spinning" 14 15# DeviceFactory class to generate device instances 16class DeviceFactory: 17 def create_device(self, device_type): 18 if device_type == "light": 19 return Light() 20 elif device_type == "fan": 21 return Fan() 22 else: 23 raise ValueError("Unknown device type") 24 25# Example of using the factory to create devices 26factory = DeviceFactory() 27 28device1 = factory.create_device("light") 29print(device1.operate()) # Expected Output: Light is turned on 30 31device2 = factory.create_device("fan") 32print(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.

Adapting Devices with Adapter Pattern

Now, let's ensure our devices can interact with other parts of the system that expect a different interface. Specifically, we create an adapter to make our devices compatible with a method called plug_into_us_socket.

Code Implementation

Here is the complete code for implementing the Adapter pattern:

Python
1# Define the Adapter interface 2class USPlugInterface: 3 def plug_into_us_socket(self): 4 pass 5 6# LightAdapter class to adapt Light to work with USPlugInterface 7class LightAdapter(USPlugInterface): 8 def __init__(self, light): 9 self.light = light 10 11 def plug_into_us_socket(self): 12 return self.light.operate() 13 14# Example of using the LightAdapter 15light = Light() 16light_adapter = LightAdapter(light) 17print(light_adapter.plug_into_us_socket()) # Expected Output: Light is turned on 18 19# FanAdapter class to adapt Fan to work with USPlugInterface 20class FanAdapter(USPlugInterface): 21 def __init__(self, fan): 22 self.fan = fan 23 24 def plug_into_us_socket(self): 25 return self.fan.operate() 26 27# Example of using the FanAdapter 28fan = Fan() 29fan_adapter = FanAdapter(fan) 30print(fan_adapter.plug_into_us_socket()) # Expected Output: Fan is spinning

We create a Light and a Fan object and then instances of LightAdapter and FanAdapter by passing the respective device objects to their constructors. The plug_into_us_socket method is called on the adapters, which internally call the operate methods of the devices.

Conclusion

Implementing the Factory Method and Adapter patterns in our smart home system allows us to create a variety of devices in a clean and reusable manner. The Factory Method pattern helps in managing the creation of device objects, while the Adapter pattern ensures that these devices can seamlessly interact with different interfaces. This structured approach makes our smart home system more modular, flexible, and easier to maintain and extend in the future.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.