Skip to content

Instantly share code, notes, and snippets.

@louismullie
Last active July 27, 2024 17:45
Show Gist options
  • Save louismullie/0228cb30b0822e11fb253aaade9cf46d to your computer and use it in GitHub Desktop.
Save louismullie/0228cb30b0822e11fb253aaade9cf46d to your computer and use it in GitHub Desktop.
Cell biology design patterns
### Cell Biology Patterns
1. Singleton (Cell Nucleus):
```typescript
class Nucleus {
private static instance: Nucleus;
private constructor() {}
static getInstance(): Nucleus {
if (!Nucleus.instance) {
Nucleus.instance = new Nucleus();
}
return Nucleus.instance;
}
controlCell(): void {
console.log("Controlling cell activities");
}
}
```
2. Factory Method (Ribosomes):
```typescript
abstract class Ribosome {
abstract synthesizeProtein(mRNA: string): Protein;
}
class Protein {}
class EukaryoticRibosome extends Ribosome {
synthesizeProtein(mRNA: string): Protein {
return new Protein();
}
}
```
3. Abstract Factory (Cell Differentiation):
```typescript
interface CellFactory {
createNucleus(): Nucleus;
createMitochondria(): Mitochondria;
}
class StemCell implements CellFactory {
createNucleus(): Nucleus { return new Nucleus(); }
createMitochondria(): Mitochondria { return new Mitochondria(); }
}
class NeuronCell implements CellFactory {
createNucleus(): Nucleus { return new NeuronNucleus(); }
createMitochondria(): Mitochondria { return new NeuronMitochondria(); }
}
```
4. Builder (Protein Synthesis):
```typescript
class ProteinBuilder {
private sequence: string = '';
addAminoAcid(aa: string): this {
this.sequence += aa;
return this;
}
build(): Protein {
return new Protein(this.sequence);
}
}
```
5. Prototype (Cell Division):
```typescript
class Cell implements Cloneable {
constructor(public dna: string) {}
clone(): Cell {
return new Cell(this.dna);
}
mutate(): void {
// Implement mutation logic
}
}
```
6. Adapter (Enzyme-Substrate Interactions):
```typescript
interface Substrate { react(): void; }
class Glucose implements Substrate {
react(): void { console.log("Glucose reacting"); }
}
class Enzyme {
process(substrate: Substrate): void {
substrate.react();
}
}
```
7. Bridge (Signal Transduction):
```typescript
interface SignalReceiver {
receiveSignal(signal: string): void;
}
class CellResponse {
respond(): void {
console.log("Cell responding to signal");
}
}
class SignalTransducer implements SignalReceiver {
constructor(private response: CellResponse) {}
receiveSignal(signal: string): void {
console.log(`Received signal: ${signal}`);
this.response.respond();
}
}
```
8. Composite (Multicellular Organisms):
```typescript
abstract class BiologicalUnit {
abstract function(): void;
}
class Cell extends BiologicalUnit {
function(): void {
console.log("Cell functioning");
}
}
class Organ extends BiologicalUnit {
private parts: BiologicalUnit[] = [];
add(part: BiologicalUnit): void {
this.parts.push(part);
}
function(): void {
this.parts.forEach(part => part.function());
}
}
```
9. Decorator (Post-translational Modifications):
```typescript
interface Protein {
function(): void;
}
class BaseProtein implements Protein {
function(): void {
console.log("Base protein function");
}
}
class Phosphorylation implements Protein {
constructor(private protein: Protein) {}
function(): void {
this.protein.function();
console.log("Added phosphate group");
}
}
```
10. Facade (Cell Membrane):
```typescript
class CellMembrane {
transportNutrients(): void {
console.log("Transporting nutrients");
}
signalReception(): void {
console.log("Receiving signals");
}
wasteRemoval(): void {
console.log("Removing waste");
}
}
```
11. Flyweight (tRNA Usage):
```typescript
class tRNA {
constructor(public anticodon: string) {}
}
class tRNAPool {
private tRNAs: {[key: string]: tRNA} = {};
gettRNA(anticodon: string): tRNA {
if (!this.tRNAs[anticodon]) {
this.tRNAs[anticodon] = new tRNA(anticodon);
}
return this.tRNAs[anticodon];
}
}
```
12. Proxy (Nuclear Pore Complex):
```typescript
interface NuclearAccess {
enterNucleus(molecule: Molecule): void;
}
class Nucleus implements NuclearAccess {
enterNucleus(molecule: Molecule): void {
console.log("Molecule entered nucleus");
}
}
class NuclearPoreComplex implements NuclearAccess {
constructor(private nucleus: Nucleus) {}
enterNucleus(molecule: Molecule): void {
if (this.checkMolecule(molecule)) {
this.nucleus.enterNucleus(molecule);
}
}
private checkMolecule(molecule: Molecule): boolean {
return true; // Simplified check
}
}
```
13. Chain of Responsibility (Signal Cascades):
```typescript
abstract class SignalProtein {
protected next: SignalProtein | null = null;
setNext(protein: SignalProtein): SignalProtein {
this.next = protein;
return protein;
}
abstract handle(signal: string): void;
}
class KinaseA extends SignalProtein {
handle(signal: string): void {
if (signal === "hormoneA") {
console.log("KinaseA activated");
} else if (this.next) {
this.next.handle(signal);
}
}
}
```
14. Command (Hormones):
```typescript
interface CellCommand {
execute(): void;
}
class InsulinCommand implements CellCommand {
constructor(private cell: Cell) {}
execute(): void {
this.cell.absorbGlucose();
}
}
class Hormone {
constructor(private command: CellCommand) {}
bind(): void {
this.command.execute();
}
}
```
15. Iterator (DNA Replication):
```typescript
class DNA {
constructor(private sequence: string) {}
[Symbol.iterator]() {
let index = 0;
return {
next: () => {
if (index < this.sequence.length) {
return { value: this.sequence[index++], done: false };
} else {
return { done: true };
}
}
};
}
}
```
16. Mediator (Endocrine System):
```typescript
class Hormone {
constructor(public name: string) {}
}
class EndocrineSystem {
notify(organ: Organ, hormone: Hormone): void {
console.log(`${organ.name} notified of ${hormone.name}`);
}
}
class Organ {
constructor(public name: string, private system: EndocrineSystem) {}
receiveSignal(hormone: Hormone): void {
this.system.notify(this, hormone);
}
}
```
17. Memento (Gene Silencing):
```typescript
class GeneExpression {
constructor(private state: boolean = true) {}
silence(): void { this.state = false; }
express(): void { this.state = true; }
saveState(): GeneMemento {
return new GeneMemento(this.state);
}
restoreState(memento: GeneMemento): void {
this.state = memento.getState();
}
}
class GeneMemento {
constructor(private state: boolean) {}
getState(): boolean { return this.state; }
}
```
18. Observer (Gene Regulation):
```typescript
interface GeneObserver {
update(signal: string): void;
}
class Gene implements GeneObserver {
update(signal: string): void {
console.log(`Gene expression changed due to ${signal}`);
}
}
class TranscriptionFactor {
private observers: GeneObserver[] = [];
addObserver(observer: GeneObserver): void {
this.observers.push(observer);
}
notifyObservers(signal: string): void {
this.observers.forEach(observer => observer.update(signal));
}
}
```
19. State (Cell Cycle Phases):
```typescript
interface CellState {
performAction(cell: Cell): void;
}
class G1Phase implements CellState {
performAction(cell: Cell): void {
console.log("Cell growing");
}
}
class SPhase implements CellState {
performAction(cell: Cell): void {
console.log("DNA replicating");
}
}
class Cell {
private state: CellState;
constructor() {
this.state = new G1Phase();
}
setState(state: CellState): void {
this.state = state;
}
performAction(): void {
this.state.performAction(this);
}
}
```
20. Strategy (Alternative Splicing):
```typescript
interface SplicingStrategy {
splice(preRNA: string): string;
}
class ExonSkipping implements SplicingStrategy {
splice(preRNA: string): string {
return "Skipped exon version";
}
}
class IntronRetention implements SplicingStrategy {
splice(preRNA: string): string {
return "Retained intron version";
}
}
class Spliceosome {
constructor(private strategy: SplicingStrategy) {}
performSplicing(preRNA: string): string {
return this.strategy.splice(preRNA);
}
}
```
21. Template Method (DNA Replication):
```typescript
abstract class DNAReplication {
replicate(): void {
this.unwind();
this.primeSynthesis();
this.elongate();
this.terminateAndLigate();
}
protected unwind(): void {
console.log("Unwinding DNA");
}
protected abstract primeSynthesis(): void;
protected abstract elongate(): void;
protected terminateAndLigate(): void {
console.log("Terminating and ligating");
}
}
class LeadingStrandReplication extends DNAReplication {
protected primeSynthesis(): void {
console.log("Single primer synthesis");
}
protected elongate(): void {
console.log("Continuous elongation");
}
}
```
22. Visitor (Immune System Recognition):
```typescript
interface Cell {
accept(visitor: ImmuneCell): void;
}
class BodyCell implements Cell {
accept(visitor: ImmuneCell): void {
visitor.visit(this);
}
}
interface ImmuneCell {
visit(cell: BodyCell): void;
}
class TCell implements ImmuneCell {
visit(cell: BodyCell): void {
console.log("T Cell checking for antigens");
}
}
```
These snippets provide a simplified representation of how each design pattern might be analogous to cellular processes. They are not meant to be scientifically accurate but rather to illustrate the conceptual similarities between software design patterns and biological systems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment