The recent study of design patterns, for the abstract factory is very difficult to understand, on the internet to see some, and encountered a friend wrote the article, analogy is very appropriate, but also let me understand the approximate. Here is a reprint of the article he wrote.
I've forgotten his address for a long time. Recently organized a release to the blog up.
Always feel that the design pattern is very abstract (in fact, MS is also ...) ), so I would like to put it in the vernacular, so that people can understand more easily.
About the abstract factory (abstract Factory) model, a very popular example on the Internet, McDonald's and KFC's stories. Specifically how to say I have forgotten, but I would like to describe it again, hoping to deepen their understanding of the design model of the idea, but also hope to help you.
Since it is called abstract factory, there must be a factory (nonsense). Then we can think that McDonald's and KFC is the production of food factories, then, of course, hamburgers and chicken legs are the two kinds of food they produce together, whether you go to MDL or KDJ, said: "I want chicken leg, that certainly won't bring you to roast lamb leg." Hey. Here, we assume that McDonald's and KFC only produce these two products (this is important, because an important scenario of the abstract factory model is that the product is relatively stable, and if new products are to be added frequently, it will need to be mixed with other design patterns or seek other solutions, not to be redundant here).
The situation has been very obvious, we are consumers, we are customers, is the product of consumers, is the object of the program caller. And McDonald's and KFC, for granted, they are factories, a McDonald's factory, a KFC plant, they are real producers, and for our customer consumers (the caller of the program), whether it's going to McDonald's or KFC, I We all say the same thing (our request is stable): I want the chicken leg. As long as we make this request, we will certainly get what we want. As a factory (producer), McDonald's and KFC produce chicken legs and hamburgers, so the abstract of the abstract factory has the production of chicken legs and the production of hamburgers, which is the interface of two methods. Because in this interface, do not know exactly who to produce the product, so can only return an abstract chicken leg or hamburger, wait until McDonald's or KFC factory produced, know who is the home. (a sign, haha, this is the dynamic creation object)
Whether it's McDonald's or KFC's chicken leg or hamburger, it's essentially chicken leg or hamburger, so it can be abstracted. Then the chicken leg is derived from McDonald's chicken leg and KFC's chicken leg, and Hamburg is derived from McDonald's hamburger and KFC's hamburger. And for those of us who are customer consumers (the caller of the program), whether it's going to McDonald's or KFC, we say the same thing (our requirements are stable): I want chicken legs. As long as we make this request, we will certainly get what we want. No matter whose chicken leg it is, the chicken leg must not be the leg of the lamb. So, we just have to stipulate that the chicken leg (interface) on the line, and let the factory to the absolute specific production process. We only reached for a chicken leg, mercilessly bite a mouthful, ah, really fragrant ~:)
So far, we've only got chicken legs (abstract interfaces) and abstract factories (because we have to deal with chicken legs, whether it's McDonald's or KFC).
When we move to a fast food restaurant, the process determines exactly which factory we want to produce chicken legs and hamburgers. We can put the habit of our consumption into the brain, every time out directly out, just walk on the line. Also do not need to see in the end is to where, anyway, finally eat a lot of chicken legs and hamburgers, full of go. : Haha theory is complete, so now look at the UML diagram.