Abstract Factory
//Abstract Factory
public abstract class CreditCardFactory {
public static CreditCardFactory getCreditCardFactory(int creditScore) {
if(creditScore > 650) {
return new AmexFactory();
}
else {
return new VisaFactory();
}
}
public abstract CreditCard getCreditCard(CardType cardType);
public abstract Validator getValidator(CardType cardType);
}
Concreate Facotry - Amex
public class AmexFactory extends CreditCardFactory {
@Override
public CreditCard getCreditCard(CardType cardType) {
switch (cardType) {
case GOLD:
return new AmexGoldCreditCard();
case PLATINUM:
return new AmexPlatinumCreditCard();
default:
break;
}
return null;
}
@Override
public Validator getValidator(CardType cardType) {
switch (cardType) {
case GOLD:
return new AmexGoldValidator();
case PLATINUM:
return new AmexPlatinumValidator();
}
return null;
}
}
Concreate Amex Credit Card
//AbstractProduct
public abstract class CreditCard {
protected int cardNumberLength;
protected int cscNumber;
public int getCardNumberLength() {
return cardNumberLength;
}
public void setCardNumberLength(int cardNumberLength) {
this.cardNumberLength = cardNumberLength;
}
public int getCscNumber() {
return cscNumber;
}
public void setCscNumber(int cscNumber) {
this.cscNumber = cscNumber;
}
}
public class AmexPlatinumCreditCard extends CreditCard {
}
public class AmexGoldCreditCard extends CreditCard {
}