In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 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 the relevant knowledge about how to achieve the simple factory model of Java. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
I. Overview of software design patterns
Software design pattern, we also call it design pattern. Since it is a pattern, it is specified according to a certain law. Just like this, the design pattern is a set of repeated use, most people know, after sorting, previous code design ideas and experience summary. It exists to improve the reusability, readability and reliability of the code.
Second, simple factory model 1. Brief introduction
Throughout human history, factories from the beginning of self-production and marketing model, to the later farming society of the small workshop model, and then the industrial reform of the assembly line model, to the formation of a modern industrial chain model. By the same token, our project code is iterated from simple to complex. For callers, it is getting easier and easier. It's like we don't have to think about how the factory makes things, we just need to buy them.
According to the actual business scenario, we can divide the factory pattern into three different implementation ways: simple factory pattern, factory method pattern and abstract factory pattern.
Here, we call the created object "product" and the object that created the product "factory". If you create very few products that can be done with only one factory class, then we call this pattern the simple factory pattern.
2. Analysis of advantages and disadvantages.
Advantages
The factory class must contain logical judgments that determine when to create an instance of which product. The client does not have to bear the responsibility of creating product objects directly, and the corresponding products can be created conveniently and quickly.
The client does not need to know the name of the class that creates the specific product, as long as it knows the relevant parameters.
Through the introduction of configuration files, specific product classes can be replaced or added without modifying the client code.
Shortcoming
Simple factory model factory class is relatively single, assume the responsibility of all products. Once an exception occurs, the whole system will be affected. And the factory code will be very bloated, violating the principle of high aggregation.
Using the simple factory pattern to introduce new factory classes will increase the complexity and difficulty of the system.
It is difficult to expand the system, so you have to modify the logic to add new products. When there are too many products, it will cause the problem of too complicated logic.
The simple factory pattern uses the static factory method, which prevents the factory role from forming an inheritance-based hierarchy.
Application scenario: the simple factory model is suitable for a small variety of products.
3. The structure and implementation of the pattern.
The main roles of the simple factory model are divided:
Simple factory (SimpleFactory): is the core of this design pattern and is mainly responsible for implementing the internal logic for creating all instances.
Abstract product (Product): is the parent class of all objects created by a simple factory and is mainly responsible for describing the common interface of all instances.
Specific product (ConcreteProduct): is the creation goal of this design pattern
Code implementation:
Public class Client {public static void main (String [] args) {} / / Abstract product public interface Product {void show ();} / specific product: ProductA static class ConcreteProduct1 implements Product {public void show () {System.out.println ("concrete product 1 display.") }} / / specific products: ProductB static class ConcreteProduct2 implements Product {public void show () {System.out.println ("specific product 2 shows...");}} final class Const {static final int PRODUCT_A = 0; static final int PRODUCT_B = 1; static final int PRODUCT_C = 2 } static class SimpleFactory {public static Product makeProduct (int kind) {switch (kind) {case Const.PRODUCT_A: return new ConcreteProduct1 (); case Const.PRODUCT_B: return new ConcreteProduct2 ();} return null } above is all the content of the article "how to realize the simple factory model of Java". 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.