Moe's Tech Blog

Composite + Factory Design Pattern is Awesome 본문

Software Design Pattern/Journal

Composite + Factory Design Pattern is Awesome

moe12825 2022. 8. 18. 12:38

I was in the task of building software requiring many subtasks. Some of the functions included data analysis, data filtering, data customization, and generation of output. Depending on audiences, the data filtering and data customization may differ.

 

Initially, I attacked the problem using only functions. It resulted in a thousand lines of functions and codes. Even though it worked, it was hard to look at and think about and improve the software. Time was ticking, and even though my head was full of thoughts about making progress, I was making none. It was as if I had hit a wall.

 

The trouble didn't stop there. I was tasked to add more features! Adding each new feature required an exponential amount of time because I had to:

  1. Re-read and understand the whole code.
  2. Find the place where I needed to modify the code.
  3. Debug the full software after changing because changing a variable did something to other software parts.

I felt the need to rethink how I approach things to build an application that can quickly satisfy my superior's needs.

 

Driven by need, a couple of days ago, I learned about Composite Design patterns from Coursera's Design Pattern course. It was about using a list of classes of the same subtype to manage codes. I knew my subtasks were similar. They all had methods: run, reset and isComplete. Using the aggregation relationship between the whole software and the subtasks, all of a sudden, I had clean software, where adding more subtasks was as easy as adding one! To improve this, I used a factory pattern to allow different combinations of data analysis, filtering and generation of output. Using composite and factory design patterns made a difference to the software I built.

 

The composite + Factory design pattern is incredible. It allows the running of a combination of tasks without or little complexity.