SR Ass Part2
SR Ass Part2
java
Copy code
if (age > 65) {
giveSeniorDiscount();
}
if (hasDisability) {
giveSeniorDiscount();
}
java
Copy code
if (age > 65 || hasDisability) {
giveSeniorDiscount();
}
java
Copy code
if (isMember) {
sendNotification();
applyDiscount();
} else {
sendNotification();
applyExtraCharge();
}
java
Copy code
sendNotification();
if (isMember) {
applyDiscount();
} else {
applyExtraCharge();
}
3. Decompose Conditional
o Problem: Complex conditionals make the code hard to understand.
java
Copy code
if (age > 65 && income < 50000 && isRetired) {
applyTaxRelief();
}
o Solution: Break down the conditional into smaller, meaningful methods.
java
Copy code
if (isEligibleForTaxRelief()) {
applyTaxRelief();
}
java
Copy code
void calculateDiscount(Customer customer) {
if (customer instanceof SeniorCustomer) {
applySeniorDiscount();
} else if (customer instanceof RegularCustomer) {
applyRegularDiscount();
}
}
java
Copy code
abstract class Customer {
abstract void applyDiscount();
}
1. Add Parameter
Problem: A method is hardcoded to handle only one specific case, reducing its
flexibility.
java
Copy code
class Order {
void applyDiscount() {
totalPrice -= 10; // Hardcoded discount
}
}
java
Copy code
class Order {
void applyDiscount(double discountAmount) {
totalPrice -= discountAmount;
}
}
2. Remove Parameter
Problem: A parameter is unnecessary because the value can be derived from existing
fields or state.
java
Copy code
class Employee {
void setSalary(double salary, boolean isFullTime) {
this.salary = isFullTime ? salary : salary * 0.5;
}
}
Solution: Remove the redundant parameter and use the class's existing field to derive the
value.
java
Copy code
class Employee {
boolean isFullTime;
3. Rename Method
Problem: The method name does not clearly communicate its functionality.
java
Copy code
class Customer {
void calc() {
// Calculate total order price
}
}
java
Copy code
class Customer {
void calculateTotalPrice() {
// Calculate total order price
}
}
Problem: A method is both querying data and modifying state, making it harder to
reason about.
java
Copy code
class BankAccount {
double withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
return balance;
}
}
Solution: Separate the query (returning the balance) from the modification (withdrawing
money).
java
Copy code
class BankAccount {
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
}
double getBalance() {
return balance;
}
}
1. Pull Up Field
o Problem: Two or more subclasses have the same field, leading to code
duplication.
java
Copy code
class Teacher {
String name;
}
class Student {
String name;
}
java
Copy code
class Person {
String name;
}
2. Pull Up Method
o Problem: Two or more subclasses have the same method.
java
Copy code
class GasCar {
void startEngine() {
System.out.println("Gas engine starting...");
}
}
class ElectricCar {
void startEngine() {
System.out.println("Electric engine starting...");
}
}
java
Copy code
class Car {
void startEngine() {
System.out.println("Engine starting...");
}
}
java
Copy code
class Desktop {
String model;
Desktop(String model) {
this.model = model;
}
}
class Laptop {
String model;
Laptop(String model) {
this.model = model;
}
}
java
Copy code
class Computer {
String model;
Computer(String model) {
this.model = model;
}
}
java
Copy code
class Employee {
String programmingLanguage; // Only used by developers
String department;
}
java
Copy code
class Employee {
String department;
}
java
Copy code
class Employee {
void writeCode() {
System.out.println("Writing code...");
}
}
o Solution: Move the method to the relevant subclass to make the superclass more
general.
java
Copy code
class Employee {
// No writeCode method here
}