In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is abstract factory pattern". In daily operation, I believe that many people have doubts about what abstract factory pattern is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is an abstract factory pattern"! Next, please follow the editor to study!
For example, in the business, you need to create three kinds of products: mask, gas mask and protective clothing, and each product contains both high-end and low-end products. According to the solution of the factory method model, the classes you need to create are as follows:
As shown in the figure, each product class corresponds to a factory class, when the number of products is large, the number of factory classes will be older and more, making the system very complex.
What should we do at this time?
First, take a look at the code of the product class. Masks and protective clothing are two abstract interfaces with high-end and low-end implementation classes, respectively:
Public interface IMask {void showMask ();} public class LowEndMask implements IMask {@ Override public void showMask () {System.out.println ("my low-end mask");}} public class HighEndMask implements IMask {@ Override public void showMask () {System.out.println ("I'm a high-end mask");}} public interface IProtectiveSuit {void showSuit () } public class LowEndProtectiveSuit implements IProtectiveSuit {@ Override public void showSuit () {System.out.println ("I am low-end protective clothing");}} public class HighEndProtectiveSuit implements IProtectiveSuit {@ Override public void showSuit () {System.out.println ("I am high-end protective clothing");}}
Next is the factory category. As the products are divided into high-end and low-end groups, the factory is also divided into high-end factories and low-end factories, each responsible for the creation of products in the group:
Public interface IFactory {/ / create mask IMask createMask (); / / create protective clothing IProtectiveSuit createSuit ();} public class LowEndFactory implements IFactory {@ Override public IMask createMask () {IMask mask = new LowEndMask (); / /. The initialization code return mask;} @ Override public IProtectiveSuit createSuit () {IProtectiveSuit suit = new LowEndProtectiveSuit () of / / LowEndMask; / /. / / LowEndProtectiveSuit's 100 lines of initialization code return suit;}} public class HighEndFactory implements IFactory {@ Override public IMask createMask () {IMask mask = new HighEndMask (); / /. The initialization code return mask;} @ Override public IProtectiveSuit createSuit () {IProtectiveSuit suit = new HighEndProtectiveSuit () of / / HighEndMask; / /. / / HighEndProtectiveSuit's 100 lines of initialization code return suit;}}
Finally, the client code can create different products by instantiating different factory subclasses and calling different creation methods:
Public class Test {public static void main (String [] args) {IFactory factoryA = new LowEndFactory (); IFactory factoryB = new HighEndFactory (); / / create low-end mask IMask maskA = factoryA.createMask (); / / create high-end mask IMask maskB = factoryB.createMask (); / / create low-end protective clothing IProtectiveSuit suitA = factoryA.createSuit () / / create high-end protective clothing IProtectiveSuit suitB = factoryB.createSuit (); maskA.showMask (); maskB.showMask (); suitA.showSuit (); suitB.showSuit ();}}
Simple factory model:
The simple factory pattern has a unique factory class, and the creation method of the factory class determines what kind of product object to create according to the if-else condition of the passed parameters.
Factory method mode:
The factory method pattern implements the factory interface by multiple factory classes and uses polymorphism to create different product objects, thus avoiding lengthy if-else condition judgment.
Abstract factory pattern:
The abstract factory pattern groups product subclasses, and different products in the same group are created by different methods of the same factory subclass, thus reducing the number of factory subclasses.
Partners familiar with the spring framework must be aware of an important feature of spring: dependency injection (DI).
With spring dependency injection, developers do not need to instantiate bean objects manually in business code, nor do they need to know any factory classes.
The whole process from creation to destruction of bean objects is completely managed by the spring container. All you need to do is to set the properties of bean in the xml configuration file (or use annotations):
Based on the above configuration, the spring container dynamically creates UserController objects and creates UserService objects that UserController depends on.
If the developer wants to replace userService, the implementation class of the bean object, with another class, there is no need to change any code, just modify the class property of the corresponding bean in the configuration file.
In most cases, we use the new keyword to create an object, and the class to which the object belongs is clearly defined in the code.
But in a few cases, we need to use the meta-information of class (such as the full class name) to dynamically create objects while the program is running, which uses the reflection of Java.
When we configure the corresponding bean in the spring configuration file, start the project, spring will parse the xml configuration file for us, and according to the different life cycle of the bean, the bean object will be created by the "factory" within spring.
Partners who are interested in the principle of spring dependency injection can read the BeanFactory interface in the spring source code and the related implementation classes.
At this point, the study of "what is the abstract factory pattern" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.