Only $2.99/month

Design Patterns in Java / Spring

Key Concepts:

Terms in this set (41)

public class Builder {
public static void main(String[] args) {
Account account = new Account.AccountBuilder("Saving Account", "Dinesh Rajput", 1111l)
.balance(38458.32)
.interest(4.5)
.type("SAVING")
.build();
System.out.println(account);
}
}
class Account {
private String accountName;
private Long accountNumber;
private String accountHolder;
private double balance;
private String type;
private double interest;
private Account(AccountBuilder accountBuilder) {
super();
this.accountName = accountBuilder.accountName;
this.accountNumber = accountBuilder.accountNumber;
this.accountHolder = accountBuilder.accountHolder;
this.balance = accountBuilder.balance;
this.type = accountBuilder.type;
this.interest = accountBuilder.interest;
}
//setters and getters
public static class AccountBuilder {
private final String accountName;
private final Long accountNumber;
private final String accountHolder;
private double balance;
private String type;
private double interest;
public AccountBuilder(String accountName,
String accountHolder, Long accountNumber) {
this.accountName = accountName;
this.accountHolder = accountHolder;
this.accountNumber = accountNumber;
}
public AccountBuilder balance(double balance) {
this.balance = balance;
return this;
}
public AccountBuilder type(String type) {
this.type = type;
return this;
}
public AccountBuilder interest(double interest) {
this.interest = interest;
return this;
}
public Account build() {
Account user = new Account(this);
return user;
}
}
public String toString() {
return "Account [accountName=" + accountName + ", accountNumber=" + accountNumber + ", accountHolder="
+ accountHolder + ", balance=" + balance + ", type="
+ type + ", interest=" + interest + "]";
}
}
public class Composite {
public static void main(String[] args) {
//Saving Accounts
SavingAccount savingAccount1 = new SavingAccount();
SavingAccount savingAccount2 = new SavingAccount();
//Current Account
CurrentAccount currentAccount1 = new CurrentAccount();
CurrentAccount currentAccount2 = new CurrentAccount();
//Composite Bank Account
CompositeBankAccount compositeBankAccount1 = new CompositeBankAccount();
CompositeBankAccount compositeBankAccount2 = new CompositeBankAccount();
CompositeBankAccount compositeBankAccount = new CompositeBankAccount();
//Composing the bank accounts
compositeBankAccount1.add(savingAccount1);
compositeBankAccount1.add(currentAccount1);
compositeBankAccount2.add(currentAccount2);
compositeBankAccount2.add(savingAccount2);
compositeBankAccount.add(compositeBankAccount2);
compositeBankAccount.add(compositeBankAccount1);
compositeBankAccount.accountType();
}
}
interface Account {
void accountType();
}
class SavingAccount implements Account{
@Override
public void accountType() {
System.out.println("SAVING ACCOUNT");
}
}
class CurrentAccount implements Account {
@Override
public void accountType() {
System.out.println("CURRENT ACCOUNT");
}
}
class CompositeBankAccount implements Account {
//Collection of child accounts.
private List<Account> childAccounts = new ArrayList<>();
@Override
public void accountType() {
for (Account account : childAccounts) {
account.accountType();
}
}
//Adds the account to the composition.
public void add(Account account) {
childAccounts.add(account);
}
//Removes the account from the composition.
public void remove(Account account) {
childAccounts.remove(account);
}
}
public class Decorator {
public static void main(String[] args) {
/ Saving account with no decoration /
Account basicSavingAccount = new SavingAccount();
System.out.println(basicSavingAccount.getTotalBenefits());
/ Saving account with senior citizen benefits decoration /
Account seniorCitizenSavingAccount = new SavingAccount();
seniorCitizenSavingAccount = new SeniorCitizen(seniorCitizenSavingAccount);
System.out.println(seniorCitizenSavingAccount.getTotalBenefits());
/ Saving account with privilege decoration /
Account privilegeCitizenSavingAccount = new SavingAccount();
privilegeCitizenSavingAccount = new Privilege(privilegeCitizenSavingAccount);
System.out.println(privilegeCitizenSavingAccount.getTotalBenefits());
}
}
interface Account {
String getTotalBenefits();
}
class SavingAccount implements Account {
@Override
public String getTotalBenefits() {
return "This account has 4% interest rate with per day $5000 withdrawal limit";
}
}
class CurrentAccount implements Account {
@Override
public String getTotalBenefits() {
return "There is no withdrawal limit for current account";
}
}
abstract class AccountDecorator implements Account {
abstract String applyOtherBenefits();
}
class SeniorCitizen extends AccountDecorator {
Account account;
public SeniorCitizen(Account account) {
super();
this.account = account;
}
public String getTotalBenefits() {
return account.getTotalBenefits() + " other benefits are " + applyOtherBenefits();
}
String applyOtherBenefits() {
return " an medical insurance of up to $1,000 for Senior Citizen";
}
}
class Privilege extends AccountDecorator {
Account account;
public Privilege(Account account) {
this.account = account;
}
public String getTotalBenefits() {
return account.getTotalBenefits() + " other benefits are " + applyOtherBenefits();
}
String applyOtherBenefits() {
return " an accident insurance of up to $1,600 and an overdraft facility of $84";
}
}