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
    '''
    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()

上記は一段の例ですが、簡単には編集できず、簡単に拡張できません。简单,很容易修正和扩展呢?

上記はいくつかのサンプル コードですが、非常に見苦しく、メンテナンスも簡単ではなく、拡張も簡単ではありません。新しいペット、アクション、目標を追加することです。たくさんのコードを書く必要があります。それを再構築して、非常にシンプルで、変更や拡張が非常に簡単に見えるようにする方法は?

4

3 に答える 3

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()
于 2013-09-27T01:40:20.000 に答える
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'
于 2013-09-27T01:57:55.903 に答える