Skip to content

Instantly share code, notes, and snippets.

View mcsee's full-sized avatar
🏠
Working from home

mcsee mcsee

🏠
Working from home
View GitHub Profile
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
public class Person {
private List<String> hobbies;
public Person(List<String> hobbies) {
this.hobbies = hobbies;
}
public List<String> getHobbies() {
return hobbies;
}
// 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();
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,
public class TransactionService {
private EntityManager entityManager;
public TransactionService(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void saveTransaction(Transaction transaction) {
entityManager.getTransaction().begin();
entityManager.persist(transaction);
// Domain classes
abstract class Transaction {
private String id;
private double amount;
}
class BankTransaction extends Transaction {
private String bankName;
}
<?php
class AccessControlPanel {
private $users = [];
// 1. Make a contextual copy of the repeated code
private function createUser(
$username,
<?php
class AccessControlPanel {
private $users = [];
public function createRegularUser($username, $password, $email) {
$user = [
"username" => $username,
"email" => $email,
@mcsee
mcsee / blackhole.cs
Created June 9, 2024 19:40
Gemini Black Hole
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);
// 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.
*/