python中的工厂方法

时间:2011-11-14 22:20:19

标签: python

我正在做以下小项目: https://github.com/AndreaCrotti/project-organizer

简而言之,它旨在更轻松地管理许多不同的项目。 其中一个有用的方法是自动检测我正在处理的项目类型,以正确设置一些命令。

目前我正在使用classmethod“match”函数和一个迭代各种“匹配”的检测函数。 我确信可能有更好的设计,但找不到它。

有什么想法吗?

class ProjectType(object):
    build_cmd = ""

    @classmethod
    def match(cls, _):
        return True


class PythonProject(ProjectType):
    build_cmd = "python setup.py develop --user"

    @classmethod
    def match(cls, base):
        return path.isfile(path.join(base, 'setup.py'))


class AutoconfProject(ProjectType):
    #TODO: there should be also a way to configure it
    build_cmd = "./configure && make -j3"

    @classmethod
    def match(cls, base):
        markers = ('configure.in', 'configure.ac', 'makefile.am')
        return any(path.isfile(path.join(base, x)) for x in markers)


class MakefileOnly(ProjectType):
    build_cmd = "make"

    @classmethod
    def match(cls, base):
        # if we can count on the order the first check is not useful
        return (not AutoconfProject.match(base)) and \
            (path.isfile(path.join(base, 'Makefile')))


def detect_project_type(path):
    prj_types = (PythonProject, AutoconfProject, MakefileOnly, ProjectType)
    for p in prj_types:
        if p.match(path):
            return p()

2 个答案:

答案 0 :(得分:4)

这是合理使用工厂函数作为类方法。

一个可能的改进是让所有类继承自单个父类,这个类具有单个类方法,该方法包含 detect_project_type 中的所有逻辑。

也许这样的事情会起作用:

class ProjectType(object):
    build_cmd = ""
    markers = []

    @classmethod
    def make_project(cls, path):
        prj_types = (PythonProject, AutoconfProject, MakefileOnly, ProjectType)
        for p in prj_types:
            markers = p.markers
            if any(path.isfile(path.join(path, x)) for x in markers):
                return p()

class PythonProject(ProjectType):
    build_cmd = "python setup.py develop --user"
    markers = ['setup.py']

class AutoconfProject(ProjectType):
    #TODO: there should be also a way to configure it
    build_cmd = "./configure && make -j3"
    markers = ['configure.in', 'configure.ac', 'makefile.am']

class MakefileOnly(ProjectType):
    build_cmd = "make"
    markers = ['Makefile']

答案 1 :(得分:2)

对我来说这看起来不错,不过我会做两处改进

1-使用元类自动收集所有ProjectTypes而不是手动列出它们,这样可以避免错误地丢失某些项目类型或错误的订单,例如。

class ProjectTypeManger(type):
    klasses = []
    def __new__(meta, classname, bases, classDict):
        klass = type.__new__(meta, classname, bases, classDict)
        meta.klasses.append(klass)
        return klass

    @classmethod
    def detect_project_type(meta, path):
        for p in meta.klasses:
            if p.match(path):
                return p()

class ProjectType(object):
    __metaclass__ = ProjectTypeManger
    build_cmd = ""

    @classmethod
    def match(cls, _):
        return None

2- match方法应返回对象本身而不是true / false,这样类无论如何都可以配置对象+你可以调用baseclass match方法,例如MakefileOnly可以从AutoconfProject派生,以便它首先检查基类是否有任何匹配,但不确定这种继承是否有意义

class MakefileOnly(AutoconfProject):
    build_cmd = "make"

    @classmethod
    def match(cls, base):
        ret = super(MakefileOnly, cls).match(base)
        if ret is not None:
            return ret
        if path.isfile(path.join(base, 'Makefile')):
            return cls()

        return None