Skip to content

Instantly share code, notes, and snippets.

View semlinker's full-sized avatar
👊
Fighting

阿宝哥 semlinker

👊
Fighting
View GitHub Profile
@semlinker
semlinker / command.ts
Created November 30, 2022 01:51
Command Pattern
interface Command {
name: string;
execute(args: any): any;
}
class OpenUrlCommand implements Command {
name = "openUrl";
execute(args: any) {
console.log(`Open url: ${args[0]}`);
}
@semlinker
semlinker / strategy.ts
Created November 30, 2022 01:50
Strategy Patterns
interface Strategy {
authenticate(args: any[]): boolean;
}
class Authenticator {
strategies: Record<string, Strategy> = {};
use(name: string, strategy: Strategy) {
this.strategies[name] = strategy;
}
@semlinker
semlinker / localstorage-proxy.ts
Created October 28, 2022 04:28
LocalStorageProxy
type CacheItem = {
now: number;
value: string;
maxAge: number;
};
class LocalStorageProxy {
setItem(key: string, value: unknown, maxAge: number = 0) {
localStorage.setItem(
key,
@semlinker
semlinker / http-service-proxy.ts
Created October 27, 2022 23:41
Proxy Pattern in TypeScript
class HttpServiceProxy {
private httpService: HttpService;
private dataCache: Map<string, any> = new Map();
constructor() {
this.httpService = new HttpService();
}
async sendRequest(method: string, url: string, body?: BodyInit) {
const cacheKey = method + ":" + url;
@semlinker
semlinker / client.ts
Created October 24, 2022 05:24
Facade Pattern in TypeScript
class Client {
private shapeFacade: ShapeFacade;
constructor() {
this.shapeFacade = new ShapeFacade();
}
drawShapes() {
this.shapeFacade.drawCircle();
this.shapeFacade.drawRectangle();
this.shapeFacade.drawTriangle();
@semlinker
semlinker / shape-facade.ts
Created October 24, 2022 04:41
Facade Pattern in TypeScript
class ShapeFacade {
private circle: Shape;
private rectangle: Shape;
private triangle: Shape;
constructor() {
this.circle = new Circle();
this.rectangle = new Rectangle();
this.triangle = new Triangle();
}
@semlinker
semlinker / shape.ts
Created October 24, 2022 04:40
Facade Pattern in TypeScript
interface Shape {
draw(): void;
}
class Circle implements Shape {
draw(): void {
console.log("Draw circle");
}
}
@semlinker
semlinker / flyweight-pattern.ts
Created October 23, 2022 15:23
Design Patterns: Flyweight Pattern in TypeScript
class UPhoneFlyweight {
constructor(model: string, screen: number, memory: number) {}
}
class FlyweightFactory {
public phonesMap: Map<string, UPhoneFlyweight> = new Map();
public get(model: string, screen: number, memory: number): UPhoneFlyweight {
const key = model + screen + memory;
if (!this.phonesMap.has(key)) {
@semlinker
semlinker / builder.js
Created October 18, 2022 11:23
Proxy API usage scenarios —— Builder API
function Builder(typeOrTemplate, template) {
let type;
if (typeOrTemplate instanceof Function) {
type = typeOrTemplate;
} else {
template = typeOrTemplate;
}
const built = template ? Object.assign({}, template) : {};
const builder = new Proxy(
{},
@semlinker
semlinker / sandbox.js
Created October 18, 2022 10:48
Proxy API usage scenarios —— Sandbox
function sandbox(code) {
code = "with (sandbox) {" + code + "}";
const fn = new Function("sandbox", code);
return function (sandbox) {
const sandboxProxy = new Proxy(sandbox, {
has(target, key) {
return true;
},
get(target, key) {