Abstract Factorypackage com.vishwa.account;
public interface Account { public Account getAccount(int accNo); public int getAccountNo() ; public String getAccType(); -------------------------------------------------------------------- |
package com.vishwa.fund;
public interface Fund { public Fund getFund(int fundNo); public String getFundType(); public int getFundNo() ; } -------------------------------------------------------------------- |
package com.vishwa.account;
public class CurrentAccount implements Account { int accountNo; public final String accType ="CURRENT"; public CurrentAccount(int accountNo) { super(); this.accountNo = accountNo; } @Override public Account getAccount(int accNo) { return new CurrentAccount(accNo); } public int getAccountNo() { return accountNo; } public void setAccountNo(int accountNo) { this.accountNo = accountNo; } public String getAccType() { return accType; } } -------------------------------------------------------------------- |
package com.vishwa.fund;
public class CurrentFund implements Fund { int fundNo; public final String fundType ="CURRENT"; public CurrentFund(int fundNo) { super(); this.fundNo = fundNo; } public int getFundNo() { return fundNo; } public void setFundNo(int fundNo) { this.fundNo = fundNo; } public String getFundType() { return fundType; } @Override public Fund getFund(int fundNo) { return new CurrentFund(fundNo); } } -------------------------------------------------------------------- |
package com.vishwa.account;
public class SavingAccount implements Account { int accNo; public final String accTyp ="SAVING"; public SavingAccount(int accNo) { super(); this.accNo = accNo; } @Override public Account getAccount(int accNo) { return new SavingAccount(accNo); } public int getAccountNo() { return accNo; } public void setAccountNo(int accountNo) { this.accNo = accountNo; } public String getAccType() { return accTyp; } } -------------------------------------------------------------------- |
package com.vishwa.fund;
public class SavingFund implements Fund { int fundNo; final String fundType ="SAVING"; public SavingFund(int fundNo) { super(); this.fundNo = fundNo; } @Override public Fund getFund(int fundNo) { return new SavingFund(fundNo); } public String getFundType() { return fundType; } public int getFundNo() { return fundNo; } public void setFundNo(int fundNo) { this.fundNo = fundNo; } } -------------------------------------------------------------------- |
package com.vishwa.factory;
import com.vishwa.account.Account; import com.vishwa.account.SavingAccount; import com.vishwa.fund.Fund; import com.vishwa.fund.SavingFund; public class SavingFactory extends GeneralFactory { @Override public Account getAccount(int account) { return new SavingAccount(account); } @Override public Fund getFund(int fundNo) { return new SavingFund(fundNo); } } -------------------------------------------------------------------- |
package com.vishwa.factory;
import com.vishwa.account.Account; import com.vishwa.account.CurrentAccount; import com.vishwa.fund.CurrentFund; import com.vishwa.fund.Fund; public class CurrentFactory extends GeneralFactory { @Override public Account getAccount(int account) { return new CurrentAccount(account); } @Override public Fund getFund(int fundNo) { return new CurrentFund(fundNo); } } -------------------------------------------------------------------- |
package com.vishwa.factory;
import com.vishwa.account.Account; import com.vishwa.fund.Fund; public abstract class GeneralFactory { public abstract Account getAccount(int account); public abstract Fund getFund(int fundNo); public static GeneralFactory getFactory(String type){ if(type.equalsIgnoreCase("CURRENT")) return new CurrentFactory(); else return new SavingFactory(); } } -------------------------------------------------------------------- |
package com.vishwa.test;
import com.vishwa.account.Account; import com.vishwa.factory.GeneralFactory; import com.vishwa.fund.Fund; public class Test { /** * @param args */ public static void main(String[] args) { GeneralFactory factory = GeneralFactory.getFactory("CURRENT"); Account currentAccount =factory.getAccount(1); System.out.println("Current Account No"+currentAccount.getAccountNo()); System.out.println("Current account type"+currentAccount.getAccType()); Fund fund = factory.getFund(1); System.out.println("Fund No"+fund.getFundNo()); System.out.println("Fund type"+fund.getFundType()); factory = GeneralFactory.getFactory("SAVING"); Account newAccount =factory.getAccount(1); System.out.println("newAccount Account No"+newAccount.getAccountNo()); System.out.println("newAccount account type"+newAccount.getAccType()); } } -------------------------------------------------------------------- |