Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8422127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:21:10+00:00 2026-06-10T03:21:10+00:00

I am refactoring some legacy code that was doing close to the same thing

  • 0

I am refactoring some legacy code that was doing close to the same thing over and over via a case statement:

switch(identifier)
    case firstIdentifier: 
        (SomeCast).SetProperties(Prop1,Prop2,Prop3);
        break;
    ...
    case anotherIdentifier:
        (SomeDifferentCast).SetProperties(Prop1, Prop2, Prop3);
        break;

So, I tried to create a unique interface so that this could become

(SameInterfaceCast).SetProperties(Prop1,Prop2,Prop3);

However, I then found that some items do not even use all of the properties. So, I began to think of something more like this:

if(item is InterfaceForProp1)
    (InterfaceForProp1).SetProp1(Prop1);
if(item is InterfaceForProp2)
    (InterfaceForProp2).SetProp2(Prop2);
if(item is InterfaceForProp3)
    (InterfaceForProp3).SetProp3(Prop3);

and you could create a class like this:

public class MyClassUsesProp2And3 : InterfaceForProp2, InterfaceForProp3

However, I am afraid that I am overly fragmenting this code and it could balloon too much. Maybe I should not be too fearful of what will essentially be one method interfaces, but I wanted to see if I am missing a design pattern before going down this path? (the only ones that popped into my mind but were not quite the right fit were the Decorator or Composite patterns)

UPDATE

All properties are unique types.

Ultimately, this is a form of dependency injection. The code is too messed up to use something like Ninject right now, but eventually I might even be able to get rid of some of these and use an injection container. There is also currently some logic that is being done beyond just setting a variable. This is all legacy code, and I am just trying to clean it up little by little.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:21:12+00:00Added an answer on June 10, 2026 at 3:21 am

    Basically you want to make the actors (type with setProp methods) the same type ‘Actor’ AND make the properties (prop1…n) the same type ‘Prop’. This would reduce your code to

    actor.setProp(prop)
    

    If you want to avoid using instanceOf, the only way I can think of is to go with the Visitor pattern, making ‘Prop’ the visitor. I would also use template method to make my life easier. In Java I would make it look like this (for two actual kinds of Prop).

    class Actor {
    
        protected void set(Prop1 p1) {
            // Template method, do nothing
        }
    
        protected void set(Prop2 p2) {
            // Template method, do nothing
        }
    
        public void setProp(Prop p) {
            p.visit(this);
        }
    
        public interface Prop {
            void visit(Actor a);
        }
    
        public static Prop makeComposite(final Prop...props ) {
            return new Prop() {
    
                @Override
                public void visit(final Actor a) {
                    for (final Prop p : props) {
                        p.visit(a);
                    }
                }
            };
        }
    
        public static class Prop1 implements Prop {
            public void visit(Actor a) {
                a.set(this);
            }
        }
    
        public static class Prop2 implements Prop {
            public void visit(Actor a) {
                a.set(this);
            }
        }
    }
    

    This allows you to do stuff like this:

        ConcreteActor a = new ConcreteActor();
        Prop p = Actor.makeComposite(new ConcreteProp1(42), new ConcreteProp2(-5));
        a.setProp(p);
    

    …which is super nice!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing some refactoring in some legacy code I've found in a project. This is
Whilst refactoring some legacy C++ code I found that I could potentially remove some
I am refactoring some legacy code. The app was not using querystrings. The previous
I was refactoring some code and found there are two places that can be
I'm refactoring some legacy code, and I keep running into this Object. I would
I'm working on some refactoring of legacy code to move styles to a stylesheet
I'm refactoring some code that currently looks like this: $$('#wrapper > div').each(function(e){ if((e.id!='header') &&
I've been refactoring some code that was originally using the Messenger in MVVM Foundation
I am currently refactoring some of my old code that is pretty terrible. I
I am working on some legacy code and have come across something that I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.