tags:

views:

55

answers:

3

Hello,

I'm importing products and so I have an product import class. It has around 4000 lines so I want to break it up. I've started to break it up but I have difficulty deciding what should be a class and what should not. I think all my methods use three of the same instance variables, so if I separated the methods, I would be passing in these instance variables to each separate class in it's constructor. One way I started breaking this up was by looking at groups of methods that dealt with the same type of product data. For example, some methods worked with product descriptions and some worked with product categories and so I started splitting them into separate classes but then someone told me if these different classes are all using the same three instance variables, they probably should be combined into one class. So, how do I do this? I don't really understand proper class design and was only splitting them up because it was hard to find code in the big class.

If the question here is lost, what are some tips to re-factor a large class. Is what I've purposed above a good solution and should I be re-factoring a class where all the methods use the same three instance variables.

This app is an asp.net app that takes product data from one system A's database and, based on a plethora of settings and configuration the user can choose from, saves the product data to system B's database. For example....

public class ProductImporter
{
    ProductA productA;
    ProductB productB;
    ImportSettings settings;

    public void GetProductADescription()
    {
        //look at the settings and add productB description to productA
        productA.Desc = productB.Desc;
        //....
     }

     //.... tons of methods that all deal with product moving one product to the other

     public void AddProduct()
     {
           //go through all the settings
           productA.Save();
      }

}
A: 

You can

  • Do new OPP design and rewrite your code from old class to new.
  • Do OPP design while object spliting.

And if

  • You have a functions with getting only arguments from stack and dont use class members; you can simply copy, paste this function from old class to new, and refactor it in new enviroment.
Svisstack
A: 

Your goal to refactor classes is a good one. Smaller classes are usually better than bigger classes. However, a bad refactor could put you in a worse place than where you started.

I suggest you spend some time reading about good Java design patterns. You can learn a lot from Head First Design Patterns (http://www.amazon.com/First-Design-Patterns-Elisabeth-Freeman/dp/0596007124 ) among others...

bwawok
Since he doesn't specify a language, "Java design patterns" might not apply. While patterns are somewhat independent of language, a pattern that's common in one language might be rare or even completely inapplicable to another.
Jerry Coffin
+1  A: 

Perhaps you should encapsulate those three instance variables into another class. Code which only deals with those variables can go in the new class; code which also uses other variables can stay in the existing class. You'd then replace the three existing instance variables with one variable of the new type.

That's assuming the three variables really do make sense to encapsulate together. Your description suggests that's the case (as they're often used together) but it's very hard to tell without more information. Without knowing any of the details about what this class is meant to be doing, what the three instance variables represent, or what other state is in the existing class, we're really just guessing.

One thing to consider: would these three variables be logically grouped together other than in your existing class?

Jon Skeet
I've added more info about the application