Design Patterns Java™ Workbook

(Michael S) #1
Appendix B. Solutions

SOLUTION 17.2....................................................................................................................................


Here is one solution:


package com.oozinoz.check.canada;
import com.oozinoz.check.*;
public class CheckFactoryCanada extends CheckFactory
{
public BillingCheck createBillingCheck()
{
return new BillingCheckCanada();
}


public CreditCheck createCreditCheck()
{
if (isAgencyUp())
{
return new CreditCheckCanadaOnline();
}
else
{
return new CreditCheckOffline();
}
}


public ShippingCheck createShippingCheck()
{
return new ShippingCheckCanada();
}
}


Your solution should



  • Have create- methods for each interface in com.oozinoz.check

  • Have the proper interface for the return type of each create- method

  • Return a CreditCheckOffline object if the agency is down


SOLUTION 17.3....................................................................................................................................


A version of CheckFactory.java that makes U.S. and Canada factories readily available
is:


package com.oozinoz.check;


import com.oozinoz.check.us.CheckFactoryUS;
import com.oozinoz.check.canada.CheckFactoryCanada;


public abstract class CheckFactory {
public static final CheckFactory US =
new CheckFactoryUS();


public static final CheckFactory CANADA =
new CheckFactoryCanada();


public abstract BillingCheck createBillingCheck();

Free download pdf