In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the meaning of Java factory mode". In daily operation, I believe many people have doubts about what Java factory mode means. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what does Java factory mode mean?" Next, please follow the editor to study!
Factory mode:
The design pattern is derived from life, and the production of human society has developed from the original collective production, privatization, workshops, handicraft factories and assembly line engineering, and the factory production mode has become the most efficient way.
Then this approach is also referred to in software design:
The factory pattern is our most commonly used instantiation object pattern, which uses factory methods instead of new operations. The famous Jive forum makes extensive use of the factory model, which can be seen everywhere in the Java program system. Because the factory pattern is equivalent to creating an instance object new, we often have to generate instance objects according to the class Class, such as An a=new A () factory pattern is also used to create instance objects, so in the future new should be more thoughtful, whether you can consider using the factory pattern, although this may do some more work, but it will bring your system greater scalability and as few modifications as possible. The factory pattern allows the user of the object not to know the specific implementation, but to use it directly through the object factory.
Java architect learning materials: Java architect technology stack / high concurrency / high availability / high performance / source code parsing
Simple factory model:
The simple factory pattern is a creative pattern, also known as the static factory method (Static Factory Method) pattern, but it is not one of the 23 design patterns. The simple factory pattern is for a factory object to decide which instance of a product class to create. Simple factory pattern is the simplest and most practical pattern in the family of factory patterns, which can be understood as a special implementation of different factory patterns.
Factory (WaterFactory) role:
The core of the simple factory pattern, which is responsible for implementing the internal logic for creating all instances. The method of creating the product class of the factory class can be called directly by the outside world to create the desired product object.
Abstract product (Water) roles:
The parent class of all objects created by the simple factory pattern, which is responsible for describing the common interface common to all instances.
Specific products (Binglu, Nongfu Spring, Yibao) roles:
Is the creation goal of the simple factory pattern, and all the objects created are instances of a specific class that plays this role.
Advantages:
The factory class is the key to the whole pattern. It contains the necessary logical judgment to determine which specific class of objects should be created according to the information given by the outside world. By using factory classes, outsiders can extricate themselves from the embarrassment of creating specific product objects directly, and only need to be responsible for "consuming" objects. Regardless of how these objects are created and organized, their respective responsibilities and rights are clearly defined, which is conducive to the optimization of the whole software architecture.
Disadvantages:
Because the factory class centralizes the creation logic of all instances, violates the principle of high cohesion responsibility allocation, and centralizes all the creation logic into one factory class; the classes it can create can only be considered in advance, and if you need to add a new class, you need to change the factory class.
Simple factory pattern code example:
Package demo;/** * drinking water abstract product role * @ author OuYang * * / public interface Water {/ * get a product * @ return * / public String getWater ();}-- package demo / * * ice dew implementation class * @ author OuYang * * / public class IceDew implements Water {public String getWater () {return "ice dew pure water";}}-- package demo / * * Yibao implementation class * @ author OuYang * * / public class SimpleFactory_Water {public Water getWater (String brand) {if (brand.equals ("ice dew")) {return new IceDew ();} if (brand.equals ("Nongfu Spring")) {return new NongFu () } if (brand.equals ("Yibao")) {return new YiBao ();} return null;}}-package simplefactory_demo;import demo.IceDew;import demo.SimpleFactory_Water Public class TestWater {public static void main (String [] args) {/ / normal new mode System.out.println (new IceDew () .getWater ()) / / simple factory production mode (small workshop) / / users no longer need to understand the production process, only need the result SimpleFactory_Water factory = new SimpleFactory_Water (); System.out.println (factory.getWater ("Nongfu Spring"). GetWater ();}}
In Spring, beanFactory is the object factory, and configuration is the equivalent of code:
Factory.getWater ("Farmer Spring")
Of course, Spring's object factory is far from that simple.
Factory method mode:
In the simple factory model, because the factory class centralizes the creation logic of all instances, violates the principle of high cohesion responsibility allocation, and centralizes all the creation logic into one factory class; the classes it can create can only be considered in advance. If you need to add a new class, you need to change the factory class.
Obviously, the factory should also be abstracted and then implement a specific brand of object factory, so that the class can be enriched with more parameters and functions in the factory. The division of labor is finer and the maintainability is higher.
Import SimpleFactory.Water;/** * factory model * @ author OuYang * * / public interface Factory {/ / factory product export Water getWater ();}-package methodfactory;import SimpleFactory.NongFu;import SimpleFactory.Water / * * Nongfu Spring production Plant * @ author OuYang * * / public class NongFuFactory implements Factory {public Water getWater () {return new NongFu ();}}-package methodfactory;package abstractfactory;import SimpleFactory.IceDew;import SimpleFactory.NongFu;import SimpleFactory.YiBao / * * Test class * @ author OuYang * * / public class FactoryTest {public static void main (String [] args) {System.out.println (new YiBaoFactory (). GetWater (). GetWater ());}}
Abstract factory pattern:
In the factory method pattern, we don't need to new a variety of objects to use, we only need to new an object factory to get the objects we want.
But this only solves the problem that the functions are gathered in the same factory and solves the trouble caused by the change requirements, but we still have to new the corresponding object factory, and the complexity of the program still exists.
Create an abstract factory with public properties and abstract methods.
Implement the production plant of Binglu, Farmer and Yibao in the implementation class.
This not only solves the complexity of the code, but also sets different parameters in the factory implementation class.
/ * Abstract Factory * @ author OuYang * * / public abstract class AbstractFactory {/ / Common attributes and logic, capacity int volume; / / get Ipoh public abstract YiBao getYiBao (); / / get Ice dew public abstract IceDew getIecDew (); / / get Nongfu Spring public abstract NongFu getNongFu () }-- package abstractfactory;import SimpleFactory.IceDew;import SimpleFactory.NongFu;import SimpleFactory.YiBao;/** * Abstract Factory implementation class * @ author OuYang * * / public class WaterFactory extends AbstractFactory {@ Override public YiBao getYiBao () {/ / set to 1 liter this.volume=1 Return new YiBao ();} @ Override public IceDew getIecDew () {/ / set to 2 liter return new IceDew ();} @ Override public NongFu getNongFu () {/ / set to 3 liter return new NongFu () }-- package simplefactory_demo;import abstractfactory.WaterFactory;/*** test class * / public class AbstractFactoryTest {public static void main (String [] args) {WaterFactory factory = new WaterFactory (); System.out.println (factory.getIecDew () .getWater ()) At this point, the study on "what is the meaning of Java factory model" 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.