Chapter 1 of design patterns-Abstract Factory patterns (Java implementation)

Source: Internet
Author: User

Chapter 1 of design patterns-Abstract Factory patterns (Java implementation)
"The last time was my fault. The author of bribery asked me to talk about it first. However, my wife and adults have a lot to do with me, this time, let me add the application scenarios that have not been completed last time." (After that, I touched my knees and coughed. I didn't smile! Really !). Benefits: The application scenarios of the factory method model are good. If we talk about the application scenarios, it is simply that the project has a factory method everywhere ~ Although it is a bit difficult to say this, it is true that the usage is very high. When a class does not know the class of the object it must create. When a class wants its subclass to specify the object it creates. When a class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies. Well, I want to talk so much about it. I will give it to my wife in the next time. (Shoushenglei ~) Hello, everyone. Let's take a look at the abstract factory model today. In fact, people are also a common model. My definition is as follows: provide an interface for creating families of related or dependent objecets without specifying their concrete classes. "Husband, give me a translation. "Ah? Oh, okay ." (Press by the author: show love, dead... Hi, I have not finished speaking. The author who is dragged away without dignity ). This statement provides an interface for creating a group of related or mutually dependent objects without specifying their specific classes. The general class diagram of the abstract factory model is as follows: I am, in fact, an upgraded version of my husband. When there are multiple business types and business classifications, it is my turn to debut ~ Well, the basic situation is soy sauce. I'm going back to cook. "Hey, wife, don't go. This is not the case. You need to explain the specific implementation, or they don't understand it. "However, the Minister can't do it. "It's very easy. You just need to do this. Then, you can do it. "I do not know much about books, so don't lie to me, husband. Otherwise, hum ~" The specific implementation of the abstract factory model the previous time the fish brother said that there were too few varieties of things to eat, so he had to eat too much, so this time there were a few more varieties. By the way, many things of his husband last time were useless, let's see if we can use the waste of things as much as possible. We still need to maintain the traditional virtues of diligence and family ownership ~ 1 public interface Food {2 public void createFood (); 3 4 // each type of Food has a brand and category. 5 public void getBrand (); 6} the Food has two abstract classes, one is sugar gourd and the other is spicy. The following code is the implementation class of the sugar gourd and Hot Strips: 1 public abstract class AbstractTanghulu implements Food {2 public void createFood () {3 System. out. println ("I Am a delicious sugar gourd ~ "); 4} 5} 1 public abstract class into actlatiao implements Food {2 public void createFood () {3 System. out. println (" I Am a delicious spicy ~ "); 4} 5} Each abstract class has two implementation classes, which are specific things that implement public details, that is, the brands of foods, the following example shows how to implement the function of using a sugar cane gourd. It is similar to the following: 1 public class OldPekingTanghulu extends acttanghulu {2 public void getBrand () {3 System. out. println ("Old Beijing sugar gourd ~ "); 4} 5} This is the specific implementation class of the sugar gourd. It shows that its brand is from Old Beijing, the most famous sugar gourd ~. 1 public class TianjinTanghulu extends acttanghulu {2 public void getBrand () {3 System. out. println ("Tianjin sugar gourd ~ "); 4} 5} This is naturally the sugar gourd in Tianjin. Tianjin is also a good place. It's a good match in Tianjin ~ Fish brother is also our favorite, Ma Hua, Tianjin ~ Well, the food-related classes have been implemented. The next step is to implement the "Factory" for the manufacture of sugar gourd and spicy strips. The first step is to implement the interface for producing sugar gourd and spicy Strips: 1 public interface FoodFactory {2 3 // 4 public Food createTanghulu (); 5 6 // 7 public Food createLatiao (); 8} the next step is to check whether the real sugar gourd and spicy food can be produced. The first step is to implement the production of sugar gourd in Old Beijing: 1 public class OldPekingFactory implements FoodFactory {2 // production Old Beijing sugar gourd 3 public Food createOldPeking (); 4 5} next is the implementation class for the production of Tianjin sugar gourd (due to laziness, so we only implement the sugar cane gourd. As for the spicy food, you will naturally write it): 1 public class OldPeking Factory implements FoodFactory {2 // The production of Tianjin sugar gourd 3 public Food createTianjin (); 4 5} is now complete, do you want to try the different types of sugar gourd and which one is better? Needless to say, you also know what you want, so we will come to a test class for sugar gourd to eat, or the author will vomit again, the following is the test class, used to produce two sugar Gourd: 1 public class Y {2 public static void main (String [] args) {3 4 // produce the old Beijing Brand sugar gourd 5 FoodFactory peckingFactory = new OldPekingFactory (); 6 7 // produce tianjinFactory TianjinFactory = new tianjinFactory (); 9 10 Food pekingFood = peckingFactory. createTanghulu (); 11 Food tianjinFood = tianjinFactory. createTanghulu (); 12 13 System. out. println ("production in Beijing Card sugar gourd "); 14 pekingFood. createFood (); 15 pekingFood. getBrand (); 16 17 System. out. println ("Tianjin brand sugar gourd"); 18 tianjinFood. createFood (); 19 tianjinFood. getBrand (); 20} 21} "wife, hurry up, I'm hungry. "Come on, go back and cook for you ." This husband and wife show their favor ~ (PS: The author walked slowly from the back ). Okay, this is the time. "Ah, what are the disadvantages ?" (Abstract Factory model: Oh, my husband, I forgot to talk about the Application Scenario ~~~ Factory method mode: Is there any? No. Have a good meal first, starve to death ah,) want to know what will happen later, and listen to the next decomposition.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.