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

Code From the Video

// Replace an implicit tree structure made up
// of primitives with the 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

// This acts as an interface for every Product
// and ProductGroup that is created

public abstract class ProductComponent {

	void add(ProductComponent newProductComponent) {
	void remove(ProductComponent newProductComponent) {
	ProductComponent getProductComponent(int componentIndex) {
		return null;
	String getProductGroupName() {
		return null;
	abstract void displayProductInfo();

import java.util.ArrayList;
import java.util.Iterator;

public class ProductGroup extends ProductComponent{
	// Each group and all the products contained in
	// that group are stored in this ArrayList
	ArrayList<ProductComponent> productComponents = new ArrayList<ProductComponent>();
	private String productGroupName;

	public ProductGroup(String productGroupName) {
		this.productGroupName = productGroupName;

	public void add(ProductComponent newProductComponent) {

	public void remove(ProductComponent newProductComponent) {

	public ProductComponent getProductComponent(int componentIndex) {
		return (ProductComponent)productComponents.get(componentIndex);

	public String getProductGroupName() { return productGroupName; }

	public void displayProductInfo() {
		// Cycle through and print every product in this Product Group
		Iterator<ProductComponent> productIterator = productComponents.iterator();
			ProductComponent productInfo = (ProductComponent);


// Each product represents a leaf in the ProductGroup

public class Product extends ProductComponent{

	private String productName;
	private double productPrice;
	public Product(String productName, double productPrice) {
		this.productName = productName;
		this.productPrice = productPrice;

	public String getProductName() { return productName;}

	public void setProductName(String productName) {
		this.productName = productName;

	public double getProductPrice() { return productPrice; }

	public void setProductPrice(double productPrice) {
		this.productPrice = productPrice;

	void displayProductInfo() {
		System.out.println(getProductName() + " $" + getProductPrice());


public class ProductSystem {

	public static void main(String[] args){
		// Add product groups
		ProductComponent produce = new ProductGroup("Produce");
		ProductComponent cereal = new ProductGroup("Cereal");
		// Top level component that contains all products
		ProductComponent everyProduct = new ProductGroup("All Products\n");
		// Add produce and cereal groups to everyProduct list
		// Add individual products to the groups
		produce.add(new Product("Tomato", 1.99));
		produce.add(new Product("Orange", .99));
		produce.add(new Product("Potato", .35));
		cereal.add(new Product("Special K", 4.79));
		cereal.add(new Product("Cheerios", 3.68));
		cereal.add(new Product("Raisin Bran", 3.68));
		// Display all products sorted into groups

12 Responses to “Code Refactoring 9”

  1. punit says:

    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?


    • admin says:

      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. Punit says:

    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.


    • admin says:

      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.

  3. Mike says:

    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?


Leave a Reply

Your email address will not be published.