In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to realize the Java factory model". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Concept: the factory pattern is responsible for instantiating a large number of classes that have a common interface.
Related roles: abstract factory: the concrete factory class that creates the object must implement this interface concrete factory: the concrete factory class that implements the abstract factory interface abstract product: the supertype (parent abstract class or parent interface) of the concrete object created concrete product: the concrete object created.
1) simple factory mode (static factory method mode):
Concept: create a new factory class that has a static factory method that determines which type of object to create based on the input parameters of the static factory method. Implementation: / / factory class public class Creator {/ / static factory method: the type of returned object is determined according to the input parameter condition. Public static ConcreteProduct factory (condition) {if (condition satisfies condition A) {return new ConcreteProductA () / Note: ConcreteProduct is a superclass of ConcreteProductA and ConcreteProductB} else if (condition satisfies condition B) {return new ConcreteProductB () } else {/ / return a default ConcreteProductX object or directly throw an exception} disadvantage: when adding a type (eg:ConcreteProductC), you need to modify the static factory method There is no requirement of the principle of opening and closing. For example: the getDateInstance () method of java.text.DateFormat. Note: the factory class DateFormat determines the condition by overloading the getDateInstance () method.
2) Factory method mode:
Concept: create a new factory interface (or factory abstract class), multiple concrete factory classes, all of which implement the factory methods defined in the factory interface, and each concrete factory class produces only specific types of objects. Implementation: / / Abstract factory public interface Creator {/ / factory method public Product factory () } / / specific factory ConcreteCreatorA implements the factory interface Creator public class ConcreteCreatorA implements Creator {/ / factory method public Product factory () {return new ProductA () }} / / specific factory ConcreteCreatorB implements the factory interface Creator public class ConcreteCreatorA implements Creator {/ / factory method public Product factory () {return new ProductB () }} disadvantages: when you add a new type (eg:ProductC), you need to add a new specific factory. When there are more types (specific factory classes will also increase), it is not easy to maintain. Note: the comparison between the factory method pattern and the template method pattern: 1) both are method-based and leave specific work to subclasses. 2) the factory method pattern defers the created work to the subclass, and the template method pattern gives the remaining logic to the subclass. Example: factory pattern in the java collection: / / Abstract factory public interface List extends Collection {Iterator iterator () / / Factory method} / / specific factory public class ArrayList implements List {public Iterator iterator () {return new Itr () / / java.util.Vector.Itr implements Iterator interface}} / / specific factory public class Vector implements List {public synchronized Iterator iterator () {return new Itr () / / java.util.ArrayList.Itr implements Iterator interface}}
3) Abstract factory pattern:
Concept: create a new factory interface (or factory abstract class), a concrete factory class that implements multiple factory methods defined in the factory interface, so this concrete factory class can produce multiple types of objects. Implementation: / / Abstract factory: specifies multiple factory methods public interface Creator {/ / create a factory method public ProductA factoryA () for A-series product objects / / create the factory method public ProductB factoryB () for B-series product objects } / / concrete factory: multiple factory methods for abstract factories have been implemented, public class ConcreteCreator implements Creator {/ / factory methods for creating class A product objects Public ProductA factoryA () {return new ProductA1 () / / Note: ProductA1 implements ProductA} / / the factory method public ProductB factoryB () {return new ProductB1 () to create a class B product object / / Note: ProductB1 implements ProductB}} disadvantage: when adding products from other series (eg:ProductC), you need to modify the abstract factory (add a new factory method to the abstract factory). " This is the end of the content of how to implement the Java factory model. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.