In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 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 points of Java factory model case analysis, the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Advantages
1. A caller wants to create an object by knowing its name.
two。 High scalability, if you want to add a product, just extend a factory class.
3. Shielding the specific implementation of the product, the caller only cares about the interface of the product.
Shortcoming
1. Each time a product is added, it is necessary to add a specific class and object implementation factory, which doubles the number of classes in the system, increases the complexity of the system to a certain extent, and increases the dependence of the specific classes of the system. This is not a good thing.
Working with scen
1. Logger: records may be recorded to the local hard disk, system events, remote servers, etc., the user can choose where to record the log.
two。 Database access, when the user does not know what kind of database the final system uses, and the database may change.
3. Designing a framework to connect to the server requires three protocols, "POP3", "IMAP" and "HTTP", which can be used as product classes to implement an interface.
I. the way of realization
For example, we need to buy a mobile phone on the e-commerce platform, we don't need to care about how the mobile phone manufacturer produces the mobile phone, we just need to enter: Huawei mobile phone, Xiaomi mobile phone in the search box, we can buy the mobile phone we want. Let's take a look at the implementation.
1. Define an interface package com.asurplus.common.factory;public interface Phone {/ * obtain mobile phone * * @ return * / String getPhone ();} 2. Define two interface implementation classes package com.asurplus.common.factory;/** * Huawei Mobile implementation Class * / public class HuaweiPhone implements Phone {@ Override public String getPhone () {return "Huawei Mobile" }} package com.asurplus.common.factory;/** * Xiaomi mobile phone implementation class * / public class XiaomiPhone implements Phone {@ Override public String getPhone () {return "Xiaomi mobile phone";}} 3. Define a factory class package com.asurplus.common.factory / * * Mobile factory class * / public class PhoneFactory {/ * acquire mobile phone * * @ param name * @ return * / public static Phone getPhone (String name) {if ("Huawei" .equals (name)) {return new HuaweiPhone ();} if ("Xiaomi" .equals (name)) {return new XiaomiPhone () } return null;}} II. Test package com.asurplus.common.factory;/** * factory mode * / public class TestMain {public static void main (String [] args) {/ / get the phone Phone huaweiPhone = PhoneFactory.getPhone ("Huawei"); System.out.println (huaweiPhone.getPhone ());}}
Output result
What we typed: Huawei, got Huawei's mobile phone, thus realizing our factory model.
These are all the contents of the article "case Analysis of Java Factory pattern". 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.