I don’t have anyone to ask / find out / get a hint, so I’m writing here ... There are such attempts to write a specific case, in ruby and MVC I’m completely green, so I ask you to treat this kind of code with understanding.
The task is that the object of the Handler class must contain ONLY public members add_handler, calculate, print_out . It must implement the behavior:
controller = HandlerController.new controller.add_handler(empty_handler) controller.add_handler(min_handler) controller.add_handler(max_handler)
When the calculator method is called with one argument, the calculation is performed on the transferred array in all handler objects transferred to the controller.
controller.calculator([1,2,3,4,5]) controller.print_out #Handler min return: 1 #Handler max return: 5 #Handler 'empty name' return "method is empty"
When calling a method with two arguments (the second argument is an array of object names), we calculate only by the objects whose name is passed.
controller.calculator([1,2,3],['min','max']) controller.print_out
class Handler attr_reader :name def initialize(name='empty name') @name = name end def calculator(array) 'Calculate method is empty' end end class HandlerController attr_reader :arr_operands def initialize @arr_operands = Array.new end def add_handler(operand) @arr_operands.push operand end def calculator(array) @arr_operands.each { |operand| puts operand } end def print_out # примерно так, наверное... puts "Handler" + arr_operands[name] + "return:" + arr_result[result] end end class MinHandler < Handler end class MaxHandler < Handler end class AverageHandler < Handler end class PremaxHandler < Handler end class SumHandler < Handler end empty_handler = Handler.new('max') max_handler = MaxHandler.new min_handler = MinHandler.new p empty_handler controller = HandlerController.new #p controller controller.add_handler(empty_handler) #p controller puts controller.arr_operands.name #puts empty_handler.name #puts controller.calculate([1,2,3]) Poke me into errors (or potential errors) and tell me how to implement correctly, please. It would be possible to see the implementation in other languages. Javascript for example.
calculate. Sometimes it is acalculator. - anoamprint_outbit more about thecalculateandprint_out? The first one considers and stores the result in the instance, the second one displays the result. Right? - anoam