In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Java abstract factory pattern case analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Java abstract factory pattern case analysis article will have a harvest, let's take a look at it.
1. What is the abstract factory pattern?
Abstract factory pattern: defines an interface to create clusters of related or dependent objects without specifying specific classes.
The abstract factory pattern can integrate the simple factory pattern with the factory method pattern.
From a design perspective, the abstract factory pattern is an improvement (or further abstraction) of the simple factory pattern.
Abstract the factory into two layers, the AbsFactory (Abstract Factory) and the factory subclass of the concrete implementation. Programmers can use the corresponding factory subclass based on the type of object created. This turns a single simple factory class into a factory cluster, which is more conducive to code maintenance and extension.
We still focus on the case in the previous article, drawing a class diagram in the abstract factory pattern.
two。 Case code
First of all, there are still several classes related to the Pizza category.
Package com.szh.factory.abstractfactory.pizza; / * declares that the Pizza class is an abstract class * / public abstract class Pizza {/ / Pizza name protected String name; / / prepares raw materials for different pizzas. Therefore, we make an abstract method, and the concrete raw material implementation is handed over to its subclass to complete public abstract void prepare (); / bake public void bake () {System.out.println (name + "baking;");} / / cut public void cut () {System.out.println (name + "cutting;") } / / package public void box () {System.out.println (name + "boxing;");} public void setName (String name) {this.name = name;}} package com.szh.factory.abstractfactory.pizza; public class LDPepperPizza extends Pizza {@ Override public void prepare () {setName ("Pepper pizza of London") System.out.println ("Pepper pizza in London prepares raw materials");}} package com.szh.factory.abstractfactory.pizza; public class LDCheesePizza extends Pizza {@ Override public void prepare () {setName ("London cheese pizza"); System.out.println ("London cheese pizza preparation raw materials") }} package com.szh.factory.abstractfactory.pizza; public class BJPepperPizza extends Pizza {@ Override public void prepare () {setName ("Pepper pizza in Beijing"); System.out.println ("Pepper pizza in Beijing prepares raw materials");} package com.szh.factory.abstractfactory.pizza Public class BJCheesePizza extends Pizza {@ Override public void prepare () {setName ("Beijing cheese pizza"); System.out.println ("Beijing cheese pizza prepares raw materials");}}
Here is the difference between an abstract factory and a factory approach.
Package com.szh.factory.abstractfactory.order; import com.szh.factory.abstractfactory.pizza.Pizza; / / the abstract layer (interface) of an abstract factory pattern public interface AbsFactory {/ / let the following factory subclasses implement public Pizza createPizza (String orderType);} package com.szh.abstractfactory.order; import com.szh.abstractfactory.pizza.BJCheesePizza;import com.szh.abstractfactory.pizza.BJPepperPizza;import com.szh.abstractfactory.pizza.Pizza Public class BJFactory implements AbsFactory {@ Override public Pizza createPizza (String orderType) {Pizza pizza = null; if ("cheese" .equals (orderType)) {pizza = new BJCheesePizza ();} else if ("pepper" .equals (orderType)) {pizza = new BJPepperPizza ();} return pizza;}} package com.szh.abstractfactory.order; import com.szh.abstractfactory.pizza.LDCheesePizza Import com.szh.abstractfactory.pizza.LDPepperPizza;import com.szh.abstractfactory.pizza.Pizza; public class LDFactory implements AbsFactory {@ Override public Pizza createPizza (String orderType) {Pizza pizza = null; if ("cheese" .equals (orderType)) {pizza = new LDCheesePizza ();} else if ("pepper" .equals (orderType)) {pizza = new LDPepperPizza ();} return pizza }} package com.szh.factory.abstractfactory.order; import com.szh.factory.abstractfactory.pizza.Pizza; import java.util.Scanner; public class OrderPizza {AbsFactory absFactory; public OrderPizza (AbsFactory absFactory) {setFactory (absFactory);} private void setFactory (AbsFactory absFactory) {Pizza pizza = null; String orderType = ""; / / user enters this.absFactory = absFactory; do {orderType = getType () / / factory may be a factory subclass in Beijing or a factory subclass in London pizza = absFactory.createPizza (orderType); if (pizza! = null) {/ / order ok pizza.prepare (); pizza.bake (); pizza.cut (); pizza.box () } else {System.out.println ("order failed"); break;}} while (true);} / / write a method to get the kind of pizza the customer wants to order private String getType () {Scanner scanner = new Scanner (System.in) System.out.println ("Please enter pizza category:"); String str = scanner.nextLine (); return str;}}
Finally, there is the test class.
Package com.szh.abstractfactory; import com.szh.abstractfactory.order.BJFactory;import com.szh.abstractfactory.order.LDFactory;import com.szh.abstractfactory.order.OrderPizza; import java.util.Scanner; public class MainTest {public static void main (String [] args) {Scanner scanner = new Scanner (System.in); String content = scanner.next (); if ("Beijing" .equals (content)) {new OrderPizza (new BJFactory ()) } else if ("London" .equals (content)) {new OrderPizza (new LDFactory ());} else {System.out.println ("cannot pre-match Pizza types...."); scanner.close ();}
This is the end of the article on "Java Abstract Factory pattern instance Analysis". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Java Abstract Factory pattern instance Analysis". If you want to learn more, you are 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.