我使用类编写了用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)。
- Application processes
- Virtual storage processes
- Cache processes
- Storage processes
- Disk handling processes
我如何重构类,以便对于Topology 1,类及其方法是相同的,但是对于Topology 2,Storage_Server_Class从AppHost_Class获得一些方法?
我想做一个像这样的儿童课:
class Both_Class(AppHost_Class, Storage_Server_Class):但是我不想这样做,因为我不希望applcation_service方法对Both_Class可用。
有没有办法将AppHost_Class中的几个方法映射到Storage_Server_Class中
发布于 2017-07-20 23:01:52
下面是一个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)发布于 2017-07-20 23:16:40
我觉得你想要三个基类。一个用于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您可能不需要在不同的拓扑中直接使用的基类的别名名称,我只是把它们加进去,以使它看起来非常漂亮。
发布于 2017-07-20 23:18:35
将方法分成三个类,然后根据需要合并。
#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 NotImplementedErrorhttps://stackoverflow.com/questions/45226153
复制相似问题