KFC Cashier System - Part III (Abstract Factory Design Patterns)
- Abstract factory
The abstract factory pattern allows customers to create a set of related products using abstract interfaces. Customer classes and factory classes are independent. Customers only need to invoke factory methods. When the product changes, they only need to modify the corresponding factory. Customers do not need to modify it.
-
Systematic analysis combined with abstract factory (class diagram of abstract factory)
-
Code:
- First create abstract food classes
package product; /** * *<p>Title:AbstractFood</p> *<p>Description:The basic category of food defines the basic attributes of food </p>. * @author dynamic * @date 2018 October 26, 2000, 2:43:39 p.m. * */ public abstract class AbstractFood { protected String foodname; protected int num; protected float price; public Float money() { return num * price; } }
Create an interface to return food information
public interface FoodMessage { public String getMessage(); }
Establish abstract base classes of different foods to implement FoodMessage interface, such as beverage class, hamburger class, fries class, etc. The code of beverage is given here, and the other classes can be described as cats and tigers:
/** * *<p>Title:Drink</p> *<p>Description:Abstract class of beverages </p> * @author Lenovo * @date 2018 October 26, 2000, 2:49:22 p.m. * */ public abstract class Drink extends AbstractFood implements FoodMessage { public String getMessage() { return (this.foodname + "-" + this.num + "-" + this.price + "-"); } }
Create specific foods for different types of food (here, beverages for example)
Two drinks were created
/** * *<p>Title:AppleJuice</p> *<p>Description:Fresh pressed watermelon juice </p> * @author dynamic * @date 2018 October 26, 2000, 2:51:29 p.m. * */ public class AppleJuice extends Drink { public AppleJuice(int num) { this.foodname = "Watermelon juice"; this.num = num; this.price = 6.00f; } }
/** * *<p>Title:GrapeJuice</p> *<p>Description:Fresh pressed orange juice </p> * @author Lenovo * @date 2018 October 26, 2000, 2:52:55 p.m. * */ public class GrapeJuice extends Drink { public GrapeJuice(int num) { this.foodname = "Fresh Orange Juice"; this.price = 8.00f; this.num = num; } }
After everything is created, a factory responsible for growing food objects is built. First, an abstract KFC factory is created to produce abstract food.
/** * *<p>Title:IFactory</p> *<p>Description:Abstract factory base class </p> * @author dynamic * @date 2018 October 26, 2000, 2:56:11 p.m. * */ public interface IFactory { public Drink createAppleJuice(int num); public Drink createGrapeJuice(int num); // public Hamburger createBeefHamburger(int num); // public Hamburger createPrawnHamburger(int num); // public FrenchFry createTomatoFrenchFry(int num); // public FrenchFry createOriginalFrencFry(int num); // public Chicken createHotChicken(int num); // public Chicken createOriginalChicken(int num); }
Create specific KFC factories to generate specific things
/** * *<p>Title:FoodFactory</p> *<p>Description:Specific KFC plant, production of specific things </p> * @author dynamic * @date 2018 October 26, 2000, 2:58:46 p.m. * */ public class FoodFactory implements IFactory { @Override public Drink createAppleJuice(int num) { return new AppleJuice(num); } @Override public Drink createGrapeJuice(int num) { // TODO Auto-generated method stub return new GrapeJuice(num); } @Override public Hamburger createBeefHamburger(int num) { // TODO Auto-generated method stub return new BeefHamburger(num); } @Override public Hamburger createPrawnHamburger(int num) { // TODO Auto-generated method stub return new PrawnHamburger(num); } @Override public FrenchFry createTomatoFrenchFry(int num) { // TODO Auto-generated method stub return new TomatoFrenchFry(num); } @Override public FrenchFry createOriginalFrencFry(int num) { // TODO Auto-generated method stub return new OriginalFrenchFry(num); } @Override public Chicken createOriginalChicken(int num) { // TODO Auto-generated method stub return new OriginalChicken(num); } @Override public Chicken createHotChicken(int num) { // TODO Auto-generated method stub return new HotChicken(num); } }
Finally, create a shop assistant to use the factory
/** * *<p>Title:Salesclerk</p> *<p>Description:How to use factories to obtain food objects and how to operate things </p> * @author dynamic * @date 2018 October 26, 2000, 3:23 p.m. * */ public class Salesclerk { private IFactory factory; private StringBuilder message = new StringBuilder(); public Salesclerk(IFactory factory) { this.factory = factory; } public StringBuilder getMessage() { return this.message; } public Float orderAppleJuice(int num) { Drink j = factory.createAppleJuice(num);// Obtaining apple juice objects message.append(j.getMessage());// Output of apple juice information return j.money(); // Return the total price of apple juice } public Float orderGrapeJuice(int num) { Drink jDrink = factory.createGrapeJuice(num); message.append(jDrink.getMessage()); return jDrink.money(); } public Float orderBeefHamburger(int num) { Hamburger beef = factory.createBeefHamburger(num); message.append(beef.getMessage()); return beef.money(); } public Float orderPrawnHamburger(int num) { Hamburger prawn = factory.createPrawnHamburger(num); message.append(prawn.getMessage()); return prawn.money(); } public Float orderHotChicken(int num) { Chicken hot = factory.createHotChicken(num); message.append(hot.getMessage()); return hot.money(); } public Float orderOriginalChicken(int num) { Chicken original = factory.createOriginalChicken(num); message.append(original.getMessage()); return original.money(); } public Float orderTomatoFrenchFry(int num) { FrenchFry tomato = factory.createTomatoFrenchFry(num); message.append(tomato.getMessage()); return tomato.money(); } public Float orderOriginalFrenchFry(int num) { FrenchFry original = factory.createOriginalFrencFry(num); message.append(original.getMessage()); return original.money(); } }
Finally, we create a user to simulate the effect of ordering and test whether the code works properly.
/** * *<p>Title:Custom</p> *<p>Description:Simulated ordering effect (used to test whether abstract factories are functioning properly)</p> * @author dynamic * @date 2018 October 26, 2000, 3:02:22 p.m. * */ public class Custom { public static void main(String[] args) { IFactory factory = new FoodFactory(); Salesclerk salesclerk = new Salesclerk(factory); System.out.println("A beef hamburger"); salesclerk.orderAppleJuice(1); salesclerk.orderGrapeJuice(1); salesclerk.orderBeefHamburger(1); System.out.println(salesclerk.getMessage()); } }
Test results:
So far, the core of the ordering part of the kFC cash register system under the abstract factory mode has been completed.
Visual interface implementation source code Click See