This article describes the KFC consumption case implemented by Java using abstract factory model. Share it for your reference, as follows:
1. Pattern definition
The abstract factory pattern provides an interface for creating families of related or dependent objects without specifying concrete implementation classes.
The abstract factory model allows customers to use abstract interfaces to create a set of related products. Customer classes and factory classes are separated. When customers need any products, they only need to request them from the factory. Customers can obtain new products without modification.
2. Examples of the model
1 Pattern Analysis
We borrow the scene of father and son going to KFC store to consume to illustrate this pattern. The screenshot after performing abstract analysis is as follows
2 Static modeling of abstract factory patterns
3 Code Examples
3.1 The establishment of abstract food
Abstract Food - AbstractBaseFood
package com.demo.factory.model;/** * * Food base class* * @author maofw * */public abstract class AbstractBaseFood{ // category protected String kind; // quantity protected int num; // price protected float price; // total public float totalPrice() { return this.num * this.price; }}Food Interface - IFood
package com.demo.factory.model;/** * Abstract food interface* * @author maofw * */public interface IFood{ /** * Print out food information*/ void printMesage();}3.2 Establish abstract base classes for different foods
Hamburger base class - Hamburg
package com.demo.factory.model;/** * Hamburg base class * @author maofw * */public abstract class Hamburg extends AbstractBaseFood implements IFood{ public void printMesage() { System.out.println("--" + this.kind + "flavored hamburger, /t unit price: " + this.price + ", /t quantity: " + this.num + ", /t total: " + this.totalPrice()); }}Chicken Wings
package com.demo.factory.model;/** * Chicken Wings base class * @author maofw * */public abstract class ChickenWings extends AbstractBaseFood implements IFood{ public void printMesage() { System.out.println("--" + this.kind + "flavored chicken wings, /t unit price: " + this.price + ", /t quantity: " + this.num + ", /t total: " + this.totalPrice()); }}French Fries Base Class - FrenchFries
package com.demo.factory.model;/** * French fries base class * * @author maofw * */public abstract class FrenchFries extends AbstractBaseFood implements IFood{ public void printMesage() { System.out.println("--" + this.kind + "flavored fries, /t unit price: " + this.price + ", /t quantity: " + this.num + ", /t total: " + this.totalPrice()); }}Beverage base class - Beverage
package com.demo.factory.model;/** * Beverage base class * @author maofw * */public abstract class Beverage extends AbstractBaseFood implements IFood{ public void printMesage() { System.out.println("--" + this.kind + "Beverage, /t unit price: " + this.price + ", /t quantity: " + this.num + ", /t total: " + this.totalPrice()); }}3.3 Create specific foods
Spicy chicken leg burger - ChinaHanburm
package com.demo.factory.model.kfc;import com.demo.factory.model.Hamburg;/** * Chinese-style spicy chicken leg burger* * @author maofw * */public class ChinaHanburm extends Hamburg{ /** * Constructing method* * @param kind * @param price * @param num */ public ChinaHanburm(int num) { this.kind = "spicy"; this.price = 14.0f; this.num = num; }}Orleans Chicken Wings - ChinaChickenWings
package com.demo.factory.model.kfc;import com.demo.factory.model.ChickenWings;/** * Chicken Wings implementation class* * @author maofw * */public class ChinaChickenWings extends ChickenWings{ public ChinaChickenWings(int num) { this.kind = "Orleans"; this.price = 2.5f; this.num = num; }}French fries - ChinaFrenchFries
package com.demo.factory.model.kfc;import com.demo.factory.model.FrenchFries;/** * French fries implementation class* * @author maofw * */public class ChinaFrenchFries extends FrenchFries{ public ChinaFrenchFries(int num) { this.kind = "normal"; this.price = 8.0f; this.num = num; }}Coke - ChinaBeverage
package com.demo.factory.model.kfc;import com.demo.factory.model.Beverage;/** * Beverage implementation class* * @author maofw * */public class ChinaBeverage extends Beverage{ public ChinaBeverage(int num) { this.kind = "Cola"; this.price = 7.0f; this.num = num; }}3.4 Establish a factory
Create an abstract KFC factory - IKfcFactory Produces abstract food
package com.demo.factory.itf;import com.demo.factory.model.Beverage;import com.demo.factory.model.ChickenWings;import com.demo.factory.model.FrenchFries;import com.demo.factory.model.Hamburg;/** * KFC abstract factory base class* * @author maofw * */public interface IKfcFactory{ // Produce hamburger public Hamburg createHamburg(int num); // Produce fries public FrenchFries createFrenchFries(int num); // Produce chicken wings public ChickenWings createChickenWings(int num); // produce beverages public Beverage createBeverage(int num);}Create a specific KFC factory - ChinaKfcFactory to produce specific foods
package com.demo.factory.itf;import com.demo.factory.model.Beverage;import com.demo.factory.model.ChickenWings;import com.demo.factory.model.FrenchFries;import com.demo.factory.model.Hamburg;import com.demo.factory.model.kfc.ChinaBeverage;import com.demo.factory.model.kfc.ChinaChickenWings;import com.demo.factory.model.kfc.ChinaFrenchFries;import com.demo.factory.model.kfc.ChinaHanburm;public class ChinaKfcFactory implements IKfcFactory{ // Produce Coke public Beverage createBeverage(int num) { return new ChinaBeverage(num); } // Produce Orleans roasted chicken wings public ChickenWings createChickenWings(int num) { return new ChinaChickenWings(num); } // Produce FrenchFries createFrenchFries(int num) { return new ChinaFrenchFries(num); } // Produce spicy chicken leg burger public Hamburg createHamburg(int num) { return new ChinaHanburm(num); }}3.5 Create a customer class - Customer
package com.demo.factory.custom;import com.demo.factory.itf.IKfcFactory;import com.demo.factory.model.Beverage;import com.demo.factory.model.ChickenWings;import com.demo.factory.model.FrenchFries;import com.demo.factory.model.Hamburg;/** * Client class* * @author maofw * */public class Customer{ // Abstract factory private IKfcFactory kfcFactory; // Constructor method passes the abstract factory as a parameter into public Customer(IKfcFactory kfcFactory) { this.kfcFactory = kfcFactory; } /** * Order food*/ // Order spicy chicken leg burger public float orderHamburg(int num) { // Get spicy chicken leg burger Hamburg hamburg = kfcFactory.createHamburg(num); // Output order information hamburg.printMesage(); // Return the total price return hamburg.totalPrice(); } // Order Orleans roast chicken wings public float orderChickenWings(int num) { // Get Orleans roast chicken wings ChickenWings chickenWings = kfcFactory.createChickenWings(num); // Output order information chickenWings.printMesage(); // Return total price return chickenWings.totalPrice(); } // Order fries public float orderFrenchFries(int num) { // Get fries FrenchFries FrenchFries = kfcFactory.createFrenchFries(num); // Output order information FrenchFries.printMesage(); // Return total price return frenchFries.totalPrice(); } // Order Coke public float orderBeverage(int num) { // Get Coke Beverage beverage = kfcFactory.createBeverage(num); // Output order information beverage.printMesage(); // Return the total price return beverage.totalPrice(); }}3.6 Storyline display
package com.demo.factory;import java.lang.management.ManagementFactory;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import java.net.URLConnection;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.text.NumberFormat;import java.util.Arrays;import java.util.Calendar;import java.util.ResourceBundle;import com.demo.factory.custom.Customer;import com.demo.factory.itf.ChinaKfcFactory;import com.demo.factory.itf.IKfcFactory;public class MainApp{ /** * Main application method* * @param args */ public static void main(String[] args) { /** * Define a KFC (IKfcFactory type) */ IKfcFactory kfcFactory = new ChinaKfcFactory(); /** * Dad and son walked into KFC to prepare to order*/ Customer customer = new Customer(kfcFactory); /** * Start ordering*/ // A spicy chicken leg burger float hamhurgMoney = customer.orderHamburg(1); // Four Orleans grilled chicken wings float chickenWingsMoney = customer.orderChickenWings(4); // A pack of fries float frenchFriesMoney = customer.orderFrenchFries(1); // Two cups of cola float beverageMoney = customer.orderBeverage(2); System.out.println("Total:" + (hamhurgMoney + chickenWingsMoney + frenchFriesMoney + beverageMoney)); }}Running results:
--Spicy burger, unit price: 14.0, quantity: 1, total: 14.0
--Orleans Flavored Chicken Wings, Unit Price: 2.5, Quantity: 4, Total: 10.0
--Ordinary flavor fries, unit price: 8.0, quantity: 1, total: 8.0
--Cola drink, unit price: 7.0, quantity: 2, total: 14.0
Total: 46.0
3. The design principles of this model
1 Use more object combinations and less inheritance
2. For abstract programming, not for implementation programming
3 Product objects are created through factory exposure method
4. Use occasions
1. Create a product family and use related products together;
2 Want to provide a product library and only want to display its interface instead of implementation;
3 When using the factory in combination.
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.