In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you how to use Java code to achieve factory method pattern related knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you have something to gain after reading this article, let's take a look at it.
1. Introduction of factory method model
Definition: the factory method pattern is a further abstraction of the simple factory pattern. The advantage is that it enables the system to introduce new products without modifying the original code, that is, to meet the opening and closing principle. Define an object interface to create and let the subclass decide which class to instantiate, delaying the instantiation of a class to the subclass.
two。 Factory method pattern class diagram UML
3. Factory method pattern role description
Abstract factory (AbstractFactory): provides an interface to create a product through which the caller accesses the factory method of the concrete factory to create the product
Concrete factory (ConcreteFactory): implements the method of abstract factory definition and completes the creation of concrete products.
Abstract product (Product): defines the specification of the product and describes the main features and performance of the product
ConcreteProduct: a method that implements the definition of an abstract product. There are specific factories to create products, and specific factories correspond to specific products one by one.
4. Code implementation
Factory method pattern implementation:
/ * Abstract factory: provide common methods for specific factories * / public interface Product {public void show ();} public class ConcreteProduct1 implements Product {@ Override public void show () {System.out.println ("concrete product 1 presentation...");}} public class ConcreateProduct2 implements Product {@ Override public void show () {System.out.println ("concrete product 2 presentation...") }} / * Abstract factory * / public interface AbstractFactory {public Product createProduct ();} / * concrete factory 1 produces concrete products 1 * / public class ConcreteFactory1 implements AbstractFactory {@ Override public Product createProduct () {ConcreteProduct1 concreteProduct1 = new ConcreteProduct1 (); System.out.println ("concrete factory 1 creates concrete products 1."); return concreteProduct1 } public class ConcreteFactory2 implements AbstractFactory {@ Override public Product createProduct () {System.out.println ("specific factory 2 produces specific products 2..."); return new ConcreateProduct2 ();}}
Call method:
/ / get specific product 1 Product product = new ConcreteFactory1 () .createProduct (); product.show (); Product product1 = new ConcreteFactory2 () .createProduct (); product1.show ()
Steps to use:
Create abstract product classes and define public methods for products
Create a concrete product class (implement an abstract product interface) and define the generated concrete product
Create an abstract factory class that defines the public interface of a concrete factory
Create a specific factory class and define the method for creating a specific product instance
The caller calls the method of a specific factory class to create instances of different specific products.
5. Re-exploration of Factory Model
Solve the problem: solve the simple factory class new products need to modify the factory class method logic problem, that is, do not open and close the principle.
Exit the creation of specific products to the subclass of the factory class (specific factory). At this time, the factory class is no longer responsible for the creation of all products, but gives the interfaces that the specific factory must implement, so that when adding new products, the factory method does not need to modify the logic of the factory class, but adds a new factory subclass, which conforms to the opening and closing principle.
6. Advantages and disadvantages
Advantages:
Strong flexibility, for the creation of new products, only need to write one more corresponding factory class
Users only need to guide the name of the factory to get the desired product, and there is no need to guide the specific product creation process.
Disadvantages:
It is easy to have too many classes, which increases the complexity.
Each factory can only produce one product, and the secondary malpractice can be solved by using abstract factory pattern.
7. Application scenario
Customers only focus on the name of the factory that created the product, and do not need to know the specific product name.
These are all the contents of the article "how to implement the factory method pattern in Java code". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.