Loading ...
Sorry, an error occurred while loading the content.
 

Refactoring Combo: Extract Command

Expand Messages
  • Alex Chaffee
    Let s say you ve got a big class with a lot of methods -- a dispatcher or controller, say, that s gotten a bit out of control. The methods are real business
    Message 1 of 2 , Dec 4, 2005
      Let's say you've got a big class with a lot of methods -- a dispatcher
      or controller, say, that's gotten a bit out of control. The methods
      are real business methods (or at least they have business logic inside
      them), but they refer to several different entities, so you're not
      sure they belong on any given entity. But you want to get them out of
      the original class and into domain classes.

      Sounds like it might be time for a Command Pattern.

      Usually a Command is used when you want to encapsulate the doing of
      something, without actually doing it right away. But in this case,
      we'll execute it immediately. Don't worry, it will carry its own
      weight by removing the business logic from the original object, and by
      being cleaner and more isolated, and thus more testable. It will also
      be set up so that you can do all the cool Command pattern tricks later
      if you want, like polymorphic dispatch or undo.

      http://www.purpletech.com/content/index.php?itemid=22

      In this article, I walk through the precise sequence of steps to
      perform this refactoring in IntelliJ IDEA (version 5). There are some
      pretty fun keystroke combos you can use to make the whole thing
      relatively painless and automated. (Automating refactorings is better
      than just typing because it's less likely to introduce errors, it's
      got more stable intermediate points so you can safely stop halfway
      through, and most of all, because it makes you feel cool.)

      Comments and kvetches welcome :-)

      - Alex
    • Anthony Williams
      ... Martin Fowler calls this Replace Method with Method Object in the Refactoring book. ... Nice, clear walkthrough. Anthony -- Anthony Williams Software
      Message 2 of 2 , Dec 5, 2005
        Alex Chaffee <alexch@...> writes:

        > Let's say you've got a big class with a lot of methods -- a dispatcher
        > or controller, say, that's gotten a bit out of control. The methods
        > are real business methods (or at least they have business logic inside
        > them), but they refer to several different entities, so you're not
        > sure they belong on any given entity. But you want to get them out of
        > the original class and into domain classes.
        >
        > Sounds like it might be time for a Command Pattern.
        >
        > Usually a Command is used when you want to encapsulate the doing of
        > something, without actually doing it right away. But in this case,
        > we'll execute it immediately.

        Martin Fowler calls this "Replace Method with Method Object" in the
        Refactoring book.

        > http://www.purpletech.com/content/index.php?itemid=22
        >
        > In this article, I walk through the precise sequence of steps to
        > perform this refactoring in IntelliJ IDEA (version 5). There are some
        > pretty fun keystroke combos you can use to make the whole thing
        > relatively painless and automated.

        Nice, clear walkthrough.

        Anthony
        --
        Anthony Williams
        Software Developer
        Just Software Solutions Ltd
        http://www.justsoftwaresolutions.co.uk
      Your message has been successfully submitted and would be delivered to recipients shortly.