简体   繁体   English

python中的面向对象设计

[英]Object oriented design in python

I have a base functionality 我有基本功能

 class Foo(object):


   def method(self):
       return True # or False based on custom logic for each class that will implement this class

   def almost_common_method(self, params, force):
      if self.method():
         params.bleh = 'foo bar'
      else:
        params.bleh = 'not foo bar'
      if force:
         foobar = some_foo_bar(model_name = 'foo bar')
       else:
         default = Default()


   def common_method(self, params, force):
     return self.almost_common_method(params, force)

So, I am thinking of making a base class... where method raises NotImplementedError I implement the common_method() in the base class as this will be shared across all the classes inheriting the base class? 因此,我正在考虑制作一个基类...方法引发NotImplementedError我在基类中实现common_method(),因为这将在继承基类的所有类之间共享? But is there a way to define almost_common_method() in base class.. THe variable names are common.. but it is the string assignments which will differ.. across different implementations of this base class.? 但是,有没有一种方法可以在基类中定义almost_common_method() 。.变量名是常见的..但是在此基类的不同实现之间,字符串分配会有所不同。

Define almost_common_method in the base class as well, but parameterize the values you assign using class attributes: almost_common_method在基类中定义almost_common_method ,但是使用类属性对您分配的值进行参数化:

class Base(object):
    BLEH_IF_METHOD = 'foo bar'
    BLEH_IF_NOT_METHOD = 'not foo bar'

    def almost_common_method(self, params, force):
        if self.method():
            params.bleh = self.BLEH_IF_METHOD
        else:
            params.bleh = self.BLEAH_IF_NOT_METHOD
        if force:
            foobar = some_foo_bar(model_name = 'foo bar')
        else:
            default = Default()

Then in the child class, just override the values of the two class attributes and use the inherited method as-is. 然后在子类中,只需重写两个类属性的值,并按原样使用继承的方法。

class Child(Base):
    BLEH_IF_METHOD = 'child bleh'
    BLEH_IF_NOT_METHOD = 'not child bleh'

This is a good fit for the template method pattern . 这非常适合模板方法模式 Break the string out into a separate method: 将字符串分成一个单独的方法:

class FooBase(object):
    @property
    def bleh(self):
        raise NotImplementedError()
        # consider making this an abstract base class with the abc module
    def almost_common_method(self, params, force):
        params.bleh = self.bleh
        if force:
            foobar = some_foo_bar(model_name = 'foo bar')
         else:
            default = Default()

class Foo(FooBase):
    @property
    def bleh(self):
        if self.method():
            return 'foo bar'
        else:
            return 'not foo bar'

Each subclass can override bleh with its own separate implementation. 每个子类都可以使用其自己的单独实现覆盖bleh

The use of @property is optional, and should generally be used only if you expect bleh to act like an attribute (that is, it doesn't change "by magic", fetching it is relatively cheap and free of visible side effects, etc.). 使用@property是可选的,如果你期望一般应只用于bleh表现得像一个属性(即,它不会“魔法”的变化,取它是相对便宜和无可见的副作用等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM