Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中重构类?_Python_Oop - Fatal编程技术网

如何在Python中重构类?

如何在Python中重构类?,python,oop,Python,Oop,我使用类用Python编写了测试代码 测试环境有两种类型的主机—应用程序运行的应用程序主机和存储组件运行的存储主机 我有两个类,每个类代表主机的类型: class AppHost_Class(object): def __init_(self, ip_address): # etc. # This method handles interfacing with the application def application_service(self):

我使用类用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\u Server\u Class
AppHost\u Class
获取一些方法

我在考虑做一个这样的儿童班:

class Both_Class(AppHost_Class, Storage_Server_Class):
但是我不想这样做,因为我不希望
application\u服务
方法对
两个类都可用


是否有一种方法可以将
AppHost\u类中的几个方法映射到
Storage\u Server\u类中?

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

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

对于拓扑1,您有一个从
应用程序
虚拟存储
基类继承的类(并且您使用未修改的
存储
基类)。对于拓扑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
您可能希望在父/基类中定义额外的方法,但除非应用了拓扑,否则它们会引发异常


您是否可以将要共享的方法放入另一个类中,该类继承并将
应用程序\u服务
方法引入到另一个类中,然后继承另一个类和
存储\u服务器\u类
,用于
两个类
。虽然-如果你在两个类中都有可用的方法,这真的很重要吗?那将是一种方法,Jon。但它需要一些代码更改,因为为了制作示例,我简化了它。这些类没有2-3个方法/属性。它更像是30多个方法和30多个属性。这将是一项重大的重新编码工作。
#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