Move Class

Refactoring contributed by Gerard M. Davison

You have a class that is in a package that contains other classes that it is not related to in function.

Move the class to a more relavent package. Or create a new package if requried for future use.


class org.davison.ui.TextThing
class org.davison.ui.TextProcessor
class org.davison.log.Logger

depends on 

class org.davison.ui.StringUtil



class org.davison.ui.TextThing
class org.davison.ui.TextProcessor
class org.davison.log.Logger

depends on

class org.davison.util.StringUtil


Motivation

Classes are often created in a packages close to where they are being used, this can make sense until the class starts to be re-used by other parts of the product. The package in question might also have just become too big. (I have a preference that my packages never have more than about 10 classes)

It is often better to move to this class to a package more related to it in form or function. This can help remove complex package level dependencies and make it easier for developers to find and re-use classes.

If there are many dependencies for the class within its own package, then Extract Class could be used first to split out the relavent parts.

Another example where is this used often is to move String resource objects into sub a res package to simplify localisation compilation.

Mechanics

Example

Additional Comments

Consider using a tool to do this. See Chris Sequin's refactory or Woodenchair

--Martin Fowler

| Refactoring Home | | Alphabetical List |