0

我想编写执行以下操作的代码:

move = Exit.new(exit_type)
move.scan(person)
move.scan(person)
number_of_people = move.total

当超过 3 人退出时,“exit_type”敲击会移除一个人,所以如果 1 或 2 人退出它保持不变,但如果 3 人或超过 1 人从总数中移除。

我将如何在 Exit 类初始化方法中编写一个“exit_type”方法作为参数?我希望这是有道理的,我非常乐意为您澄清。

先谢谢了。

4

2 回答 2

1

I'm not 100% sure I understand the requirements, but it could just be due to poor naming.

My first spin would simply remove the need for callers to understand implementation details, e.g.,

mover = Exit.remove_only_after(3)

Internally, Exit would construct an instance using a symbol or whatever that could be checked in the scan method. This way you only need to know the behavior you want, instead of a method name, a symbol, or whatever, that actually implements that behavior.

Depending on actual needs, I might refactor it internal to Exit and use some form of strategy pattern, but it might be overkill. It really depends on how different the behaviors are, and how complex they are.

于 2013-11-09T16:05:27.477 回答
1

我希望你想用 exit_type 改变扫描成员函数的性质

class Exit
    attr_accessor :exit_type

    def initialize(exit_type = nil)
        @extit_type = exit_type
        end

    def scan(person)
        send(@exit_type || :default_exit_type_handler)
    end

    def default_exit_type_handler
        #your code
    end

    def exit_type_1
            #your code
    end

现在您可以将退出对象初始化为 move = Exit.new(:exit_type_1)

于 2013-11-09T15:57:05.120 回答