Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does java implement a simple factory pattern

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you "java how to achieve a simple factory model", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "java how to achieve a simple factory model" this article.

The scenario example shows the algorithm operation before two numbers, as shown in the following code:

The code here needs to be judged four times, but if other operations are added, conditional judgment will be added on the basis of the original. In the future, judgment conditions will be constantly added as the business expands. The coupling degree of the code will be higher and higher, if you modify the problem of a certain operation, the online business can not access the related operation function, and the problem must be modified before other operation functions can be used.

Solution.

Now we can use what we call a simple factory pattern. First of all, we want to think that addition, subtraction, multiplication and division all belong to the operator subclass, so we create an operation parent class, Operation. Class.

Add, subtract, multiply, divide four operations, we create four subclasses of OperationAdd.class,OperationSub.class,OperationMul.class,OperationDiv.class respectively inherit the operation parent class Operation.class, and implement the calculation method.

When we want to use the operation method, we only need to create its object, and then execute the calculation method, we can calculate the operation of two numbers, but now there is another problem, that is, each operation method has to create an object. To expose the parent class object, we can create a factory class, which creates the desired object, as shown in the following code

These are all the contents of the article "how to implement a simple factory model in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report