flowchart TD
Q1{函式參考<br/>哪個環境的元素較多?} -->|當前環境| Stay[保留原位]
Q1 -->|其他環境| Move[移動函式]
Move --> Q2{移動後<br/>封裝性改善?}
Q2 -->|是| Done[✅ 完成移動]
Q2 -->|否| Reconsider[重新考慮]
Move Function
要讓軟體具備模組特質,相關元素得在一起。
這項任務會動態發生:越了解要做的事,就越知道如何妥善聚集元素。
持續地聚集顯出我們理解的持續提升。
移動函式會發生在其參考其他環境的元素量大於當前環境,而這樣做通常能改善封裝。
這決定不容易,而越困難的選擇往往越不重要。
# Before
class Employee:
def __init__(self, name, email):
self.name = name
self.email = email
def print_employee_details(self):
print(f"Name: {self.name}, Email: {self.email}")
def send_email(employee, message):
print(f"Sending email to {employee.email} with message: '{message}'")
employee = Employee("John Doe", "john@example.com")
send_email(employee, "Welcome to the team!")
# After
class Employee:
def __init__(self, name, email):
self.name = name
self.email = email
def print_employee_details(self):
print(f"Name: {self.name}, Email: {self.email}")
def send_email(self, message):
print(f"Sending email to {self.email} with message: '{message}'")
employee = Employee("John Doe", "john@example.com")
employee.send_email("Welcome to the team!")