title: How to apply Simple Factory Pattern Java
body: <p>I am trying ot understand how to apply a the simple factory pattern to an assigment I have but I do not understand how to do it.</p>
  
  <p>This is the request: Apply the Simple Factory pattern that creates the appropriate Strategy object.
  Remember, this is a Simple Factory pattern. Draw the UML diagram of the new
  design.</p>
  
  <p>We already implemented the strategy pattern but I do not understand how to apply the simple factory pattern to the code. I understand that the simple factory pattern is supposed to provide encapsulation for the creation of the object but I do not see how the examples I have found show how to apply to this. Any help would be appreciated.</p>
  
  <p>EDIT: Updated code
  EDIT: Changed code to make use of polymorphism</p>
  
  <pre><code>package Client;
  
  import domain.Loan;
  import factory.StrategyFactory;
  import strategy.ComplexLoan;
  import strategy.ICapitalStrategy;
  
  public class Client {
      public static void main(String[] args){
          Loan complexLoan = new Loan(2.2, 2, 3.3, 4.4, 5, 6, 7, StrategyFactory.getComplexStrategy());
          System.out.print("hello");
  
      }
  
  }
  
  package factory;
  
  import strategy.ComplexLoan;
  import strategy.ICapitalStrategy;
  import strategy.RevolvingLoan;
  import strategy.TermLoan;
  
  
  
  public class StrategyFactory {
      /*
      public static ICapitalStrategy getStrategy(String type) {       
          if (type.equals("Complex")){
              return new ComplexLoan();
          }
          else if (type.equals("Revolving")){
              return new RevolvingLoan();
          }
          else if (type.equals("Term")){
              return new TermLoan();
          }
          return null;
      }
      */
  
      public static ICapitalStrategy getComplexStrategy() {
          return new ComplexLoan();
      }
      public static ICapitalStrategy getRevolvingStrategy() {
          return new RevolvingLoan();
      }
      public static ICapitalStrategy getTermStrategy() {
          return new TermLoan();
      }
  }
  
      package domain;
      import strategy.ICapitalStrategy;
  
  
      public class Loan {
          private ICapitalStrategy strategy;
  
          double commitment;
          int duration;
          double riskFactor;
          double unusedPercentage;
          int outstandingRiskAmount;
          int unusedRiskAmount;
          double unusedRiskFactor;
          double capital;
  
          public Loan(double commit, int dura, double rskFact, double unusedPer,
                      int outStandRskAmt, int unusedRskAmt, double unusedRskFac,
                      ICapitalStrategy strat) {
              this.commitment = commit;
              this.duration = dura;
              this.riskFactor = rskFact;
              this.outstandingRiskAmount = outStandRskAmt;
              this.unusedRiskAmount = unusedRskAmt;
              this.unusedRiskFactor = unusedRskFac;
              this.strategy = strat;
  
          }
  
          public double CalculateCapital() {
              return strategy.CapitalLoan(this);
          }
  
          public double getCommitment() {
              return commitment;
          }
  
          public void setCommitment(double c) {
              commitment = c;
          }
  
          public int getDuration() {
              return duration;
          }
  
          public void setDuration(int dur) {
              duration = dur;
          }
  
          public double getRiskFactor() {
              return riskFactor;
          }
  
          public void setRiskFactor(double rskFac) {
              riskFactor = rskFac;
          }
  
          public double getUnusedPercentage() {
              return unusedPercentage;
          }
  
          public void setUnusedPercentage(double unusedPercent) {
              unusedPercentage = unusedPercent;
          }
  
          public double getOutstandingRiskAmount() {
              return outstandingRiskAmount;
          }
  
          public void setOutstandingRiskAmount(int outStandingRskAmt) {
              outstandingRiskAmount = outStandingRskAmt;
          }
  
          public double getUnusedRiskAmount() {
              return unusedRiskAmount;
          }
  
          public void setUnusedRiskAmount(int UnusedRskAmt) {
              unusedRiskAmount = UnusedRskAmt;
          }
  
          public double getUnusedRiskFactor() {
              return unusedRiskFactor;
          }
  
          public void setUnusedRiskFactor(double unusedRskFac) {
              unusedRiskFactor = unusedRskFac;
          }
  
          public Loan(ICapitalStrategy strategy) {
              this.strategy = strategy;
          }
  
          /*public double executeStrategy() {
              return this.strategy.CapitalLoan(this);
          }
          */
      }
  
      package strategy;
  
      import domain.Loan;
  
      public class ComplexLoan implements ICapitalStrategy {
  
          @Override
          public double CapitalLoan(Loan l) {
              return ((l.getOutstandingRiskAmount() * l.getDuration() * l.getRiskFactor()) + (l.getUnusedRiskAmount() 
                      * l.getDuration() * l.getUnusedRiskFactor() ));
          }
  
      }
  
      package strategy;
  
      import domain.Loan;
  
      public interface ICapitalStrategy {
          public double CapitalLoan(Loan l);
      }
  
      package strategy;
  
      import domain.Loan;
  
      public class RevolvingLoan implements ICapitalStrategy {
  
          @Override
          public double CapitalLoan(Loan l) {
              return (l.getCommitment() * l.getUnusedPercentage() * l.getDuration() *l.getRiskFactor());  
          }
  
      }
  
      package strategy;
  
      import domain.Loan;
  
      public class TermLoan implements ICapitalStrategy {
          public TermLoan() {
  
          }
          public double CapitalLoan(Loan l) {
              return (l.getCommitment() * l.getDuration() * l.getRiskFactor());
          }
  
      }
  </code></pre>