Chapter 1 of design patterns-factory method patterns (Java implementation)

Source: Internet
Author: User
Tags abstract definition

Chapter 1 of design patterns-factory method patterns (Java implementation)
"I come first", "No, husband, I come first !". I heard a few people from far away. Oh no, it's the factory method model and the abstract factory model. The couple are quarreling. nimei is not eating, not all come first (the world of food ~). "Abstract Factory model, fast self-introduction, factory method model. As a man, you must understand the gentleman's manners. lady first doesn't understand it. Come back later. What is your urgent need ." (Voiceover: Yu Ge, This is a snack from my family, and my dad's good wine blablabla), "Oh, that abstract factory model, aunt called you back for dinner ." "Oh, I will go back. Wait for me ." Factory method, hurry up. "Wait, fish brother. I haven't finished the last time. Please give me a minute to explain the application scenario ." "Well, don't you" (Voiceover: the author who filled his mouth with old Beijing sugar gourd and Other snacks has been dragged away by everyone), and welfare: in the following scenarios, the singleton mode appears: when there is only one instance in the class and the customer can access it from a well-known access point. This unique instance should be extensible through subclass, and the customer should be able to use an extended instance without changing the code. All right, it's about me. Come on with the factory method. Factory method mode self-introduction good mode is me, I am good mode (pick up the eyebrows): Factory method ~ Everyone applauded, (PS: after waiting for a few seconds, it was silent. Masses: Good guys without gentleman manners, I'll let it go, face?), Keke, that, as a developer, I have a very high outbound rate during daily development and can often see me. The definition is as follows: Define an interface for creating an object, but let subclasses decide which class to instantiate. factory Method lets a class defer instantiation to subclasses. do you know what it means? (PS: even if the English language is good, your pure Chinglish makes us quite drunk ). I don't know. I want to translate it to you. It means to define an interface for creating objects and let the subclass decide which class to instantiate. The factory method delays the instantiation of a class to its subclass. Still don't understand? Let's talk about the generic class diagram of the factory method model. In my model, abstract Product products define Product commonalities and implement the most abstract definition of things. Creator creates classes for abstraction, that is, abstract factories, the specific creation of product classes is completed by the specific implementation factory ConcreteCreator. I have many children and grandchildren (PS: There are many variants). It's time for me to introduce myself and show my excellent character. Self-Analysis of the factory method model my biggest weakness is that there are no shortcomings. Perfect is really bad. If you want to be modest, it won't work. Don't believe it? Well, you can: first, the encapsulation is better, so the factory method makes the code structure clear and facilitates maintenance. Second, scalability is also superb. If you want to add a product, you may need to expand a factory class to embrace changes. Once again, the specific implementation of product classes can be shielded. users do not need to worry about how to implement the product. They only need to call the interface. As long as the interface is inconvenient, the upper-layer module will not change. Finally, I am a typical decoupling framework that complies with the legendary Demeter law and relies on the inverted principle. Disadvantages. "You lie, if you don't say anything, I will peek at you ***". "Stop, stop, stop, I said, I just made a joke. Why should I take it seriously? As the saying goes, if the mode is not sages, there will be no disadvantages for growth." Disadvantage: the customer may have to create a subclass of the Creator just to create a specific ConcreteProduct object. What is the implementation of the factory method model? Well, it's just as stupid as Guo Jing. Well, I'll show it to you. Well, I know that the author is a pure fool. Oh no, food lovers. Come to a food factory. 1 public interface Food {2 public void createFood (); 3} This Food interface is a general term for Food. Each Food has a way to make Food. 1 public class Tanghulu implements Food {2 public void createFood () {3 System. out. println ("I Am a delicious Old Beijing sugar gourd ~ "); 4} 5} The above code is used to produce the old Beijing sugar gourd. 1 public class Latiao implements Food {2 public void createFood () {3 System. out. println ("I Am a delicious weilong spicy Food ~ "); 4} 5} it refers to the legendary weilong hot article. Now everything is ready. I only owe Dongfeng, wrong, Dongfeng is useless, and I only owe the factory. In this way, as long as the Food Author gives instructions to the factory, it can produce delicious snacks. The following is the implementation of the abstract factory: 1 public abstract class AbstractFoodFactory {2 public abstract <T extends Food> T createFood (Class <T> c); 3} You can see, the input parameter of the Food production method in the Food factory is the implementation class of the Food interface, which is consistent with the class diagram. Here we use generics. By defining generics, the input difference between createFood must be a Class and a Food Class. The following is the implementation of the Food Factory: 1 public class FoodFactory extends ?actfoodfactory {2 public <T extends Food> T createFood (Class <T> c) {3 food Food = null; 4 try {5 foor = (T) Class. forName (c. getName ()). newInstance (); 6} 7 catch (Exception e) {8 System. out. println ("food production failed! "); 9} 10 return (T) food; 11} 12} finally is the test class, the production of food for fish brother practical ~ 1 public class Author {2 public static void main (String [] args) {3 Export actfoodfactory foodFactory = new FoodFactory; 4 5 // Author's favorite sugar cane gourd 6 Food tanghulu = foodFactory. createFood (Tanghulu. class); 7 tanghulu. createFood (); 8 9 // The author's favorite weilong spicy Article ~ 10 Food latiao = foodFactory. createFood (Latiao. class); 11 latiao. createFood (); 12} 13} with this Food factory, the author no longer needs to worry about snacks ~ At this point, the implementation of my factory method model has ended. In order to stop the single-instance mode, I want to introduce when it is my turn to mirror, "Alas, what are you doing, abstract Factory model. What are you doing, my ears ~", "You dare to force your brother to bribe you, and then force him to lie to me. Let's introduce you first, go back and kneel on the keyboard. "My wife, I am wrong. You asked me to finish talking about this. Ah, you guys help, help, me, and", (PS: looking at the husband and wife farther and farther away, the author grinned and touched the bulging belly, coming slowly from the back), "Hey ~", Well, because the factory method model leaves the market for some reason, the application scenario will be put in the next time as welfare. Actually, even if my wife doesn't listen, you should kneel down on the keyboard (PS: the broken mind of the author ), as for the story between the factory method model and the abstract factory model, cough up and predict 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.