When is the best time to use the flyweight pattern? Are flyweight patterns applied by deconstructing the existing class?

1. The module pattern is a very good pattern to use for structuring of large scale JavaScript applications. The module pattern keeps your global scope clean, enables namespacing and provides a nice way to seperate public and private methods and fields. The module pattern is excellent for performance. It minifies really well, which makes downloading the code faster. Using loose augmentation allows easy non-blocking parallel downloads, which also speeds up download speeds. Initialization time is probably a bit slower than other methods, but worth the trade-off. Run-time performance should suffer no penalties so long as globals are imported correctly, and will probably gain speed in sub-modules by shortening the reference chain with local variables.

The module pattern is also the pattern I chose for my assignment this week. The module pattern is one of the most commonly used by professional JavaScript developers. It is widely used because it provides structure and helps organize your code as it grows. Unlike other languages, JavaScript doesn’t have special syntax for packages, but the module pattern provides the tools to create self-contained decoupled pieces of code, which can be treated as black boxes of functionality and added, replaced, or removed according to the (ever-changing) requirements of the software you’re writing. It’s all based around the self-executing function closure, which allows us to create a sandboxed area of code that can access global variables and functions but that does not expose variables or functions declared within it to the surrounding scope, unless explicitly with a return statement.

2. Structural design patterns are used to make it easier on the developer by identifying how the classes and objects relate together to form larger types of structures. These types of design patterns have been used and tested thousands and thousands of times. There are seven types of structural design patterns, each one focuses on inherited classes and how they are composed from one another. Imagine if you had to get from point A to point B in a certain amount of time, this would be the amount of time that you would have to create a program. Do you take the interstate and get their quicker? Or do you take the local roads with traffic congestion and lights and arrive at a later time? Knowing that the interstate would get you there quicker would be like using existing design patterns to complete the job. If you decide to take the local roads with traffic would be like creating the design patterns from scratch. Each one may give you the finished product but the existing design patterns that have been tested over and over again will get you there much quicker with greater accuracy. The more practice you have with any type of programming language will enable you to learn the common problems and then find the best solutions. So a Jr. Developer may not need to know all the ins and outs but they will need to know when and how to use design patterns to their advantage.

For this week I am choosing the Flyweight Pattern for my assignment. Out of the seven design patterns, the idea of reusing already existing objects intrigued me the most. One of the main advantages of flyweight patterns are reducing the amount of objects and the amount of memory that is needed to store them.

Here are some questions that I found about Flyweight Patterns in Chapter 6, see if you can provide an answer.

  1. What are the three stages that are applied when using the flyweight pattern?
  2. When is the best time to use the flyweight pattern?
  3. Are flyweight patterns applied by deconstructing the existing class?