Code Refactoring 9

Code Refactoring 9In this part of my code refactoring tutorial I will cover how to turn an implied tree structure into a composite pattern.

What do you do when you want to model a tree structure? You can either list the info as primitives and then search through them for what you want or you can create objects that represent each level of the tree with the composite pattern.

I have an additional composite design pattern tutorial here. The code follows to help you learn.

If you like videos like this, it helps to tell Google [googleplusone]

Code From the Video

ProductComponent.java

ProductGroup.java

Product.java

ProductSystem.java

12 thoughts on “Code Refactoring 9”

  1. Hi Darek,
    just an awesome tutorial as you always do.
    i have one question, why do we do code refactoring?
    perhaps it is good way of designing a software, but what are the benefits of doing this, does the provide some performance improvements or what else?
    also if they provide performance improvements, how can i test them?

    Thanks,
    Punit

    1. Thank you 🙂 We refactor code so that it is easier to understand and to extend. I have been on numerous projects in which I was handed a 5 inch thick slab of code that wasn’t refactored and then told to add a bunch of features and fix a bunch of errors. When that day occurs for you you’ll understand how important refactoring is. It can be almost impossible to add features to non-refactored code.

      If you get good at writing clean code that is understandable people will literally worship you because you will make their lives so much better 🙂

  2. one more thing i wanted to know, not related to this tutorial,
    as i am doing my internship with a product based company, and they have one product, can you tell me what is the best way to understand their classes and coding, as hey have thousands of classes, and i always get myself messed with them.

    Thanks

    1. Get those people that wrote to code to explain it to you. Get ahold of all the diagrams for the system. The UML diagrams will help you along better than anything else. Get them to tell you what is most important to understand for you do do your job. Every company is different. I have seen fabulous easy to understand code and nightmarish code.

        1. Oh boy! That sounds like a nightmare. How was the software developed without UML diagrams? I never worked on a large project that didn’t start with UML

          1. lol, by the way i am working as a internee there.
            one more thing i wanted to know darek, so far we are using xml for data loading and creation, should we use json,(i am not aware to json as of now), should i suggest them to use json?

  3. Hey Derek,

    Just a little question. I’m a C# guy so maybe I’m missing something, – is there any special reason you cast all the returns from generic Arraylist and its iterator to the ProductComponent? Is’t it redundant with the generic collection?

    Regards,
    Mike

Leave a Reply

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