Commit 159dd098 authored by Nurasyl's avatar Nurasyl

Initial commit

parents
node_modules
\ No newline at end of file
This diff is collapsed.
{
"name": "classwork",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon --ignore tests/ --watch src -e ts,tsx --exec ts-node src/index.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/cli-table": "^0.3.4",
"@types/nodemon": "^1.19.6",
"@types/readline-sync": "^1.4.8",
"axios": "^1.6.7",
"cli-table": "^0.3.11",
"nodemon": "^3.1.0",
"readline-sync": "^1.4.10",
"ts-node": "^10.9.2"
},
"devDependencies": {
"@types/node": "^20.11.24"
}
}
import TransportFactory from "./TransportFactory";
import ITransport from "./Transport";
import Truck from "../Transports/Truck";
import Ship from "../Transports/Ship";
import Airplan from "../Transports/Airplane";
import Moto from "../Transports/Moto";
export class TruckCreator extends TransportFactory {
public factoryMethod(): ITransport {
return new Truck();
};
};
export class ShipCreator extends TransportFactory {
public factoryMethod(): ITransport {
return new Ship();
};
};
export class AirplanCreator extends TransportFactory {
public factoryMethod(): ITransport {
return new Airplan();
};
};
export class MotoFactory extends TransportFactory {
public factoryMethod(): ITransport {
return new Moto();
};
};
\ No newline at end of file
interface ITransport {
transfer(country: string, transferItem: string[]): void;
};
export default ITransport;
\ No newline at end of file
import ITransport from "./Transport";
abstract class TransportFactory implements ITransport {
public abstract factoryMethod(): ITransport;
public transfer(country: string, transferItem: string[]): void {
const transport = this.factoryMethod();
transport.transfer(country, transferItem);
};
};
export default TransportFactory;
\ No newline at end of file
import ITransport from "../Creator/Transport";
class Airplan implements ITransport {
transfer(country: string, transferItem: string[]): void {
console.log(`Доставка по воздуху. Место доставки: ${country}. Груз: ${transferItem.join(" ")}.`);
};
};
export default Airplan;
\ No newline at end of file
import ITransport from "../Creator/Transport";
class Moto implements ITransport {
transfer(country: string, transferItem: string[]): void {
console.log(`Доставка по суше на мотоцикле. Место доставки: ${country}. Груз: ${transferItem.join(" ")}.`);
};
};
export default Moto;
\ No newline at end of file
import ITransport from "../Creator/Transport";
class Ship implements ITransport {
transfer(country: string, transferItem: string[]): void {
console.log(`Доставка по морю. Место доставки: ${country}. Груз: ${transferItem.join(" ")}.`);
};
};
export default Ship;
\ No newline at end of file
import ITransport from "../Creator/Transport";
class Truck implements ITransport {
transfer(country: string, transferItem: string[]): void {
console.log(`Доставка по суше. Место доставки: ${country}. Груз: ${transferItem.join(" ")}.`);
};
};
export default Truck;
\ No newline at end of file
import UserDocument from "../UserDocument";
abstract class State {
protected _document!: UserDocument;
public setDocument(document: UserDocument): void {
this._document = document;
};
public abstract draft(): void;
public abstract moderate(): void;
public abstract publish(): void;
};
export default State;
\ No newline at end of file
import State from "../DocumentState/State";
import Moderate from "./Moderate";
class Draft extends State {
public draft(): void {
console.log("Документ уже находится в подготовке");
};
public moderate(): void {
if(this._document.currentUser === "admin") {
console.log("Документ отправлен на модерацию.");
this._document.changeState(new Moderate());
} else {
console.log("У вас нет прав для отправки документа на модерацию");
};
};
public publish(): void {
console.log("Документ не может быть опубликован без модераций");
};
};
export default Draft;
\ No newline at end of file
import State from "../DocumentState/State";
import Publish from "./Publish";
class Moderate extends State {
public draft(): void {
console.log("Документ не может уйти на подготовку после модераций");
};
public moderate(): void {
console.log("Документ уже находится на модерации");
};
public publish(): void {
console.log("Отправляем документ в публикацию");
this._document.changeState(new Publish());
};
};
export default Moderate;
\ No newline at end of file
import State from "../DocumentState/State";
class Publish extends State {
public draft(): void {
console.log(
"Не возможно отправить документ на подготовку, он уже опубликован"
);
}
public moderate(): void {
console.log(
"Не возможно отправить документ на модерацию, он уже опубликован"
);
}
public publish(): void {
console.log("Документ уже опубликован");
}
}
export default Publish;
import State from "./DocumentState/State";
import Draft from "./States/Draft";
class UserDocument {
private _state!: State;
private _currentUser: string;
constructor(user: string) {
this._currentUser = user;
this.changeState(new Draft());
}
public changeState(state: State): void {
this._state = state;
this._state.setDocument(this);
}
get currentUser(): string {
return this._currentUser;
}
public sendForModeration(): void {
this._state.moderate();
}
public submitForPublication(): void {
this._state.publish();
}
public sendForDraft(): void {
this._state.draft();
}
}
export default UserDocument;
import IStrategy from "./Strategy";
class AddStrategy implements IStrategy {
execute(a: number, b: number): number {
return a + b;
};
};
export default AddStrategy;
\ No newline at end of file
import IStrategy from "./Strategy";
class MultiplyStrategy implements IStrategy {
execute(a: number, b: number): number {
return a * b;
};
};
export default MultiplyStrategy;
\ No newline at end of file
interface IStrategy {
execute: (a: number, b: number) => number;
};
export default IStrategy;
\ No newline at end of file
import IStrategy from "./Strategy";
class SubstactStrategy implements IStrategy {
execute(a: number, b: number): number {
return a - b;
};
};
export default SubstactStrategy;
\ No newline at end of file
import AddStrategy from "./Strategy/AddStrategy";
import MultiplyStrategy from "./Strategy/MultiplyStrategy";
import IStrategy from "./Strategy/Strategy";
class Context {
private _strategy!: IStrategy;
public setStrategy(strategy: IStrategy): void {
this._strategy = strategy;
}
execute(a: number, b: number): number {
return this._strategy.execute(a, b);
}
}
const addNumber = new Context();
addNumber.setStrategy(new MultiplyStrategy());
const res = addNumber.execute(4, 4);
console.log(res)
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment