程序代写代做代考 SOFT2201 Module 5 Tutorial Creational Design Patterns

SOFT2201 Module 5 Tutorial Creational Design Patterns
Creational Design Patterns
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 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));
} else {
System.out.println(“Invalid input”);
} }
}
1

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 { CAR4,
CAR5,
MOTORBIKE,
UTE
}
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
}
SOFT2201 Creational Design Patterns
Software Construction
Page 2 of 7

public class VehicleApplication {
private static void addVehicle(int numPassengers,
int numWheels,
String colour) { if (numPassengers == 4 && numWheels == 4) {
vehicles.add(VehicleFactory.make(VehicleType.CAR4,
colour);
else if (numPassengers == 5 && numWheels == 4) { vehicles.add(VehicleFactory.make(VehicleType.CAR5,
colour); } else if (numPassengers == 2 && numWheels == 4) {
vehicles.add(VehicleFactory.make(VehicleType.UTE,
colour);
} else if (numPassengers == 2 && numWheels == 2) { vehicles.add(VehicleFactory.make(VehicleType.MOTORBIKE,
} else {
System.out.println(“Invalid input”);
} }
}
Factory method
The Factory Method design pattern specifies construction of multiple related objects via a single substitutable method. The factory object can decide what product object to create, how to create it, and return the result to the caller. The object created is utilised through a common base type that all objects created from the factory method share.
SOFT2201 Creational Design Patterns
colour);
Software Construction Page 3 of 7

Question 3: Refactor again
Using your knowledge of factory method, map out the previous code base (VehicleFactory) and refactor the design to utilise the factory method pattern.
• How would you break up the invocation of objects?
• How would the programmer invocate an object of this type?
SOFT2201 Creational Design Patterns
Software Construction Page 4 of 7

Builder
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 hdds; private ArrayList ram; 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 hdds) { this(cpu, motherboard, enclosure);
this.hdds = hdds; }
public Computer(CPU cpu,
Motherboard motherboard,
Enclosure enclosure,
ArrayList hdds,
SOFT2201 Creational Design Patterns
Software Construction
Page 5 of 7

• 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 products; /**
* Constructs an order class
* @param c , a customer is provided and is owner of the order
*/
public Order(String c) {
Software Construction Page 6 of 7
SOFT2201 Creational Design Patterns
ArrayList ram) { this(cpu, motherboard, enclosure, hdds);
this.ram = ram; }
/* Getters and Setters */
}

} }
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));
}
SOFT2201 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 getProducts() { 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++) { Software Construction Page 7 of 7