0

I wrote a code to represent the factory design pattern as below:

package com.factory2;

public interface CreditCheck {
     Double creditLimit(int id);
}


package com.factory2;

public class CreditCheckFactory {

     public boolean isAgencyUp(){
         return true;
     }

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


package com.factory2;

public class CreditCheckOffline implements CreditCheck{
     public Double creditLimit(int id){
         return 21.00;
     }
}


package com.factory2;

public class CreditCheckOnline implements CreditCheck{
     public Double creditLimit(int id){
         return 20.00;
     }
}


package com.factory2;

public class Test { 
    public static void main(String[] args){
        CreditCheckFactory ccf = new CreditCheckFactory();
        CreditCheck cc1 = ccf.createCreditCheck();
        System.out.println(cc1.creditLimit(8));
        CreditCheck cc2 = ccf.createCreditCheck();
        System.out.println(cc2.creditLimit(8));
    }
}

However I require some expert advice on whether my code does comply the factory design pattern. Some say that
the Factory class that I written should consist of static methods.

Your help is kindly appreciated.

Thank You.

2
Contributors
1
Reply
19
Views
3 Years
Discussion Span
Last Post by JamesCherrill
0

That looks right to me. I agree the CreditCheckFactory methods could be static - there's no need to instantiate a CreditCheckFactory.

(ps: Without some way to make isAgencyUp() false you can't fully test your code. And there's no obvious reason why isAgencyUp() should be public.)

Edited by JamesCherrill

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.