如何在Python中重构类?

2024-03-28 23:50:54 发布

您现在位置: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从{}获得一些方法?在

我在想办一个这样的儿童班:

^{pr2}$

但我不想这样做,因为我不想applcation_service方法对Both_Class可用。在

有没有办法把AppHost_Class中的几个方法映射到Storage_Server_Class?在


Tags: the方法selfserver进程defwithstorage
3条回答

将方法分成三个类,然后根据需要组合。在

#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

另一个选择是用它们始终包含的方法定义这两个类

^{pr2}$

。。。定义要混合和匹配的方法

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

我觉得你想要三个基类。一个用于App内容,一个用于VirtualStorage(和缓存)内容,一个用于Storage(和磁盘)内容。然后,您可以为两个拓扑创建子类,将所需的方法混合在一起。在

对于拓扑1,您有一个既继承了AppVirtualStorage基类的类(并且未经修改地使用Storage基类)。对于拓扑2,创建一个继承VirtualStorageStorage基类的类,并未经修改地使用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

您可能不需要直接在不同拓扑中使用的基类的别名,我只是添加了这些名称以使其看起来更漂亮。在

下面是一个类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)

相关问题 更多 >