Still remember the Interpreter example? No matter what you type is a number or an expression; it can calculate the result just by the clause” new GeneralExpression(inputText.text).interpret()”
Archive for the ‘Design Patterns’ Category
In web programming, we often use regular expression for validating the e-mail address or phone number. Regular expression is a powerful tool in validating the specific format field. However, the interpretation of regular expression is not an easy job.
In the GoF’s design patterns, there is a corresponding pattern named Interpreter.
Have you ever use HTTP-proxy or some other proxy? When you’re in a relatively isolated environment, such as the LAN in your company, maybe you’ll need it. Actually, when I was an intern in an IT company, I always used the HTTP-proxy to login the MSN and surf on the internet. When I used MSN or surf on the net, I can’t feel the existence of the proxy. And this is the role of a proxy. And this can be express as follows.
Yesterday, when I was on my way home, I suddenly met an old friend. I haven’t met him since I began to write these articles. We stop at a little cafe, and began to talk.
“I’ve just changed my job”, he said, “and now I join the Orange”.
“WOW, Orange, you mean the biggest MP3 manufacturer company,” I answered.
“Yeah, and I join the new product team, we want to surprise the whole world by our new product.” He said proudly.
……. (The rest of our conversation is very boring, so, let’s stop here)
Have you ever buy a computer online, maybe from dell? In dell’s website, we just need to follow its process to order the accessories you need, then you can get your own computer configuration. Of course, you can’t get the real computer until you pay it
Here, you direct the producer to produce your own computer through the dell website. Ok, three roles here, you, dell website and the real producer.
When I want to write the Prototype pattern, I firstly look up the ActionScript 3.0 manual. I want to find out whether there is a clone method in Object class. If so, I will use this as an example. But, unfortunately, I can’t find this method in the Object class. Then I found the prototype attribute, but the explanation confused me. So, I decide to show you this pattern in my own way without using the Object class.
Firstly, you need to know the intent of this pattern. You can read the following text.
There is a famous saying in computer science, “Program = Data Structure + Algorithm”. It figures out the importance of how to organize the data and how to deal with the data.
Maybe, in your applications, you care more about how to show the data, because this has much to do with the user experience. However, how to organize the data and deal with the data is also or much more important, because this has much to do with the performance.
Do you like playing cards? If your had ever played, you may noticed that everyone has their own way arranging the cards. And in most cases, people will put the cards in order, maybe from the biggest one to the smallest one. Eh, this is a way of sorting.
We can write down the following code to mimic this action.