Skip to content

Instantly share code, notes, and snippets.

View AliN11's full-sized avatar
😉
Focusing

Ali Nazari AliN11

😉
Focusing
View GitHub Profile
@AliN11
AliN11 / typescript-dependency-injection.ts
Created September 15, 2019 12:58
Typescript Dependency Injection
interface Wheel {}
interface Engine {}
class Car {
private wheel: Wheel;
private engine: Engine;
public constructor(wheel: Wheel, engine: Engine) {
this.wheel = wheel;
this.engine = engine;
interface Vehicle {
setMode(mode);
move();
}
abstract class Delivery {
public abstract makeVehicle(): Vehicle;
public handle() {
const vehicle = this.makeVehicle();
interface Tablet {
switchOn();
}
interface Smartphone {
switchOn();
ring();
}
class SamsungSmartphone implements Smartphone {
@AliN11
AliN11 / designpatterns-builder.ts
Created April 4, 2021 08:49
Builder Design Pattern with Typescript
interface QueryBuilder {
table(table): QueryBuilder;
select(cols): QueryBuilder;
limit(value: Number): QueryBuilder;
where(col, value): QueryBuilder;
getQuery(): String;
/* +100 Other SQL Methods */
}
interface LanguageInterface {
sayHello(): string;
}
class Persian implements LanguageInterface {
public sayHello(): string {
return 'درود';
}
}
@AliN11
AliN11 / typescript-generics.ts
Created August 15, 2019 04:41
Typescript Generics in classes
class Queue<T> {
private data: T[] = [];
push(item: T) {
this.data.push(item);
}
pop(): T | undefined {
return this.data.shift();
}
class User {
private rawUsername: string;
get userName(): string {
return "The fullname is: " + this.rawUsername;
}
set userName(newName: string) {
if (newName.length > 10) {
newName = newName.substr(0, 10);
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
sayHello(): string {
return `Hello ${this.name}, Welcome to Ditty.ir`;
}
let person = {
name: 'Bastian',
lastname: 'Schweinsteiger'
};
person = JSON.stringify(person);
localStorage.setItem('user', person);
// These two lines:
console.log(y); // undefined
y = "Street";
// are interpreted as:
var y;
console.log(y); // undefined
y = "Street";