Skip to content

Instantly share code, notes, and snippets.

Avatar

Scotty Jamison theScottyJam

View GitHub Profile
@theScottyJam
theScottyJam / strategyPattern.js
Last active Nov 10, 2021
Article: Composition Alone Can't Replace Inheritance - strategy pattern
View strategyPattern.js
class Monster {
#onAttack
constructor({ dropLoot, onAttack, customBehaviors = {} }) {
this.dropLoot = dropLoot
this.#onAttack = onAttack ?? (() => {})
this.behaviors = customBehaviors
}
attack() {
playHurtAnimation()
@theScottyJam
theScottyJam / runtimeHierarchyCheck.js
Created Oct 25, 2021
Article: Composition Alone Can't Replace Inheritance - runtime hierarchy check
View runtimeHierarchyCheck.js
const monsterSentinel = {}
export const isMonster = obj => obj.typeSentinel === monsterSentinel
export class Slime {
typeSentinel = monsterSentinel
...
}
export class Skeleton {
@theScottyJam
theScottyJam / publicHelpers.js
Created Oct 25, 2021
Article: Composition Alone Can't Replace Inheritance - public helpers
View publicHelpers.js
// Public helpers
export const monsterBehaviors = {
kill(monster) {
userInventory.add(monster.dropLoot())
removeFromUi(this)
}
}
// private helpers
const monsterHelpers = {
@theScottyJam
theScottyJam / restrictedInheritance.js
Last active Nov 10, 2021
Article: Composition Alone Can't Replace Inheritance - restricted inheritance
View restrictedInheritance.js
const monsterHelpers = {
attack() {
playHurtAnimation()
}
}
class MonsterBehaviors {
#id = Math.random()
#dropLoot
@theScottyJam
theScottyJam / classicComposition.js
Last active Nov 10, 2021
Article: Composition Alone Can't Replace Inheritance - classic composition
View classicComposition.js
class MonsterBehaviors {
#dropLoot
constructor({ dropLoot }) {
this.#dropLoot = dropLoot
}
attack() {
playHurtAnimation()
}
@theScottyJam
theScottyJam / helperFunctions.js
Last active Jul 19, 2022
Article: Composition Alone Can't Replace Inheritance - helper functions
View helperFunctions.js
const monsterBehaviors = {
attack() {
playHurtAnimation()
},
die({ monsterId, dropLoot }) {
userInventory.add(dropLoot())
removeFromUi(monsterId)
},
}
@theScottyJam
theScottyJam / originalInheritanceExample.js
Last active Oct 25, 2021
Article: Composition Alone Can't Replace Inheritance - original inheritance example
View originalInheritanceExample.js
export class Monster {
constructor() {
if (this.constructor === Monster) {
throw new Error('This is an abstract class. Do not instantiate it directly.');
}
}
attack() {
playHurtAnimation()
}
@theScottyJam
theScottyJam / exceptions.js
Last active Oct 13, 2021
Light version of the explicit-exceptions API. Designed for those would rather maintain a simplified version of the library than add it as a dependency. This gist is licensed under the MIT license.
View exceptions.js
'use strict'
// This module provides a light version of the explicit-exceptions API.
// You can find the original package this module was modeled after
// at this URL: https://www.npmjs.com/package/explicit-exceptions
/**
* An exception that can be thrown (or rethrown) within a function decorated with wrap().
*/
class Exception extends Error {
@theScottyJam
theScottyJam / new.py
Last active Sep 27, 2021
This adds a "new file" menu item to the nautilus right-click menu for when you right click on a file. Place this file inside `~/.local/share/nautilus/scripts` and name it `new`. This script is useful for when you are using nautilus in list view, because nautilus currently doesn't show this menu item when right clicking on files.
View new.py
#!/usr/bin/env python3
# https://superuser.com/a/1251013/57284
# https://gist.github.com/theScottyJam/ae19299c8d71a21c75f2abd96daea9f8
from pathlib import Path
import tkinter as tk
from tkinter import ttk
import os
import shutil