Implementations of Abstract Factory Pattern and Factory Method Pattern (UML, Java)
Wednesday, September 27, 2023
Assign. Exercise Gp. 6:
Name | Student # |
---|---|
Noah Toma | 100825559 |
Verina Bouls | 100789655 |
Ashwin Prem | 100805031 |
This repository contains all submission files as per the requirements outlined in the assignment.
The main directory features both subdirectories and files. The first subdirectory contains the Java source code, and is labeled Design Patterns. The second subdirectory includes all image files that depict the output of the Java implementation, and is labeled Output. The third subdirectory is titled UML, and contains both a PNG and JPEG image file depicting the completed UML diagram of the design pattern architecture. The files contained in the main directory include this Markdown document (titled README.md), the rubric provided for this assignment (titled Assign1-Design Patterns.pdf), and a .gitignore file to prevent unnecessary file commits.
Design Patterns contains all necessary project files, including all necessary '.java' and '.txt' source code files within subdirectory src. The other subdirectory in this folder is labeled Test, and includes a test file (Main.java) that serves as a means of analyzing the Java implementation by providing meaningful output. This output, as well as the code itself, is captured in this document, as well as in the Output folder.
Open/Close
Main.java: Source Code | Main.java: Output |
---|---|
Source Code as Text:
import java.text.DecimalFormat;
public class Main
{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("0.00");
GroceryStoreSupplier appleFactory = new AppleHarvester();
GroceryProductFactory apple = appleFactory.gatherFruit();
System.out.println("\nA new apple was created");
GroceryStoreSupplier bananaFactory = new BananaHarvester();
GroceryProductFactory banana = bananaFactory.gatherFruit();
System.out.println("\nA new banana was created\n");
System.out.println("Apple's cost: $" + df.format(apple.getPrice()));
System.out.println("Banana's cost: $" + df.format(banana.getPrice()));
}
}
Description of Main.java
First, the file imports the DecimalFormat
library, for the purposes of formatting float output. Then, the Main
class is instantiated and populated with a standard 'PSVM' method (public static void main(String[] args)
), which takes an array of Strings as an argument.
In this method, a DecimalFormat
object is instantiated. Next, a GroceryStoreSupplier
object is instantiated with an AppleHarvester
constructor and is followed by a GroceryProductFactory
object that is instantiated with the value returned by the function appleFactory.gatherFruit()
, which is an Apple
object with a price set by the value read from DB.txt (via ReadDB.java). The Apple
object (which follows a 'Factory Method' pattern inherited from GroceryProductFactory
) reads the price value stored in the database, stores it, and is returned via the AppleHarvester
object, which inherits from the GroceryStoreSupplier
'Abstract Factory'. Thus, apple
is an Apple
object (that follows the conventions of a factory method) returned by an abstract factory (GroceryStoreSupplier
's gatherFruit()
method). If the database was queried without error and the object was succcessfully instantiated, then a message stating that a new Apple
has been created is printed to the screen. The exact same logic applies to the Banana
object, only involving its respective classes/factory patterns instead for the purposes of distinction.
Finally, to demonstrate that the program has functioned as intended (i.e., the factories successfully produced respective objects and the database was read without error), the costs of Apple
and Banana
are returned via their respective 'getters' and printed to the screen with formatting. If the database was not read successfully, then an error message is printed and followed by a stack trace, and the prices of Apple
and Banana
default to zero.