vendredi 10 janvier 2020

Refactoring legacy code - interface implementation java - non-interface methods are used

I'm refactoring some legacy code, and have been stuck in a dilemma.

Some context regarding the code-base. It is a dashboard website to which can be configured to monitor some services. There are two modes in which it can be used.

  1. Static:
    1. Configurations for services that need to be monitored are stored in read-only datastores, ex: text, read-only DB.
    2. Onboarding services is done automatically during start-up
    3. The hyper-links to Dynamically onboard new services & edit exisiting services is closed off, and the controllers don't load the pages.
  2. Dynamic:
    1. Configurations for services that need to be monitored are stored in read-write datastore, ex: SQL, online-data-stores etc.
    2. Onboarding services is done manually post start-up in onboarding page.
    3. The hyper-links to Dynamically onboard new services & edit existing services is open and reachable.

I want to refactor the following.

interface Interface {
  def getX()
  def getY()
}

class StaticImpl implements Interface {
  @implement def getX(): some impl
  @implement def getY(): some impl
}

class DynamicImpl extends StaticImpl {
  def putX(x): some impl ( not in interface )
  def putY(y): some impl ( not in interface )
}

@Bean
Interface bean1 = if dynamic: DynamicImpl() else: StaticImpl()

@Bean
DynamicImpl bean2 = DynamicImpl()

bean1 is used for activities which can be called during static and dynamic mode

bean2 is used for activities which is called only during dynamic mode

I have two Ideas for now

  1. move putX, putY into the interface and keep empty implementation in StaticImpl i.e., they do nothing
  2. create new interface for putX, putY and make bean2 null during static mode.

I wanted know if these are good, or if there are any other way to refactor this.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire