The only good option, in cases like this, is refactoring the code. You don't have to change the existing class interface, however. You can create multiple new classes that are designed properly and replace the logic in the original poorly designed class. Then you can refactor the original class to use the new classes internally to perform the functionality. You don't have to do this all at once. As you find that you need a particular bit of logic in a shared library, just refactor that logic and leave the rest untouched. Over time you can, in this way, refactor the whole thing. Unless, of course, it's not that big or you have all the time in the world to refactor the beast. However, usually that's not the case.
For instance, let's say you have the following overly simplified classes:
Public Class OriginalBeast
Private _dependency As New Dependency()
Public Function Method1() As Integer
Return _dependency.Calculate(2)
End Sub
Public Function Method2() As Integer
Return _dependency.Calculate(2)
End Sub
' ...
Public Function Method1027() As Integer
Return _dependency.Calculate(1027)
End Sub
End Class
Public Class Dependency
Public Function Calculate(value As Integer) As Integer
Return value * 2
End Function
End Class
And you want to share the logic in OriginalBeast.Method2
in a class library, you would need to move the Dependency
class to the class library (and likely need to partially refactor it as well). Then you would need to create a new class that contains just the desired methods from the original beast:
Public Interface INice
Function Method2() As Integer
End Interface
Public Class Nice
Implements INice
Public Sub New(dependency As IDependency)
_dependency = dependency
End Sub
Private _dependency As IDependency
Public Function Method2() As Integer Implements INice.Method2
Return _dependency.Calculate(2)
End Function
End Class
Public Interface IDependency
Function Calculate(value As Integer) As Integer
End Interface
Public Class Dependency
Implements IDependency
Public Function Calculate(value As Integer) As Integer Implements IDependency.Calculate
Return value * 2
End Function
End Class
Then, you would need to refactor the original beast to use the class library instead of doing the logic itself:
Public Class OriginalBeast
Public Sub New()
_dependency = New Dependency()
_nice = New Nice(_dependency)
End Sub
Private _dependency As IDependency
Private _nice As INice
Public Function Method1() As Integer
Return _dependency.Calculate(2)
End Sub
Public Function Method2() As Integer
Return _nice.Method2()
End Sub
' ...
Public Function Method1027() As Integer
Return _dependency.Calculate(1027)
End Sub
End Class
Obviously real-world beasts are never that simple and it will likely require a lot of work to refactor even a small part of it, but hopefully that gives you an idea of what I'm talking about.