根据标题,我知道这是天生的可怕的设计,作为一个家长应该知道自己的孩子什么。然而,我的情况是
我需要所有的子类都具有可重复的行为,但也需要父类能够返回子类的实例,这将需要一个完整的重构,但是如何正确地设计它呢
我在这里尝试过使用组合,唯一的问题是所有类都必须声明一个显式的公共API来使用公共功能,并且将来的每个子类都需要声明它
class BasePage(object):
# Some Very Common Behaviour goes here.
# Nothing product specific, just selenium specific
class ProductBasePage(BasePage):
# Reusable behaviour here for the product
# Some of this behaviour in the parent does web-app navigation
# which merits (quite rightly) to return instances of children
# pages, for example (CustomersPage, DashboardPage, ProductsPage)
# but this is clearly flawed and inheritance is not the answer?
# but how do I keep the functionality that all subclasses need
# without then declaring it explicitly in every one of them?
class CustomersPage(ProductBasePage):
# Customer specific behaviour
# Should have access the common functionality implicitly from the parent
Customer页面和扩展产品页面的所有其他页面应该能够使用ProductBasePage的所有子类都应该具有的许多公共功能,而不需要每个子类都明确定义该行为。但是,可重用方法返回ProductBasePages子类的实例,因为它们的操作保证了这种行为
那么我该如何做到这一点呢?我认为继承不是这里的答案,但是如何在不在每个类中显式声明的情况下获得公共功能的可重用性呢
这个问题的解决方案也应该避免循环Python导入依赖关系
只需将这些可重用方法放入一个单独的类中,并使它们
static
。然后子类可以隐式地调用这些方法如果不想使用静态方法,可以使用普通函数
相关问题 更多 >
编程相关推荐