In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Java simple factory, factory method, abstract factory pattern how to achieve", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java simple factory, factory method, abstract factory pattern how to achieve" it!
1. Simple factory model 1. Define
Simple factory pattern (Simple Factory Pattern) means that an instance of a product class is determined by a factory object.
It belongs to the creative pattern, but it does not belong to the GOF23 design pattern.
two。 Code case
Let's take the manufacturing of canned products as a case study.
Step 1: define the top-level interface class ICanned for cans
/ * @ Package: com.hzg.study.design.pattern.factory.simplefactory * @ Description: interface on the top level of cans * @ Author: HuangZhiGao * @ CreateDate: 2022-02-17 17:52 * / public interface ICanned {/ * display the name of cans * @ return void * @ author HuangZhiGao * @ date 2022 2Gash 1717 17:54 * / void showCannedName ();}
Step 2: write two specific implementation classes FruitCanned and BeefCanned respectively
/ * * @ Package: com.hzg.study.design.pattern.factory.simplefactory * @ Description: canned fruit * @ Author: HuangZhiGao * @ CreateDate: 2022-02-17 17:56 * / public class FruitCanned implements ICanned {@ Override public void showCannedName () {System.out.println ("name: canned fruit") } / * * @ Package: com.hzg.study.design.pattern.factory.simplefactory * @ Description: canned beef * @ Author: HuangZhiGao * @ CreateDate: 2022-02-17 17:57 * / public class BeefCanned implements ICanned {@ Override public void showCannedName () {System.out.println ("name: canned beef");}}
Step 3: write the factory class
Import com.hzg.study.design.pattern.factory.common.ICanned / * @ Package: com.hzg.study.design.pattern.factory.simplefactory * @ Description: cannery class * @ Author: HuangZhiGao * @ CreateDate: 2022-02-17 18:16 * / public class CannedFactory {/ * method for creating cans * @ param clazz * @ return com.hzg.study.design.pattern.factory.common.ICanned * @ author HuangZhiGao * @ date 2022 * public ICanned createCanned (Class)
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.