SOFT2201/COMP9201 Week 5 Tutorial
Creational Design Patterns
Creational Design Patterns
Copyright By PowCoder代写 加微信 powcoder
Creational design patterns handle object instantiation and construction mechanisms. Being able to
manage instantiation of multiples of types, abstracting construction and process.
Question 1: Issues with constructors
Identify some potential issues with the code segment 1 below. Consider the following criteria: Read-
ability, Maintainability, Dependencies and Reusability.
public class VehicleApplication {
private static void addVehicle(int numPassengers,
int numWheels,
String colour) {
if ((numPassengers == 4 || numPassengers == 5)
&& numWheels == 4) {
vehicles.add(new Car(numPassengers, colour));
} else if (numPassengers == 2 && numWheels == 4) {
vehicles.add(new Ute(colour));
} else if (numPassengers == 2 && numWheels == 2) {
vehicles.add(new Motorbike(colour));
System.out.println(“Invalid input”);
1Please note that, all the code used in this tutorial could be found under the module 5 on Canvas so that you
don’t need to type them again if you want to use them.
SOFT2201/COMP9201 Creational Design Patterns
Question 2: Is this better?
As a class, identify problems within the following code. Consider aspects where we may need to
extend the code or refactor it.
enum VehicleType {
MOTORBIKE,
public class VehicleFactory {
public static Vehicle make(VehicleType type, String colour) {
switch (type) {
case: VehicleType.CAR4:
return new Car(4, colour);
case: VehicleType.CAR5:
return new Car(5, colour);
case: VehicleType.ElectricCar4:
return new ElectricCar(4, colour);
case: VehicleType.ElectricCar5:
return new ElectricCar(5, colour);
case: VehicleType.MOTORBIKE:
return new Motorbike(colour);
case: VehicleType.Scooter:
return new Scooter(colour);
case: VehicleType.UTE:
return new Ute(colour);
return null;
//continued
Software Design and Construction 1 Page 2 of 6
SOFT2201/COMP9201 Creational Design Patterns
public class VehicleApplication {
private static void addVehicle(int numPassengers,
int numWheels,
String colour) {
if (numPassengers == 4 && numWheels == 4) {
vehicles.add(VehicleFactory.make(VehicleType.CAR4,
else if (numPassengers == 5 && numWheels == 4) {
vehicles.add(VehicleFactory.make(VehicleType.CAR5,
} else if (numPassengers == 2 && numWheels == 4) {
vehicles.add(VehicleFactory.make(VehicleType.UTE,
} else if (numPassengers == 2 && numWheels == 2) {
vehicles.add(VehicleFactory.make(VehicleType.MOTORBIKE,
System.out.println(“Invalid input”);
Factory method
Factory Method specifies construction of multiple related objects under a single method, the object
can decide what object to create and how to create it and return to the caller. The object created is
utilised through a common type that all objects created from the factory method type share.
Question 3: Refactor again
Using your knowledge of factory method, map out the VehicleFactory in Question 2 and refactor
the design to utilise factory method pattern.
• How would you break up the invocation of objects?
• How would the programmer invoke an object of this type?
Software Design and Construction 1 Page 3 of 6
SOFT2201/COMP9201 Creational Design Patterns
The builder pattern creates an abstraction of the construction process of an object, particularly com-
plex objects with multiple constructors. This allows for the object to maintain a single construction
method and the builder object to manage the construction process.
Question 4: Discuss object construction strategy
As a class, discuss how you can transform the following code to use the Builder pattern and how a
programmer will utilise this object.
public class Computer {
private Part cpu;
private Part motherboard;
private ArrayList
private ArrayList
private Enclosure encl;
public Computer(CPU cpu,
Motherboard motherboard) {
this.cpu = cpu;
this.motherboard = motherboard;
hdds = new ArrayList<>();
ram = new ArrayList<>();
encl = null;
public Computer(CPU cpu,
Motherboard motherboard,
Enclosure enclosure) {
this(cpu, motherboard);
encl = enclosure;
public Computer(CPU cpu,
Motherboard motherboard,
Enclosure enclosure,
ArrayList
this(cpu, motherboard, enclosure);
this.hdds = hdds;
public Computer(CPU cpu,
Motherboard motherboard,
Enclosure enclosure,
ArrayList
Software Design and Construction 1 Page 4 of 6
SOFT2201/COMP9201 Creational Design Patterns
ArrayList
this(cpu, motherboard, enclosure, hdds);
this.ram = ram;
/* Getters and Setters */
• How would a programmer construct this object?
• How many construction variations exist? Is there a chance that it can be ambiguous?
• What kind of issues can you see with the list of constructors?
• Create a concrete builder type that will abstract the construction process of the object
Question 5: Refactor the Order
An e-commerce platform application back-end has been encountering a large number bugs with their
checkout system. They have discovered a lot of their issues are stemming from construction of Order
objects and the handling of promotional products. Every time a promotion on an item occurs, the
back-end is required to be updated and deployed.
Unfortunately the cost of redevelopment is starting to outweigh the benefits of promotional products.
To save the company from eternal programmer debt, refactor the code base to address serious software
architecture problems.
public class Order {
* Current offset of the promotion
private double promoOffset;
* Customer property
private String customer;
* The list of products a customer has ordered
private List
* Constructs an order class
* @param c , a customer is provided and is owner of the order
public Order(String c) {
Software Design and Construction 1 Page 5 of 6
SOFT2201/COMP9201 Creational Design Patterns
customer = c;
products = new ArrayList
promoOffset = 0.0;
* Provides a list of products that have been ordered
* @return a list of ordered products
public List
return products;
* Gets the total (and discounted) order cost
* @return The total discounted cost
public double getOrderTotalCost() {}
double total = 0.0;
for(int i = 0; i < products.size(); i++) {
total += products.cost();
return total - promoOffset;
* Adds a product to the order and specified quantity
* @param p
* @param qty
public void addProduct(Product p, int qty) {
for(int i = 0; i < qty; i++) {
if(p.getName().equals("Detergent")) {
promoOffset += p.cost()*0.1;
} else if(p.getName().equals("Pasta")) {
promoOffset += p.cost()*0.25;
} else if(p.getName().equals("Whiskey")) {
promoOffset += p.cost()*0.05;
} else if(p.getName().equals("Cookies")) {
promoOffset += p.cost()*0.1;
products.add(p));
Software Design and Construction 1 Page 6 of 6
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com