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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.