关键词 > SOFT2201/COMP9201

SOFT2201/COMP9201 Week 5 Tutorial

发布时间:2023-01-20

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

SOFT2201/COMP9201

Week 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 1below. 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" );

}

}

}

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

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 , colour);

} else {

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?

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 ,

ArrayList  ram)  {

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  products ;

/ **

*  Constructs  an  order  class

*  @param  c  ,  a  customer  is  provided  and  is  owner  of  the  order */

public Order (String  c)  {

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++) {

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));

}

}

}