Refactoring: Refactorings/Remove Middle Man
Jump to navigation
Jump to search
If a class is doing too much simple delegation, it should allow direct access to the delegate class.
Mechanics
- Create an accessor for the delegate
- For each client use of a delegate method, remove the method from the server and replace the call in the client to call the method on the delegate.
- Test after each method
Example
Before
class Person
def initialize(department)
@department = department
end
def manager
@department.manager
end
end
class Department
attr_reader :manager
def initialize(manager)
@manager = manager
end
end
# client code
manager = john.manager
After
class Person
attr_reader :department
def initialize(department)
@department = department
end
end
class Department
attr_reader :manager
def initialize(manager)
@manager = manager
end
end
# client code
manager = john.department.manager