Encapsulate What Varies
One of the most common challenges in software development is dealing with change. Requirements evolve, technologies advance, and user needs also change. Therefore, it is crucial to write code that can adapt without causing a ripple effect of modifications throughout your program or application. This is where the principle of Encapsulate What Varies comes into play.
What does it mean?
The idea behind this principle is straightforward: isolate the parts of your code that are most likely to change and encapsulate them. By doing so, you create a protective barrier that shields the rest of your code from these elements that are subject to change. This encapsulation allows you to make changes to one part of your system without affecting others.
Techniques for achieving encapsulation
As we introduced, encapsulation helps in data hiding and exposing only the necessary functionalities. Here, we will present key techniques that enhance encapsulation in Python: polymorphism and the getters and setters techniques.
Favor Composition Over Inheritance
Program to Interfaces, Not Implementations
Techniques for interfaces
In Python, interfaces can be implemented using two primary techniques: abstract base classes (ABCs) and protocols.
Loose Coupling
What does it mean?
Loose coupling refers to minimizing the dependencies between different parts of a program. In a loosely coupled system, components are independent and interact through well-defined interfaces, making it easier to make changes to one part without affecting others.
Techniques for loose coupling
Two primary techniques for achieving loose coupling are dependency injection and the observer pattern. Dependency injection allows a component to receive its dependencies from an external source rather than creating them, making it easier to swap or mock these dependencies. The observer pattern, on the other hand, allows an object to publish changes to its state so that other objects can react accordingly, without being tightly bound to each other.
An example – a message service
In Python, you can achieve loose coupling by using dependency injection. Let’s see a simple example involving a MessageService class:
1. First, we define the MessageService class as follows:
class MessageService: def __init__(self, sender): self.sender = sender def send_message(self, message): self.sender.send(message)
As you can see, the class will be initialized by passing a sender object to it; that object has a
send method to allow sending messages.
2. Second, let’s define an EmailSender class:
class EmailSender: def send(self, message): print(f"Sending email: {message}")
3. Third, let’s define an SMSSender class:
class SMSSender: def send(self, message): print(f"Sending SMS: {message}")
4. Now we can instantiate MessageService using an EmailSender object and use it to send a message. We can also instantiate MessageService using an SMSSender object instead. We add code to test both actions as follows:
if __name__ == "__main__": email_service = MessageService(EmailSender()) email_service.send_message("Hello via Email") sms_service = MessageService(SMSSender()) sms_service.send_message("Hello via SMS")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2023-08-15 Go - Predeclared identifiers, Keywords, Operators and punctuation