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.

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 3 Years Ago by JamesCherrill

This question has already been answered. Start a new discussion instead.