12/19/2024
At the start of the semester, we were introduced to Design patterns. These techniques include anything from completely restarting a project to erasing redundancy such as repetition of code. Our biggest focus was to use overrides ONLY where needed and it was our job to figure that out because there were certain methods that did need to be overridden.
We worked on a Duck Simulator that has methods for each duck itself, but some ducks needed functions to be overridden were as some didn’t need them for example a decoy duck does not fly. Whereas a mallard duck does therefore there is no need to override a function for that decoy duck. This design pattern is very easy to overuse specially for people not introduced to these types of problems.
The interesting part of this homework was also using the previously covered topic of UML diagrams. This homework though was more about reading and understanding but the main point was to transfer it to Java code. This was a nice intertwining moment because it makes it easier in the future to understand not just the UML but also the design patterns taught.
We created two different interfaces for the Duck Simulator which one was a Quack() and the other was Fly(). These made it so we as the developer do not have to keep overriding. Hence, we end up with a bit more files, but it saves us the trouble in the future if we needed to edit let’s say the decoy duck to fly. Now we are able to go into one file and edit without having affected any other files. Which in larger systems can become a big issue because now the developer has to go into every single file that uses said method which waste time and can be missed if multiple people are working on it at the same time.
We also learned about the singleton method which just retrieves a single instance of an object and if it is not there then it creates it does not use multiple of the same object. There is one down side I read about in the source and it was about unit testing. The source talks about many unit testing frameworks that rely on inheritance when producing a mock object which is a problem because the singleton class is private, and static methods cannot be overridden. Overall, it is still applicable, and we used it in the intermediate part of the design patterns homework to learn how it worked better to always only have one instance of a method.
Source: Design Patterns in Java
From the blog CS@Worcester – Cinnamon Codes by CinnamonCodes and used with permission of the author. All other rights reserved by the author.