In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of a sample analysis of the method factory pattern in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Main points of factory method model:
-in order to avoid the shortcomings of the simple factory model, OCP is not fully satisfied.
-the biggest difference between the factory method model and the simple factory model is that the simple factory model has only one factory class (for a project or a separate module). The factory method pattern has a set of factories that implement the same interface (used to produce fixed products in the same hierarchy and support the addition of arbitrary products)
2 method factory mode
I still use a code example to explain!
1. First of all, write out the Animal interface and Dog,Cat classes like a simple factory. I won't write the code here. Friends who want to see the code can read an article.
two。 Let's first create the AnimalFactory interface:
Public interface AnimalFactory {
Animal createAniaml ()
}
3. Continue to create DogFactory,CatFactory classes and inherit the AnimalFactory interface
Public class CatFactory implements AnimalFactory {
@ Override
Public Animal createAniaml () {
Return new Cat ()
}
}
Public class DogFactory implements AnimalFactory {
@ Override
Public Animal createAniaml () {
Return new Dog ()
}
}
4. Finally, let's test our code:
Public class Test {
Public static void main (String [] args) {
Animal A1 = new DogFactory () .createAniaml ()
Animal a2 = new CatFactory () .createAniaml ()
A1.speak ()
A2.speak ()
}
}
Running result:
Woof! woof!
Meow, Meow, Meow!
Process finished with exit code 0
Analysis: compared with the simple factory model, the method factory model solves the problem of OCP, but it has a very bad place-that is, the structure is too complex, because we have created two more factory classes, if we want to add some factories in the future, we need to create a lot of classes, so now this model is used less, and most of them are still simple factory patterns.
Thank you for reading! This is the end of this article on "sample Analysis of method Factory patterns in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.