1
17-214
Principles of Software Construction: Objects, Design, and - - PowerPoint PPT Presentation
Principles of Software Construction: Objects, Design, and Concurrency Introduction to Design Patterns Charlie Garrod Chris Timperley 17-214 1 Administrivia Homework 1 feedback in your GitHub repository Homework 2 due tonight 11:59 p.m.
1
17-214
2
17-214
3
17-214
4
17-214
5
17-214
public void doSomething(Account acct) { long adj = 0; if (acct instanceof CheckingAccount) { checkingAcct = (CheckingAccount) acct; adj = checkingAcct.getFee(); } else if (acct instanceof SavingsAccount) { savingsAcct = (SavingsAccount) acct; adj = savingsAcct.getInterest(); } … }
6
17-214
public void doSomething(Account acct) { long adj = 0; if (acct instanceof CheckingAccount) { checkingAcct = (CheckingAccount) acct; adj = checkingAcct.getFee(); } else if (acct instanceof SavingsAccount) { savingsAcct = (SavingsAccount) acct; adj = savingsAcct.getInterest(); } else if (acct instanceof InterestCheckingAccount) { icAccount = (InterestCheckingAccount) acct; adj = icAccount.getInterest(); adj -= icAccount.getFee(); } … }
7
17-214
public interface Account { … public long getMonthlyAdjustment(); } public class CheckingAccount implements Account { … public long getMonthlyAdjustment() { return getFee(); } } public class SavingsAccount implements Account { … public long getMonthlyAdjustment() { return getInterest(); } }
8
17-214
9
17-214
10
17-214
11
17-214
12
17-214
13
17-214
14
17-214
15
17-214
16
17-214
17
17-214
18
17-214 public abstract class AbstractAccount implements Account { protected long balance = 0; public long getBalance() { return balance; } abstract public void monthlyAdjustment(); // other methods… } public class CheckingAccountImpl extends AbstractAccount implements CheckingAccount { public void monthlyAdjustment() { balance -= getFee(); } public long getFee() { … } }
19
17-214
20
17-214
21
17-214
22
17-214
23
17-214
24
17-214
25
17-214
26
17-214
27
17-214
28
17-214
29
17-214
30
17-214
31
17-214
32
17-214
33
17-214
34
17-214
35
17-214