For a school related project, I had to design an application combining multiple Design Patterns and implement it in Java. Below is the given project description :
So, first of all we have to infer which design patterns we are supposed to use from the given text.
First sentence indicates we should use Observer Pattern
“Family of sensors” manufactured by different factories, so Abstract Factory Pattern it is
Adaptability for SMS, so Adapter Pattern will be used
Different commands will be executed, so Command Pattern should be used
Single object creation, so Singleton Pattern will be used
So, we are gonna have to combine 5 patterns to implement the application. First of all, let’s see what the final output of the program will be like, and how the main function is implemented.
First we create the Singleton object. Second, as per the requirements, family of sensors should be produced by different factories. We create the factory with FactoryX and use it to produce motion and smoke sensors. Then the sensors are triggered and the user is notified. The remote is used to start and stop the Alarm and the Sprinkler.
So, we can see that (in order) Singleton, Observer, Abstract Factory and Command patterns have been used in Main. Only Adapter is missing, which is not directly called in main.
Let’s start with the Singleton Pattern, as it will be the easiest to implement.
This piece of code is pretty much the same in all the examples you would find with a quick google search. Only in the showMessage function, we print a statement (normally to a log file, but anyways) to see that the object is created and created only once.
Now we can move on to the next line, creating the factory and the sensors. Let’s see it in the class diagram first.
Let’s start with implementing the interface first, then we will write the concrete classes.
Each factory will write code for this interface. In this scenario, I’ve created only two factories, FactoryX and FactoryY. Following is the code for their concrete classes:
Now let’s take a look at the class diagram for the sensors.
SensorListener class is not of our concern right now, we will deal with it later when we implement the Observer Pattern. Again, let’s start with the interfaces first, then move on to the concrete classes.
Normally you would have a better function than getDescription, but since our application is not supposed to work and instead just print stuff, we will just implement it like this.
Now that we have the Abstract Factory implemented, we get a better picture of how the sensors will work. And like I mentioned earlier, Adapter Pattern was not called in Main, but in the sensors. Let’s see how the Adapter pattern is implemented in.
Now let’s look at how these sensors are used with Observer Pattern. Interface first:
“detected” functionality was implemented in previous classes. So the only thing left is this class:
Here we register the events that took place and let the objects call their detected functions when the notifyUser is called.
Now the only thing left is the Command pattern. In case you want to check on the Main function again to remember how it was called:
The class diagram:
And with the command pattern finished, we are done with the program. Here is the final class diagram:
I would’ve preferred to put down a references/resources list, but unfortunately I did not keep track of that. But it was all in pretty much the first page results for related pattern name searches.
I’ve used simpleUML to generate the UML class diagrams.
You can check the Github Repo for the whole code.
That’s it for this post, and feel free to reach out for adjustments/improvements on the post.