HTML to define the content of web pages, CSS to specify the layout of web pages and finally, JavaScript to program the behavior of web pages
Scripts
JavaScript must be written inside the <script> tag in HTML. Scripts can be placed in either the <head>, <body> or both if need be. Scripts can also be placed in an external file and it’s more practical to use this method when many HTML documents make use of the same JavaScript. JavaScript files have the extension .js
HTML doesn’t support for video and audio in language HTML5 has them integrated into it
Geographical Support
It is harder in HTML to track user’s location when they are using mobile devices HTML5 uses javascript Geolocation API which is used to identify the location of any user that uses the website
Error handling
HTML cannot handle inaccurate syntax and any other errors HTML5 is capable of handling inaccurate syntax and other errors
Graphics Support
In HTML, Vector Graphics support is possible with the help of other tools In HTML5, Vector Graphics is supported by default and it has canvas and SVG inbuilt
Browser Compatibility
HTML is compatible with all the browsers as it has been there for a really long time and browsers did enough modifications to support all the features of the HTML. HTML5 is relatively new and not all browsers support all the new tags and features of HTML5
It is design pattern that uses only one object or instance of an object at a time.
Steps to create a Singleton pattern
Use getInstance() method as it is used universally for a singleton class
Example
Let’s take an example of Blackjack. In the game of blackjack, the dealer deals two cards to each player in the game. In the instance created would be the whole deck of cards that the dealer needs to deal. In this case, you would only need one instance of that class since you can only use one deck of cards. The game wouldn’t be fair if the dealer deals from different deck of cards for each player. So, once the dealer deals a card, that card will be removed from the list and the list contains the remaining cards to be dealt. Thats why a singleton class makes sense since you have to use the cards from only that instance. Check out the GitHub link for the code on this.
The Facade pattern does all the actions that need to be done by a class in the background. It will give it a false sense of not doing a lot of work but in reality all the work is done by other classes.
Steps to create a Facade Pattern
Create a class that will need to perform a certain task.
Create another class that has the implementations of these methods.
Call these methods from the first class
Explanation
As you can see from the steps above, the first class just contains the methods that needs to be performed for the program to run. All these methods are created and implemented in another class. The first class will not see how these methods are performed and all the implementations are encapsulated within another class. In conclusion, you can see that all actions are performed in the background.
It ensures that when on object changes state, all its dependencies are notified and updated automatically. It defines a one-to-many dependency.
The key objects in this pattern are the subject and the observer. When there is a change in state with the subject, all the observers are notified. In response to the change, the observer will synchronize with the subject’s state.
When to use the observer pattern?
When change to one object requires change to another and there is no knowledge of how many objects need to be changed.
When you don’t want any tightly coupled objects.
Steps to create an Observer pattern
Create a Subject and Observer patterns. The subject will register, unregister or notify the observers of any changes occurring during runtime. The observer will have the method/methods of variables that need to be updated or changed.
Create a class that implements the subject.
Create an array list in this class containing all the observers.
Create the methods for registering and unregistering the observers as well as notifying the observers of any change occurring.
Create a class that implements the observer.
In this class, make updates to the variables that need to be changed.
Use a main class where you can make changes to the variables dynamically. These values will be passed to the method implementing the subject class where the values will be set. When these values are set, notify the observers of this change so the change could take place.
It defines a family of algorithms, encapsulates each one of them and makes them interchangeable. This means that reusability increases while preventing the code from breaking.
When should you use Strategy?
When many related classes different only in one behavior. This way, you don’t constantly have to implement/write code for every class that uses that specific funtion
It enables an algorithm’s behavior to be selected at runtime.
Example
Let’s take a look at an example from geeksforgeeks. They use an example of the game “Street Fighter” where there are 4 types of fighters. They can all use the moves jump(), kick(), roll(), duck() but each of their implementation is different. In this case, you can use an interface to make sure the character has to perform all these moves. What if only a few of the characters can perform break().
The solution to this is that we have to either put it in the interface and implement it in everyclass or write a block of code for it for every character that uses it. Too much work don’t you think?
You can always use the strategy pattern to solve this type of problem.
Steps to create a Strategy Pattern
Create an interface containing the method/methods that need to be implemented by certain classes
Create other classes that implements the interface and need to be dynamically called at runtime. These are the classes that contain the different methods of a certain strategy
Make an instance of the interface
Using composition, call the methods depending on the situation of the use
Conclusion
You want to use the strategy pattern when a class has only one behavior that is similar to other behaviors in a list and you want to dynamically choose the behavior. Basically, it goes around the overriding every single implementation of a method if the behavior is similar.
References
The video by Derek Banas explains the Strategy Design pattern extremely well and I suggest you check it out after reading the description