[英]How can I refactor classes in Python?
我有用Python編寫的測試代碼,使用類。
測試環境有兩種類型的主機 - 運行應用程序的應用程序主機和運行存儲組件的存儲主機。
我有兩個類,每個類代表主機的類型:
class AppHost_Class(object):
def __init_(self, ip_address):
# etc.
# This method handles interfacing with the application
def application_service(self):
# This method handles the virtual storage component
def virtual_storage(self):
# This method handles caching
def cache_handling(self):
class Storage_Server_Class(object):
def __init_(self, ip_address):
# This method handles interfacing with the storage process
def storage_handling(self):
# This method handles interfacing with the disk handling processes
def disk_handling(self):
問題是拓撲可能會發生變化。
拓撲#1是這樣的: - 應用程序主機運行*應用程序進程*虛擬存儲進程*緩存進程
我當前的測試代碼處理拓撲#1
但是,我們還想支持另一個拓撲(拓撲#2)
應用主機運行
存儲主機運行
我如何重構類,以便對於拓撲1,類及其方法是相同的,但對於拓撲2, Storage_Server_Class
從AppHost_Class
獲取一些方法?
我想要創建一個這樣的子類:
class Both_Class(AppHost_Class, Storage_Server_Class):
但我不想這樣做,因為我不想讓applcation_service
方法可用來Both_Class
。
有沒有辦法將AppHost_Class
的幾個方法AppHost_Class
到Storage_Server_Class
?
下面是一個B類的例子,它只分享了A類中定義的一個方法:
class A:
def a1(self):
pass
def a2(self):
pass
class B:
def __init__(self, instance_of_a):
self.a2 = instance_of_a.a2
a = A()
B(a)
聽起來像你想要三個基類。 一個用於App
東西,一個用於VirtualStorage
(和緩存)的東西,一個用於Storage
(和磁盤)的東西。 然后,您可以為兩種拓撲創建子類,將所需的方法混合在一起。
對於拓撲1,您有一個繼承App
和VirtualStorage
基類的類(並且您使用未修改的Storage
基類)。 對於拓撲2,您將創建一個繼承自VirtualStorage
和Storage
基類的類,並使用未修改的App
基類。
示例代碼:
class App:
def do_app_stuff(self):
pass
class VirtualStorage:
def do_virtual_storage_stuff(self):
pass
class Storage:
def do_storage_stuff(self):
pass
# topology 1
class Top1App(App, VirtualStorage):
pass
Top1Storage = Storage
# topology 2
Top2App = App
class Top2Storage(VirtualStorage, Storage):
pass
您可能不需要在不同拓撲中直接使用的基類的別名,我只是把它們扔進去使它看起來更好。
將方法拆分為三個類,然后根據需要組合。
#class NetworkObject(object): # Python 2.7
class NetworkObject:
def __init__(self, ip_address):
self.ip_address = ip_address
class AppHost(NetworkObject):
def application_service(self):
print('app service', self.ip_address)
class Storage_Server(NetworkObject):
def storage_handling(self):
print('storage handler', self.ip_address)
def disk_handling(self):
print('disk handler', self.ip_address)
class Foo(object):
def virtual_storage(self):
print('virtual storage', self.ip_address)
def cache_handling(self):
print('cache handling', self.ip_address)
topology_1, topology_2 = True, False
# Topology 1
if topology_1:
class AppHost_Class(AppHost, Foo):
pass
class Storage_Server_Class(Storage_Server):
pass
# Topology 2
if topology_2:
class AppHost_Class(AppHost):
pass
class Storage_Server_Class(Storage_Server, Foo):
pass
另一種選擇是使用它們將始終包含的方法來定義兩個類,
#class NetworkObject(object): # Python 2.7
class NetworkObject:
def __init__(self, ip_address):
self.ip_address = ip_address
class A(NetworkObject):
def application_service(self):
print('app service', self.ip_address)
class B(NetworkObject):
def storage_handling(self):
print('storage handler', self.ip_address)
def disk_handling(self):
print('disk handler', self.ip_address)
...定義您想要混合和匹配的方法
def virtual_storage(self):
print('virtual storage', self.ip_address)
def cache_handling(self):
print('cache handling', self.ip_address)
...並有條件地將方法添加到類中
topology = 1
if topology == 1:
A.virtual_storage = virtual_storage
A.cache_handling = cache_handling
if topology == 2:
B.virtual_storage = virtual_storage
B.cache_handling = cache_handling
您可能希望在父/基類中定義額外方法,但除非已應用拓撲,否則它們會引發異常
#class NetworkObject(object): # Python 2.7
class NetworkObject:
def __init__(self, ip_address):
self.ip_address = ip_address
def virtual_storage(self):
raise NotImplementedError
def cache_handling(self):
raise NotImplementedError
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.