mardi 1 décembre 2015

Refactoring and How to deal with parameters or fields

I have a Spring-project based on Hybris. There I have a class which has many Dependency Injections and a lot of code. Now to aim at better design and the Single Responsibility paradigma I want to divide this class, so I have many classes, which should only do one thing.

My problem is now, how to deal with all the fields and the parameters. What is designwise the best approach to pass them to a new class. For example I have a method, in which I call an autowired service, or I create a new instance of something else in another method and use it in this method.

Now when I want to create a new class and transfer the method in it, should I use setter/getter or a constructor with parameters or what? What would be the best practice?

And also here a (very short) example:

@Autowired
private MyService myService;

public void doSomething(final List<String> values){
    //something is happening
    MyItem myItem = new MyItem();
    doSomethingElse(myItem);
}

private void doSomethingElse(MyItem myItem){
    MyOrder myOrder = myService.getOrder(myItem);
    //and so on
}

Now the thing is, that my private void is actually very long, so I want to make a new class for it. So again should I make a new class with both these methods, since they are together doing one task? Or only the private method, since it's doing a longer algorithm. And how do I deal with all my parameters? Last but not least, for example I have written a new class lets call it MyClassDoSomethingElse for the private method. Should I then just exchange doSomethingElse(myItem) with MyClassDoSomethingElse myClass = new MyClassDoSomethingElse(). (Or of course, when I pass parameters in the constructor it would be MyClassDoSomethingElse myClass = new MyClassDoSomethingElse(myItem, getMyService())). And then of course I would just call myClass.doSomethingElse().

So to sum it up my question isn't about how I should do it, but what is the best design-approach for doing that?

Aucun commentaire:

Enregistrer un commentaire