如何使这些代码更简单,更容易维护?

时间:2013-09-26 06:52:10

标签: python design-patterns refactoring

# -*- coding: utf-8 -*-
'''Please let code becomes much simpler and easier to maintain.
'''


def process(pet, action, target):
    '''
    >>> process('dog', 'eat', 'bone')
    ok
    >>> process('dog', 'eat', 'ball')
    faild
    >>> process('dog', 'play', 'ball')
    yes
    >>> process('dog', 'play', 'bone')
    ok
    >>> process('dolphin', 'play', 'ball')
    good
    >>> process('dolphin', 'play', 'bone')
    faild
    >>> process('dolphin', 'eat', 'bone')
    faild
    >>> process('dog', 'play', 'mouse')
    opps
    >>> process('cat', 'catch', 'mouse')
    Traceback (most recent call last):
        ...
    Exception
    '''
    if pet == 'dog':
        if action == 'eat':
            if target == 'bone':
                print 'ok'
            elif target == 'ball':
                print 'faild'
            else:
                raise Exception()
        elif action == 'play':
            if target == 'bone':
                print 'ok'
            elif target == 'ball':
                print 'yes'
            else:
                print 'opps'
        else:
            raise Exception()
    elif pet == 'dolphin':
        if action == 'eat':
            if target == 'bone':
                print 'faild'
            elif target == 'ball':
                print 'faild'
            else:
                raise Exception()
        elif action == 'play':
            if target == 'bone':
                print 'faild'
            elif target == 'ball':
                print 'good'
            else:
                raise Exception()
        else:
            raise Exception()
    else:
        raise Exception()

if __name__ == '__main__':
    import doctest
    doctest.testmod()

上面是一段示例代码,但它写的很丑陋,不容易维护,不容易扩展,增加一种新的宠物,动作,目标, 需要写很多的代码,如何重构一下,让它变的看起来很简单,很容易修改和扩展呢?

以上是一些示例代码,但它写得非常难看,不易维护,不易扩展 - 添加新的宠物,动作,目标。 我需要写很多代码;如何重建它使它看起来非常简单,很容易修改和扩展?

3 个答案:

答案 0 :(得分:1)

可能这样的事情可能会有所帮助。虽然它没有处理“失败”的情况。

class Pet:
    def __init__(self, name, action_targets):
        self.name = name
        self.action_targets = action_targets

pets = (
            Pet('dog', (('eat','bone'), ('play','bone'), ('play', 'ball')) ),
            Pet('dolphin', (('play', 'ball')) )
        ) 

for p in pets:
    if pet == p.name and (action, target) in p.action_targets:
        return 'ok'

raise Exception()

答案 1 :(得分:1)

这是一种在没有类的情况下执行它并处理“失败”情况的方法,尽管它更长:

from sets import Set

ALLOWABLE_ITEMS = (('dog', 'eat', 'bone'),
                   ('dog', 'play', 'bone'),
                   ('dog', 'play', 'ball'),
                   ('dolphin', 'play', 'ball'),
                  )

PETS, ACTIONS, TARGETS = Set(), Set(), Set()

for item in ALLOWABLE_ITEMS:
    PETS.add(item[0])
    ACTIONS.add(item[1])
    TARGETS.add(item[2])

if (pet, action, target) in ALLOWABLE_ITEMS:
    print 'ok'
elif  pet not in PETS or action not in ACTIONS or target not in TARGETS:
    raise Exception()
else:
    print 'faild'

答案 2 :(得分:0)

# -*- coding: utf-8 -*-
'''Please let code becomes much simpler and easier to maintain.
'''


def process(pet, action, target):
    '''
    >>> process('dog', 'eat', 'bone')
    ok
    >>> process('dog', 'eat', 'ball')
    faild
    >>> process('dog', 'play', 'ball')
    yes
    >>> process('dog', 'play', 'bone')
    ok
    >>> process('dolphin', 'play', 'ball')
    good
    >>> process('dolphin', 'play', 'bone')
    faild
    >>> process('dolphin', 'eat', 'bone')
    faild
    >>> process('dog', 'play', 'mouse')
    opps
    >>> process('cat', 'catch', 'mouse')
    Traceback (most recent call last):
        ...
    Exception
    '''

    def print_ok():
        print 'ok'

    def print_yes():
        print 'yes'

    def print_good():
        print 'good'

    def print_faild():
        print 'faild'

    def print_opps():
        print 'opps'

    def raise_exception():
        raise Exception()

    args_map = {}
    args_map[('dog', 'eat', 'bone')] = print_ok
    args_map[('dog', 'eat', 'ball')] = print_faild
    args_map[('dog', 'play', 'bone')] = print_ok
    args_map[('dog', 'play', 'ball')] = print_yes
    args_map[('dog', 'play', 'mouse')] = print_opps

    args_map[('dolphin', 'eat', 'bone')] = print_faild
    args_map[('dolphin', 'eat', 'ball')] = print_faild
    args_map[('dolphin', 'play', 'bone')] = print_faild
    args_map[('dolphin', 'play', 'ball')] = print_good

    func = args_map.get((pet, action, target), raise_exception)
    func()

if __name__ == '__main__':
    import doctest
    doctest.testmod()