This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
private List<String> hobbies; | |
public Person(List<String> hobbies) { | |
this.hobbies = new ArrayList<>(hobbies); | |
} | |
public List<String> hobbies() { | |
// This returns a shallow copy | |
// This is usually not a big performance issue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
private List<String> hobbies; | |
public Person(List<String> hobbies) { | |
this.hobbies = hobbies; | |
} | |
public List<String> getHobbies() { | |
return hobbies; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Step 1: Find or Create a Polymorphic Hierarchy | |
abstract class MicrophoneState { } | |
final class On extends MicrophoneState { } | |
final class Off extends MicrophoneState { } | |
// Step 2: Move the Body of Each IF to the Corresponding Abstraction | |
abstract class MicrophoneState { | |
public abstract String polymorphicMethodFromIf(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String handleMicrophoneState(String state) { | |
if (state.equals("off")) { | |
return "Microphone is off"; | |
} else { | |
return "Microphone is on"; | |
} | |
} | |
/* The constant representing the 'off' state is | |
duplicated throughout the code, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TransactionService { | |
private EntityManager entityManager; | |
public TransactionService(EntityManager entityManager) { | |
this.entityManager = entityManager; | |
} | |
public void saveTransaction(Transaction transaction) { | |
entityManager.getTransaction().begin(); | |
entityManager.persist(transaction); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Domain classes | |
abstract class Transaction { | |
private String id; | |
private double amount; | |
} | |
class BankTransaction extends Transaction { | |
private String bankName; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AccessControlPanel { | |
private $users = []; | |
// 1. Make a contextual copy of the repeated code | |
private function createUser( | |
$username, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AccessControlPanel { | |
private $users = []; | |
public function createRegularUser($username, $password, $email) { | |
$user = [ | |
"username" => $username, | |
"email" => $email, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlackHole | |
{ | |
public double Mass { get; private set; } | |
public double SchwarzschildRadius { get; private set; } | |
public bool HasSingularity { get; } // Always true for a black hole | |
public BlackHole(double mass) | |
{ | |
Mass = mass; | |
SchwarzschildRadius = CalculateSchwarzschildRadius(mass); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define the gravitational constant | |
const G = 6.67430e-11; // m^3 kg^-1 s^-2 | |
/** | |
* Calculate the gravitational force between two masses. | |
* @param {number} m1 - Mass of the first object in kilograms. | |
* @param {number} m2 - Mass of the second object in kilograms. | |
* @param {number} r - Distance between the centers of the masses in meters. | |
* @returns {number} - Gravitational force in Newtons. | |
*/ |
NewerOlder