2)Explain the difference between State pattern and Decorator pattern.5)Give the code required for the classes introduced in question 2), focusing on the code for a method selectState() which changes the value that is being adjusted from years to months. Make it clear in which classes the code is to be found. Assume the existence of other methods that are needed such as getMonthSetUpState().
时间: 2023-07-03 14:33:52 浏览: 118
mysql报错:MySQL server version for the right syntax to use near type=InnoDB的解决方法
5星 · 资源好评率100%
2) The State pattern is a behavioral design pattern that allows an object to alter its behavior when its internal state changes. This pattern involves creating state objects that represent various states of the object and a context object whose behavior varies as its state object changes.
On the other hand, the Decorator pattern is a structural design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. This pattern involves creating decorator objects that wrap the original object and provide additional functionality while keeping the original object's interface intact.
5) Here is the code for the classes introduced in question 2), focusing on the code for a method selectState() which changes the value that is being adjusted from years to months:
```java
// Context interface
interface Account {
void selectState(State state);
void deposit(double amount);
void withdraw(double amount);
double getBalance();
}
// Concrete Context class
class SavingsAccount implements Account {
private State state;
private double balance;
public SavingsAccount() {
this.state = new YearsSetUpState(); // default state
this.balance = 0.0;
}
@Override
public void selectState(State state) {
this.state = state;
}
@Override
public void deposit(double amount) {
this.balance += state.adjustDeposit(amount);
}
@Override
public void withdraw(double amount) {
this.balance -= state.adjustWithdrawal(amount);
}
@Override
public double getBalance() {
return this.balance;
}
// Other methods needed
public State getMonthSetUpState() {
return new MonthsSetUpState();
}
}
// State interface
interface State {
double adjustDeposit(double deposit);
double adjustWithdrawal(double withdrawal);
}
// Concrete State classes
class YearsSetUpState implements State {
@Override
public double adjustDeposit(double deposit) {
return deposit;
}
@Override
public double adjustWithdrawal(double withdrawal) {
return withdrawal;
}
}
class MonthsSetUpState implements State {
@Override
public double adjustDeposit(double deposit) {
return deposit / 12;
}
@Override
public double adjustWithdrawal(double withdrawal) {
return withdrawal / 12;
}
}
```
In the above code, the `Account` interface represents the context object, while the `State` interface represents the state objects. The `SavingsAccount` class is a concrete context class that implements the `Account` interface and keeps track of the current state object. The `YearsSetUpState` and `MonthsSetUpState` classes are concrete state classes that implement the `State` interface and provide specific behavior for each state.
The `selectState()` method is implemented in the `SavingsAccount` class and allows the current state object to be changed. The `deposit()` and `withdraw()` methods use the current state object to adjust the deposit and withdrawal amounts accordingly. The `getBalance()` method returns the current balance of the account.
The `getMonthSetUpState()` method is also implemented in the `SavingsAccount` class and returns a new `MonthsSetUpState` object, which can be used to change the current state object to adjust the deposit and withdrawal amounts from years to months.
阅读全文