It looks like a regular POJO. And you are right, it's regular POJO, until here all it's ok.
Now, imagine your main program needs to instantiate our class Car 10 times. Then, the java heap memory will look like something like that.
What if I tell you that there are only two types of engines and two different colors ? Do you think that the memory allocation can be optimized to something that looks like the following picture ?
The answer is yes, with the Flyweight pattern. First of all, we have to design our light car with the common attributes.
After that, the next step is the container which contains all different types of light cars. It shall contain 4 different car types (2 engines x 2 colors).
Then our Car class looks like something like that (pay attention to the new interface created Car and the delegation through LightCar).
And finally, our Factory to create cars.
I think this is a great pattern, sometimes forgotten ;) .
You can see the original article at www.arquitecturajava.com
No comments:
Post a Comment