Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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_Dependency Inversion - Fatal编程技术网

Python 对象如何在不违反依赖倒置原则的情况下进行通信?

Python 对象如何在不违反依赖倒置原则的情况下进行通信?,python,oop,dependency-inversion,Python,Oop,Dependency Inversion,我正在构建一个路径规划器,它将帮助人们通过RPG控制台游戏规划路径 我想创建一个表格,显示整个阶段的每个步骤。然而,实际上,这是一个看起来很糟糕的OOP设计;它违反了各种原则,我认为它甚至不是合法的OOP问题显然是,表是一个神类。 因此,我决定重写它,同时牢记正确的OOP原则。我想把表分成多个类 我的问题是我需要不同的对象来相互交谈。然而,我的解决方案是始终使用合成。这打破了依赖原则和单一责任原则 以下是存储玩家步骤的主表: class PathTable(object): ''' A

我正在构建一个路径规划器,它将帮助人们通过RPG控制台游戏规划路径

我想创建一个表格,显示整个阶段的每个步骤。然而,实际上,这是一个看起来很糟糕的OOP设计;它违反了各种原则,我认为它甚至不是合法的OOP问题显然是,
是一个神类。

因此,我决定重写它,同时牢记正确的OOP原则。我想把
分成多个类


我的问题是我需要不同的对象来相互交谈。然而,我的解决方案是始终使用合成。这打破了依赖原则和单一责任原则

以下是存储玩家步骤的主表:

class PathTable(object):
    ''' A path table. '''

    def __init__(self):
    # table is a list of dicts, representing rows
        self._table = []

    @property
    def table(self):
        return self._table

    def addStep(self, step):
        ''' Adds a step to the table. '''
        self._table.append(step)

    def rmStep(self):
        ''' Removes the last step from the table. '''
        try:
            del self._table[-1]
        except:
            raise IndexError('Tried to remove item from an empty table.')
现在,我创建了一个负责接受和验证用户输入的
InputManager

class InputManager(object):
    ''' Responsible for managing user input. '''
    def __init__(self):
        pass
    def addFight(self, position):
        ''' Add a 'fight' at table[position]. '''
        # table._table[position]['input'] = 'fight'
        # Need to somehow edit a particular row in the Table.
但是,现在我不知道如何访问
PathTable.\u table[position]
。不违反各种OO设计原则

这是令人沮丧的,因为
InputManager
的整个工作就是访问
PathTable
。但是我不能使用composition将
InputManager
放在
PathTable
中,因为这是一种糟糕的设计

实现这一目标的干净方法是什么?


我是一名初学者,我正在努力学习。

首先在您的
路径表
类中添加对编辑步骤行的支持:

class PathTable(object):
    def __init__(self):
        self._table = []

    ## NB : This defeats the whole point of making `_table` private
    #@property
    #def table(self):
    #    return self._table

    def add_step(self, step):
        ''' Adds a step to the table. '''
        self._table.append(step)

    def rm_step(self):
        ''' Removes the last step from the table. '''
        return self._table.pop()

    def update_step(self, position, key, value):
        self._table[position][key] = value
然后将
路径表
实例传递给您的
输入管理器

class InputManager(object):
    ''' Responsible for managing user input. '''
    def __init__(self, path_table):
        self._path_table = path_table

    def add_fight(self, position):
        ''' Add a 'fight' at table[position]. '''
        self._path_table.update_step(position, 'input', 'fight')

为什么不将
getStep
方法添加到
PathTable
?另外,
rmStep
的内容可以替换为
self.\u table.pop()
“我的问题是我需要各种对象相互通信”=>看起来像是中介的候选对象,这样可以吗,因为现在InputManager依赖于PathTable?中介或控制器怎么可能(这就是您的
InputManager
主要是什么)不依赖于它必须使用的对象吗?但是如果您重新阅读代码,
InputManager
不依赖于
PathTable
类,它只需要使用暴露相同(隐式)对象进行初始化接口为
PathTable
.FWIW,需要依赖于域模型层对象的UI层对象-否则它们如何完成其工作?-,您不希望的是依赖于UI层的域模型层。