In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how java to achieve abstract factory pattern, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Abstract factory is also called super factory.
An example is explained:
A person can have a computer and a mobile phone, but I don't generate it, I just use it.
There is a super factory (AbstractFactory) abstract factory, under which there are two contract factories (1. A factory specializing in the production of mobile phones, 2. A factory specializing in the production of computers)
The factory responsible for producing computers is a simple factory model.
The factory responsible for the production of mobile phones is also a simple mobile phone factory model.
Code implementation logic:
First create a computer interface-- Computer, which has only the draw () method, and there are two brands under it
Dell entity class, which implements draw ()
Lenvo entity class, which implements draw ()
Then create a mobile interface-phone, which has only the call () method, and it also has two brands (entity classes)
Mi entity class
HuaWei entity class
Then define an AbstractFactory abstract factory that has two products (mobile phones, computers)
Mobile phone factories inherit abstract factories, computer factories inherit abstract factories
Then define a product factory and create an abstract factory.
Finally, the demo class uses the product factory
Code: first step Computer,phone
Public interface Computer {void draw (); / / drawing function} public interface Phone {void call (); / / phone function}
Step 2: Xiaomi and Huawei implement Phone interface
Public class Mi implements Phone {@ Override public void call () {System.out.println ("Xiaomi phone calls");}} public class HuaWei implements Phone {@ Override public void call () {System.out.println ("Huawei phone calls");}}
Step 3: create an AbstractFactory abstraction factory and open the interface of computer and mobile phone.
Public abstract class AbstractFactory {public abstract Computer getComputer (String computer); public abstract Phone getPhone (String phone);}
Fourth: the mobile phone factory inherits the abstract factory, the computer factory inherits the abstract factory.
Public class PhoneFactory extends AbstractFactory {@ Override public Computer getComputer (String computer) {return null;} @ Override public Phone getPhone (String phone) {if (phone.equalsIgnoreCase ("Mi")) {return new Mi () } else if (phone.equalsIgnoreCase ("HuaWei")) {return new HuaWei ();} return null;}} public class ComputerFactory extends AbstractFactory {@ Override public Computer getComputer (String computer) {if (computer = = null) {return null } if (computer.equalsIgnoreCase ("Dell")) {return new Dell ();} else if (computer.equalsIgnoreCase ("Lenvo")) {return new Lenvo ();} return null } @ Override public Phone getPhone (String phone) {return null;}}
5: FactoryProducer to get the AbstractFactory object
Public class FactoryProducer {public static AbstractFactory getFactory (String choice) {if (choice.equalsIgnoreCase ("Computer")) {return new ComputerFactory ();} else if (choice.equalsIgnoreCase ("Phone")) {return new PhoneFactory ();} return null;}}
No. 6: demo use products Factory
Public class AbstractDemo {public static void main (String [] args) {/ / get the phone factory AbstractFactory phoneFactory = FactoryProducer.getFactory ("Phone"); / / get the object of Xiaomi phone Phone mi = phoneFactory.getPhone ("Mi"); / / call Xiaomi's method mi.call () }} these are all the contents of the article "how java implements the Abstract Factory pattern". 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.
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.