In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand the factory method pattern and abstract factory verification developed by java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
Example of factory method pattern
Abstract factory schema verification
Example of factory method pattern
Develop a data format conversion tool to convert different data sources such as txt, excel and other format files to XML format output. It is necessary to consider the expansibility of other format files that need to be converted to xml format in the future, and use the relevant knowledge of design patterns to design.
To solve the problem, the factory method pattern is used to complete the design of this function. The design class diagram is as follows:
The code structure is as follows:
The code is as follows:
Public interface Creator {Convertor getConvertor ();} public class ExcelConvertorCreator implements Creator {@ Override public Convertor getConvertor () {return new ExcelConvertor ();}} public class TxtConvertorCreator implements Creator {@ Override public Convertor getConvertor () {return new TxtConvertor ();}} public interface Convertor {void transform ();} public class ExcelConvertor implements Convertor {@ Override public void transform () {System.out.println ("excel Converter") }} public class TxtConvertor implements Convertor {@ Override public void transform () {System.out.println ("txt file converter");}}
The test code is as follows:
Public class Testor {public void factoryMethodTest () {Creator creator = new TxtConvertorCreator (); Convertor convertor = creator.getConvertor (); convertor.transform ();}}
Running result:
Verification conclusion: the factory method pattern is used to complete the file converter, which satisfies the opening and closing principle and the single responsibility principle in the class division, which makes the program easy to expand and maintain; at the same time, the caller depends on the abstract and does not depend on the concrete. the dependency inversion principle is realized, all variables are defined by interfaces (abstract classes), and the concrete implementation classes can replace it, following the Richter replacement principle.
Abstract factory schema verification
A company develops a mobile game software, which needs to support operating system platforms such as IOS and windows Mobile, and provide different operation control classes and game interface control classes for different operating systems. It is necessary to consider that other operating systems will also need to use this mobile game in the future, using the relevant knowledge of design patterns for design.
To solve the problem, the abstract factory pattern is used to complete the design of this function. The design class diagram is as follows:
The code structure is as follows:
The code is as follows:
Public interface InterfaceController {void init ();} public class IosInterfaceControllerImpl implements InterfaceController {@ Override public void init () {System.out.println ("Apple interface");}} public class WinInterfaceControllerImpl implements InterfaceController {@ Override public void init () {System.out.println ("windows interface");}} public interface OprationController {public void init () } public class IosOprationControllerImpl implements OprationController {@ Override public void init () {System.out.println ("Apple operation logic");}} public class WinOprationControllerImpl implements OprationController {@ Override public void init () {System.out.println ("windows operation logic");}} public interface AbstractFactory {InterfaceController getInterfaceContorller (); OprationController getOprationController ();} public class IosFactoryImpl implements AbstractFactory {@ Override public InterfaceController getInterfaceContorller () {return new IosInterfaceControllerImpl () } @ Override public OprationController getOprationController () {return new IosOprationControllerImpl ();}} public class WinFactoryImpl implements AbstractFactory {@ Override public InterfaceController getInterfaceContorller () {return new WinInterfaceControllerImpl ();} @ Override public OprationController getOprationController () {return new WinOprationControllerImpl ();}}
The test code is as follows:
Public class AbstractFactoryTest {public void test () {AbstractFactory abstractFactory = new WinFactoryImpl (); OprationController oprationController = abstractFactory.getOprationController (); InterfaceController interfaceController = abstractFactory.getInterfaceContorller (); oprationController.init (); interfaceController.init ();}}
The running results are as follows:
Verification conclusion: the abstract factory pattern is very similar to the factory method pattern, the factory method pattern provides a method of producing objects, while the abstract factory pattern provides multiple I methods to produce different objects. This design accords with the opening and closing principle, and the program is easy to maintain and expand. for example, if the game system needs to run on the Hongmeng system, it needs to develop a new specific Hongmeng factory. And the interface control class and game operation class related to Hongmeng operating system. In addition, it is important to note that the abstract factory pattern is easy to extend the product family, but it is not easy when the product hierarchy changes.
This is the end of "how to understand the factory method pattern and abstract factory validation developed by java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.