Initial commit

parents
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ship = void 0;
class Ship {
transfer(country, transferItem) {
console.log(`Груз плывет по морю в ${country}`);
}
}
exports.Ship = Ship;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Truck = void 0;
class Truck {
transfer(country, transferItem) {
console.log(`Груз проехал по суше в страну ${country}`);
}
}
exports.Truck = Truck;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShipCreator = void 0;
const Ship_1 = require("../transport/Ship");
const TransportCreator_1 = require("./TransportCreator");
class ShipCreator extends TransportCreator_1.TransportCreator {
factoryMethod() {
return new Ship_1.Ship();
}
}
exports.ShipCreator = ShipCreator;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransportCreator = void 0;
class TransportCreator {
transfer(country, transferItem) {
const transport = this.factoryMethod();
transport.transfer(country, transferItem);
}
}
exports.TransportCreator = TransportCreator;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TruckCreator = void 0;
const Truck_1 = require("../transport/Truck");
const TransportCreator_1 = require("./TransportCreator");
class TruckCreator extends TransportCreator_1.TransportCreator {
factoryMethod() {
return new Truck_1.Truck();
}
}
exports.TruckCreator = TruckCreator;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ship = void 0;
class Ship {
transfer(country, transferItem) {
console.log(`Груз плывет по морю в ${country}`);
}
}
exports.Ship = Ship;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Truck = void 0;
class Truck {
transfer(country, transferItem) {
console.log(`Груз проехал по суше в страну ${country}`);
}
}
exports.Truck = Truck;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ship = void 0;
class Ship {
transfer(country, transferItem) {
console.log(`Груз плывет по морю в ${country}`);
}
}
exports.Ship = Ship;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Truck = void 0;
class Truck {
transfer(country, transferItem) {
console.log(`Груз проехал по суше в страну ${country}`);
}
}
exports.Truck = Truck;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Context_1 = require("./strategy-pattern/Context");
const AddStrategy_1 = require("./strategy-pattern/strategy/AddStrategy");
const MultiplyStrategy_1 = require("./strategy-pattern/strategy/MultiplyStrategy");
const SubstractStrategy_1 = require("./strategy-pattern/strategy/SubstractStrategy");
function clientCode(creator) {
let isTruck = true;
const country = "Kazakhstan";
const goods = ["bmw", "zeekr"];
creator.transfer(country, goods);
// if (isTruck) {
// const truck = new Truck();
// truck.transfer(country, goods);
// } else {
// const ship = new Ship();
// ship.transfer(country, goods);
// }
}
// console.log('Запускается реализация для сухопутной перевозки');
// clientCode(new TruckCreator());
// console.log('Запускается реализация для морской перевозки');
// clientCode(new ShipCreator());
// const doc: UserDocument = new UserDocument("admin");
// doc.sendForModeration();
// doc.sendForModeration();
// doc.sendForPublication();
// doc.sendForDraft();
let action = "multiply";
let context = new Context_1.Context();
if (action === "add") {
context.setStrategy(new AddStrategy_1.AddStrategy());
}
else if (action === "subsctract") {
context.setStrategy(new SubstractStrategy_1.SubstractStrategy());
}
else {
context.setStrategy(new MultiplyStrategy_1.MultiplyStrategy());
}
const result = context.execute(5, 3);
console.log(result);
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserDocument = void 0;
const DraftState_1 = require("./state/DraftState");
class UserDocument {
constructor(user) {
this._currentUser = user;
this.changeState(new DraftState_1.DraftState());
}
changeState(state) {
this._state = state;
this._state.setDocument(this);
}
get currentUser() {
return this._currentUser;
}
sendForDraft() {
this._state.draft();
}
sendForModeration() {
this._state.moderate();
}
sendForPublication() {
this._state.publish();
}
}
exports.UserDocument = UserDocument;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DraftState = void 0;
const ModeratedState_1 = require("./ModeratedState");
const State_1 = require("./State");
class DraftState extends State_1.State {
draft() {
console.log("Документ находится в подготовке");
}
moderate() {
console.log("Документ отправлен на модерацию");
this.document.changeState(new ModeratedState_1.ModeratedState());
}
publish() {
console.log("Документ не прошел модерацию и не может быть опубликован");
}
}
exports.DraftState = DraftState;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModeratedState = void 0;
const DraftState_1 = require("./DraftState");
const PublishedState_1 = require("./PublishedState");
const State_1 = require("./State");
class ModeratedState extends State_1.State {
draft() {
console.log('Документ отправлен на повторную подготовку');
this.document.changeState(new DraftState_1.DraftState());
}
moderate() {
console.log('Документ уже находится в модерации');
}
publish() {
if (this.document.currentUser === "admin") {
console.log('Отправляем документ на публикацию');
this.document.changeState(new PublishedState_1.PublishedState());
}
else {
console.log('У вас нет прав на публикацию');
}
}
}
exports.ModeratedState = ModeratedState;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PublishedState = void 0;
const State_1 = require("./State");
class PublishedState extends State_1.State {
draft() {
console.log("Документ уже опубликован, нет возможности отправить на подготовку");
}
moderate() {
console.log("Документ уже опубликован, нет возможности отправить на модерацию");
}
publish() {
console.log("Документ уже опубликован");
}
}
exports.PublishedState = PublishedState;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.State = void 0;
class State {
setDocument(document) {
this.document = document;
}
}
exports.State = State;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Context = void 0;
class Context {
setStrategy(strategy) {
this._strategy = strategy;
}
execute(a, b) {
return this._strategy.execute(a, b);
}
}
exports.Context = Context;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddStrategy = void 0;
class AddStrategy {
execute(a, b) {
return a + b;
}
}
exports.AddStrategy = AddStrategy;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultiplyStrategy = void 0;
class MultiplyStrategy {
execute(a, b) {
return a * b;
}
}
exports.MultiplyStrategy = MultiplyStrategy;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubstractStrategy = void 0;
class SubstractStrategy {
execute(a, b) {
return a - b;
}
}
exports.SubstractStrategy = SubstractStrategy;
export interface ITransport {
transfer(country: string, transferItem: string[]): void;
}
\ No newline at end of file
import { ITransport } from "../ITransport";
import { Ship } from "../transport/Ship";
import { TransportCreator } from "./TransportCreator";
export class ShipCreator extends TransportCreator {
public factoryMethod(): ITransport {
return new Ship();
}
}
\ No newline at end of file
import { ITransport } from "../ITransport";
export abstract class TransportCreator {
public abstract factoryMethod(): ITransport;
public transfer(country: string, transferItem: string[]) {
const transport = this.factoryMethod();
transport.transfer(country, transferItem);
}
}
\ No newline at end of file
import { ITransport } from "../ITransport";
import { Truck } from "../transport/Truck";
import { TransportCreator } from "./TransportCreator";
export class TruckCreator extends TransportCreator {
public factoryMethod(): ITransport {
return new Truck();
}
}
\ No newline at end of file
import { ITransport } from "../ITransport";
export class Ship implements ITransport {
transfer(country: string, transferItem: string[]): void {
console.log(`Груз плывет по морю в ${country}`);
}
}
\ No newline at end of file
import { ITransport } from "../ITransport";
export class Truck implements ITransport {
transfer(country: string, transferItem: string[]): void {
console.log(`Груз проехал по суше в страну ${country}`);
}
}
\ No newline at end of file
import { ShipCreator } from "./factory-pattern/factory/ShipCreator";
import { TransportCreator } from "./factory-pattern/factory/TransportCreator";
import { TruckCreator } from "./factory-pattern/factory/TruckCreator";
import { UserDocument } from "./state-pattern/UserDocument";
import { Context } from "./strategy-pattern/Context";
import { AddStrategy } from "./strategy-pattern/strategy/AddStrategy";
import { MultiplyStrategy } from "./strategy-pattern/strategy/MultiplyStrategy";
import { SubstractStrategy } from "./strategy-pattern/strategy/SubstractStrategy";
function clientCode(creator: TransportCreator) {
let isTruck = true;
const country = "Kazakhstan";
const goods = ["bmw", "zeekr"];
creator.transfer(country, goods);
// if (isTruck) {
// const truck = new Truck();
// truck.transfer(country, goods);
// } else {
// const ship = new Ship();
// ship.transfer(country, goods);
// }
}
// console.log('Запускается реализация для сухопутной перевозки');
// clientCode(new TruckCreator());
// console.log('Запускается реализация для морской перевозки');
// clientCode(new ShipCreator());
// const doc: UserDocument = new UserDocument("admin");
// doc.sendForModeration();
// doc.sendForModeration();
// doc.sendForPublication();
// doc.sendForDraft();
let action = "add";
let context = new Context();
if (action === "add") {
context.setStrategy(new AddStrategy());
} else if (action === "subsctract") {
context.setStrategy(new SubstractStrategy());
} else {
context.setStrategy(new MultiplyStrategy());
}
const result = context.execute(5, 3);
console.log(result);
import { DraftState } from "./state/DraftState";
import { State } from "./state/State";
export class UserDocument {
private _state!: State;
private _currentUser: string;
constructor(user: string) {
this._currentUser = user;
this.changeState(new DraftState());
}
public changeState(state: State) {
this._state = state;
this._state.setDocument(this);
}
get currentUser(): string {
return this._currentUser;
}
public sendForDraft(): void {
this._state.draft();
}
public sendForModeration(): void {
this._state.moderate();
}
public sendForPublication(): void {
this._state.publish();
}
// public publish() {
// switch(this._state) {
// case 'draft':
// this._state = 'moderation';
// break;
// case 'moderation':
// if (this._currentUser === 'admin') {
// this._state = 'published';
// } else {
// console.log('У вас нет прав');
// }
// break;
// case 'published':
// break;
// }
// }
}
\ No newline at end of file
import { ModeratedState } from "./ModeratedState";
import { State } from "./State";
export class DraftState extends State {
public override draft(): void {
console.log("Документ находится в подготовке");
}
public override moderate(): void {
console.log("Документ отправлен на модерацию");
this.document.changeState(new ModeratedState());
}
public override publish(): void {
console.log("Документ не прошел модерацию и не может быть опубликован");
}
}
import { DraftState } from "./DraftState";
import { PublishedState } from "./PublishedState";
import { State } from "./State";
export class ModeratedState extends State {
public override draft(): void {
console.log('Документ отправлен на повторную подготовку');
this.document.changeState(new DraftState());
}
public override moderate(): void {
console.log('Документ уже находится в модерации');
}
public override publish(): void {
if (this.document.currentUser === "admin") {
console.log('Отправляем документ на публикацию');
this.document.changeState(new PublishedState());
} else {
console.log('У вас нет прав на публикацию');
}
}
}
\ No newline at end of file
import { State } from "./State";
export class PublishedState extends State {
public override draft(): void {
console.log("Документ уже опубликован, нет возможности отправить на подготовку");
}
public override moderate(): void {
console.log("Документ уже опубликован, нет возможности отправить на модерацию");
}
public override publish(): void {
console.log("Документ уже опубликован");
}
}
\ No newline at end of file
import { UserDocument } from "../UserDocument";
export 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;
}
\ No newline at end of file
import { IStrategy } from "./IStrategy";
export class Context {
private _strategy!: IStrategy;
public setStrategy(strategy: IStrategy) {
this._strategy = strategy;
}
execute(a: number, b: number): number {
return this._strategy.execute(a, b);
}
}
\ No newline at end of file
export interface IStrategy {
execute(a: number, b: number): number;
}
\ No newline at end of file
import { IStrategy } from "../IStrategy";
export class AddStrategy implements IStrategy {
execute(a: number, b: number): number {
return a + b;
}
}
\ No newline at end of file
import { IStrategy } from "../IStrategy";
export class MultiplyStrategy implements IStrategy {
execute(a: number, b: number): number {
return a * b;
}
}
\ No newline at end of file
import { IStrategy } from "../IStrategy";
export class SubstractStrategy implements IStrategy {
execute(a: number, b: number): number {
return a - b;
}
}
\ 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