Code Refactoring 5

Replace Constructor with Factory MethodIn this part of the code refactoring tutorial, I will show you how to replace a constructor with a factory method in 2 code examples.

I also received a challenge from one of you guys that I will solve. The challenge involves how to create a singleton factory method. It sounds more complicated than it is, but by watching this you’ll learn a lot. Here are some topics you may want to brush up on: Java Reflection, Factory Design Pattern, and the Singleton Design Pattern.

If you like videos like this, it helps to tell Google with a click here [googleplusone]

Code From the Video

Customer2.java

Athlete.java

16 thoughts on “Code Refactoring 5”

  1. Hi, another great video.

    I just though I would put a quick note on this video to help others avoid the heart ache I went through.

    Despite your 500+ videos I was swearing about you (sorry) and how crappy your code was (sorry again, lol) when mine did not work.

    Note for others: these methods rely on the package qualified class name. If your code sits in a package (com.mypack ) then your classes will all need that slapped onto them. medaltype becomes “com.mypack.”+medaltype.

    Thanks for the unexpected homework – as always I learned something.

    Cheers,
    Neil

    1. Thank you 🙂 Yes, I have to assume that people are using the same setup that I use and that sometimes causes problems. Sorry about that.

      I’m very used to getting messages about how my code sucks so that doesn’t bother me. Most of the time the error was caused because of a missing tag, quote or semicolon. Some times it is caused because my improved code sucked that day 🙂

      This tutorial has generated more grief than normal because quite literally I’m one of the few people on Earth that ever spent this much time teaching refactoring. Type “code refactoring tutorial” into Google with the quotes. Almost every returned link is either mine or from some website that copied exactly what is on my site. That put a giant bulls eye on my back for every programming expert in the world to attack.

      I’m glad you got it fixed and I know you meant no ill will with your comment.

      Thanks for the tip
      Derek

      1. I was worried, until the last line of the message, that my British sarcasm had been misinterpreted.

        Just for absolute clarity I have never had any problems with your approach or your code. And I really appreciate these tutorials.

        As you say above everyone else stops at the standard basic Java tutorials which I have done to death. I was having real problems knowing where to go next and where/what/how to improve. As a “noob” I literally didn’t know what questions to ask and I certainly “didn’t know what I didn’t know”. Then you started this series, which is great.

        As a student I should not just be copying your code, but should be extending it, and finding its limits. With this comes knowledge of where to go for help when you are not around (be it stackoverflow or the oracle Java site or etc.). The tutorials are pitched at the perfect level for that learning.

        Cheers again,
        Neil

        1. No reason to worry 🙂 I love any positive criticism. Without it I would never improve. When I first started making videos I figured nobody would ever see them so I didn’t aim to perfect them. It wasn’t until you guys started pointing out every little error that I realized I needed to step up my game.

          I still have a long way to go. I still dream about making the perfect math tutorials, but I’m just not good enough yet. Keep me on my toes. I appreciate it!

          Derek

  2. Hi Derek,
    i have a request if it is possible. Can you get off the number in front of the code please? In this time i am studying code refactoring watching your video. You are running like a Ferrari and i have just a Fiat Panda 🙂
    Thnks for your work man!!!

    1. Hi, On the part of the page that has the code, put your mouse over it and click on the button labeled View Source it looks like <>. That will get rid of the line numbers. I hope that helps

  3. Hi derek. Great tutorial. I have watched it quite of in a rush, there are concepts in here that I am yet to learn so I will certainly need to go back into it, maybe search for some knowledge or/and probably go back to your earlier tutorials and learn quite a lot of things that I skipped. Nevertheless I have a question: I had the impression that the if clause inside the singletons breaks the rule “the program isn’t allowed to contain conditional statements”. Am I misunderstanding what was asked by emily’s professor?
    I am a brazilian fellow studying computer science in Israel’s OpenUniversity, I love to code and I was feeling somewhat frustrated by the large amount of stuff I have to learn at school and the little amount that is code or direction to actually implement ideas. Your tutorials are amazing to do in parallel, they’ve been giving me back the “rush” of learning and desiring to learn and know more, they give me very strong feeling of purpose on what I’m learning, thank you sooo much.

    1. Thank you very much for the compliment 🙂 At this time I can’t remember what all the rules were, but I know that the code I wrote was accepted by the teacher.

      I’m very happy that you enjoy the videos. I do my best to make original ones.

  4. p.s: sorry, the above was meant to the singleton tutorial, hope you can delete it from here and if you desire move it to the right place. thanks again

  5. Thanks again. Just one question. Is this line necessary:

    Object[] params = new Object[]{new String(athleteName)};

    getInstanceMethod.invoke(null, params);

    I was able to get away with it using the string directly like this and it works:

    getInstanceMethod.invoke(null, athleteName);

    1. I used that line to point out that invoke receives an Object, but since String is an Object it still works. I tend to point everything out in my code even if it is sometimes not necessary.

  6. My comments On Customer2 :
    a) The 3 static PREMIER, VALUED and DEADBEAT should be commented out. No more value.
    b) I know that lots of examples are built on the fact that the java default package is used. I don’t think that it is a good idea in this case. Please always make use of a package and certainly at this level because it is not for a beginner anymore. If you use a package name then your code doesn’t work anymore because the name of the class has to be prefixed by the package and you do agree with me that in most of the cases we all use packages.
    Refactoring I did :
    String packageName = this.getClass().getPackage().getName();
    return (Customer2) Class.forName(packageName + “.” + custName).newInstance();

    1. Yes I agree with your input. Some times I get stuck in keeping everything as basic as possible even when I should know that my audience will be far from intermediate programmers.

  7. (I didn’t thank you Mr Banas for the excellent tutorial!)
    on Athlete :
    Similar to Customer2. As the default package is used the code does not function anymore if a package (not default) is used.
    Corrections I did:
    String packageName = this.getClass().getPackage().getName();
    Method getInstanceMethod = Class.forName(packageName + “.” + medalType).getMethod(“getInstance”, athleteNameParameter);

  8. Another remark. You make use a lot of System.out.println and that is good to display some logging.
    I would recommend you at this level to make use of unit tests.
    I know that will complicate a little because of the use of JUnit but as it is a tutorial on code refactoring that will demonstrate to the users that after you make some refactoring and you run the tests that no regressions were created.
    I use most of the time maven and the archetype quickstart to create a new application and JUnit is already added to the pom (3.8). The maven plugin is now installed and configured in Eclipse by default. It means that if you ship a pom.xml and the code, your readers will be able to run your examples and normally not get any dependencies problems. But I know that the fact to build the examples on the core java packages is a good plus. You have javac then you have everything to make the examples run! My 2 cents. Thanks for your work Mr Banas.

Leave a Reply

Your email address will not be published. Required fields are marked *