The Observer Pattern: A Brief Description.
What is the observer pattern? It is a design pattern in OOP programming. It helps to organise your code, speed your code up, and also avoids DRY (Don't Repeat Yourself). It is a function that is there, hiding and waiting, but it is not used on every execution. So what is it used for then exactly? It is usually used for an event handling system such as subscribe and unsubscribe system. According to Tuts Tutorials, without this there would be no event handling.
Take a look at Twitter network for example, there are millions signed up. Take a step to when you first joined and had no one on your follow list and therefore no feed from other people's tweets. In this case, this is where the subjects and they see that you have no contacts so it does not show you feed from other users. Once you follow someone on Twitter this links to the subjects in the script page, then it sends a message to the Observer and puts this information into an array, once it sees you in the array then in fires that function and displays any updates your follower makes. Within this Observer there is a special function called Remove ( ) this will remove the user from the Array and not notify you of any changes. If there was no observer pattern in place it would be a mess of code and slow down the page and possibly display unwanted information. This concept is used across all platforms.
In order to properly use a design pattern a developer must follow the following 3 steps:
· Understand your problem
· Understand the pattern
· Understand how it solves the problem
The Observer Pattern is just working away, not being bothering anyone. We can explain this with a simple analogy:
Let's imagine the subject is going around on the floor taking in orders like a waiter in a restaurant. The chef is our Observer and the waiter is the subject. The waiter takes in the orders from the customer, sends it off to the chef in the Kitchen he takes the order and cooks it. Once it’s ready it is sent out. Imagine the chef going out with food to a customer and saying did you want this, customer says "No thanks", Chef goes back, and cooks something else. "How about this food?", again the customer says "No thanks" and this process continues until finally the customer finds something they like. How annoying is that, not to mention the huge waste of resources. If the observer pattern was not in place, this chaotic mess would be playing out behind the scenes all the time.
- 12:04
- 0 Comments