0

I am trying to clean up some code, and I see there are three classes that so the very same thing except for all three some have more properties than others and need to save more or less properties than the other two.

I have something like

//class 1
public class GenerateQKey
{
      public void DoSomething()
      {
          //this one uses a specific class with some parameters
          var KeySet = new GenerateQSetOfK()
              {
                   KeyName = "some key";
                   KeyChain = "Some key chain";
                   KeyManufacturer = "Manufacturer";
              }

           someCall.Save(KeySet);
      }
}

ANd then i have another one:

//class 2
public class GenerateDifferentKey
{
      public void DoSomething()
      {
          //this one uses a specific class with some parameters + more
          var KeySet = new GenerateDifferenKey()
              {
                   Name = "some key";
                   Chain = "Some key chain";
                   Manufacturer= "Manufacturer";
                   Price = 3.00m;
              }

           someOtherCall.Save(KeySet);
      }
}

And there is another but the same principle.. they are all doing basically the same thing just using different API and saving to a different place. I would like to refactor this in a really nice way but i am not sure what it would be.

Maybe have like a manager class that receives either a GenerateQKey or a GenerateDifferentKey and upon determining the type of key call the specific api, but what about the parameters?, should i create a base class?.

I am trying to become a better developer and your input would really help towards that goal.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
jedgard
  • 868
  • 3
  • 23
  • 41
  • You could use [Inheritance](http://www.tutorialspoint.com/csharp/csharp_inheritance.htm) to clean it up, but as a word of caution that can get messy. Take a look at [Composition over Inheritance](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – disappointed in SO leadership Aug 22 '14 at 00:45
  • `I see there are three classes that so the very same thing except for all three some have more properties than others and need to save more or less properties than the other two.` sorry what? – Jonesopolis Aug 22 '14 at 00:58
  • I think the OP means that there are 3 classes that serve the same purpose, but class A has a property of `foo`, class B has properties `foo`, and `bar`, and class C has properties `foo`, `bar`, `baz`. Another option would be using the super class and only assigning the values you need. Just be very careful when reading properties, if they aren't assigned to you'll get an object reference error. – disappointed in SO leadership Aug 22 '14 at 01:10

1 Answers1

1

To me this sounds like something the Template method pattern could solve.

Your template method would have the steps that are similar between all subclasses and the methods for "using different API" and "saving to a different place" would be abstract methods that each sub class would implement differently.

public abstract class SharedClass
{
   abstract CallApi();
   abstract Save();

   public void MyTemplateMethod()
   {
     // shared steps...

     // Call abstract templated method
     this.CallApi();

     // more shared steps...

     // Call abstract templated method
     this.Save(); 
   }
}

public class ChildClass1 : SharedClass
{
  public void CallApi()
  {
     // I use API #1
  }

  public void Save()
  {
    // I save by doing it way #1
  }
}

public class ChildClass2 : SharedClass
{
  public void CallApi()
  {
     // I use API #2
  }

  public void Save()
  {
    // I save by doing it way #2
  }
}
JRP
  • 181
  • 2
  • 7