{"id":20940559,"url":"https://github.com/showrin/design-pattern","last_synced_at":"2026-04-27T13:32:58.373Z","repository":{"id":122268408,"uuid":"289741954","full_name":"Showrin/design-pattern","owner":"Showrin","description":"A test on different design patterns","archived":false,"fork":false,"pushed_at":"2020-08-25T15:12:38.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-19T20:57:47.052Z","etag":null,"topics":["design-patterns","javascript","oop","typescript"],"latest_commit_sha":null,"homepage":"https://github.com/Showrin/design-pattern#design-pattern","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Showrin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-23T18:14:00.000Z","updated_at":"2023-08-27T21:44:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"6faf1dd0-e6f3-45b8-bc85-89473fe4ab27","html_url":"https://github.com/Showrin/design-pattern","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Showrin%2Fdesign-pattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Showrin%2Fdesign-pattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Showrin%2Fdesign-pattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Showrin%2Fdesign-pattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Showrin","download_url":"https://codeload.github.com/Showrin/design-pattern/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243332527,"owners_count":20274504,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["design-patterns","javascript","oop","typescript"],"created_at":"2024-11-18T23:10:58.974Z","updated_at":"2025-12-28T13:27:54.080Z","avatar_url":"https://github.com/Showrin.png","language":"TypeScript","readme":"# design-pattern\n\nA test on different design patterns\n\n\u003cbr/\u003e\n\n## Project Setup\n\nFirst install **`Typescript`** globally.\n\n```\n$ npm i -g typescript\n```\n\n\u003cbr/\u003e\n\nClone the repo. To do so, go to the directory where you want to keep this repo. Then open the terminal from here and run the following command.\n\n```\n$ git clone git@github.com:Showrin/design-pattern.git\n```\n\n\u003cbr/\u003e\n\nThen navigate to the repository using this command.\n\n```\n$ cd design-pattern\n```\n\n\u003cbr/\u003e\n\nAfter cloning, if you are using **`yarn`**, install the required modules by running the following command.\n\n```\n$ yarn\n```\n\n\u003cbr/\u003e\n\nOr use the following command if you are using **`npm`**.\n\n```\n$ npm install\n```\n\n\u003cbr/\u003e\n\nThen run the following command and you will get products that are created using different design patterns.\n\n```\n$ yarn start\n```\n\nor\n\n```\n$ npm start\n```\n\n### Console Output\n\n![Design-pattern-output-by-Showrin](https://i.imgur.com/QKBEmQd.png)\n\n\u003cbr/\u003e\n\n## Dev Dependencies\n\nThis program has following development dependencies.\n\n| Module Name           | Version | Why it's used                                            |\n| --------------------- | ------- | -------------------------------------------------------- |\n| @types/node           | ^14.6.0 | It's being used for using `require` syntax in typescript |\n| chalk                 | ^4.1.0  | It's being used for giving colors to console texts       |\n| console-table-printer | ^2.3.17 | It's being used for printing data in console as a table  |\n\n\u003cbr/\u003e\n\n## Procedure: Factory Design Pattern\n\n\u003e **By analayzing the given interface, it seems to be following the Factory Design Pattern. Here's the procedure of creating `Car` and `Plane` class, using this design pattern.**\n\n\u003cbr/\u003e\n\nFirst, the interface for `Vehicle` is implemented.\n\n```\ninterface Vehicle {\n  set_num_of_wheels(): number;\n  set_num_of_passengers(): number;\n  has_gas(): boolean;\n}\n```\n\n\u003cbr /\u003e\n\nThen, `Car` and `Plane` classes are implemented using the `Vehicle` interface.\n\n```\nclass Car implements Vehicle {\n  set_num_of_wheels() {\n    return 4;\n  }\n\n  set_num_of_passengers() {\n    return 4;\n  }\n\n  has_gas() {\n    return true;\n  }\n}\n```\n\n```\nclass Plane implements Vehicle {\n  set_num_of_wheels() {\n    return 14;\n  }\n\n  set_num_of_passengers() {\n    return 100;\n  }\n\n  has_gas() {\n    return false;\n  }\n}\n```\n\n\u003cbr /\u003e\n\nThen, the `VehicleFactory` class is implemented **that defines which vehicle should be created**.\n\n```\nclass VehicleFactory {\n  getCar(): Car {\n    return new Car();\n  }\n\n  getPlane(): Plane {\n    return new Plane();\n  }\n}\n```\n\n\u003cbr /\u003e\n\nAnd now, it's time to create `Car` and `Plane` from the Factory Design Pattern.\n\n```\nimport { Table } from \"console-table-printer\";\nconst chalk = require(\"chalk\");\n\nconst vehicleFactory: VehicleFactory = new VehicleFactory();\nconst ourCar: Car = vehicleFactory.getCar();\nconst ourPlane: Plane = vehicleFactory.getPlane();\n\nconst table = new Table();\n\ntable.addRow(\n  {\n    Product: \"Car\",\n    set_num_of_wheels: ourCar.set_num_of_wheels(),\n    set_num_of_passengers: ourCar.set_num_of_passengers(),\n    has_gas: ourCar.has_gas(),\n  },\n  { color: \"red\" }\n);\n\ntable.addRow(\n  {\n    Product: \"Plane\",\n    set_num_of_wheels: ourPlane.set_num_of_wheels(),\n    set_num_of_passengers: ourPlane.set_num_of_passengers(),\n    has_gas: ourPlane.has_gas(),\n  },\n  { color: \"green\" }\n);\n\nconsole.log(\n  chalk.bold.red(\n    \"\\n### These products are created using [\" +\n      chalk.underline(\"Factory Pattern\") +\n      \"] ###\"\n  )\n);\n\ntable.printTable();\n```\n\n### Output\n\n![Factory-Pattern-Output-by-Showrin](https://i.imgur.com/wmM7iAo.png)\n\n\u003cbr/\u003e\n\n## Procedure: Abstract Factory Design Pattern\n\n\u003e **`Car` and `Plane` class can be also created using Abstract Factory Design Pattern. Here's the procedure of creating `Car` and `Plane` class, using this design pattern.**\n\n\u003cbr /\u003e\n\nFirst, interfaces for `VehicleFactory`, `Car` \u0026 `Plane` are implemented.\n\n```\ninterface VehicleFactoryInterface {\n  createCar(): Car;\n  createPlane(): Plane;\n}\n```\n\n```\ninterface CarInterface {\n  set_num_of_wheels(): number;\n  set_num_of_passengers(): number;\n  has_gas(): boolean;\n}\n```\n\n```\ninterface PlaneInterface {\n  set_num_of_wheels(): number;\n  set_num_of_passengers(): number;\n  has_gas(): boolean;\n}\n```\n\n\u003cbr /\u003e\n\nThen, `Car` and `Plane` classes are implemented.\n\n```\nclass Car implements CarInterface {\n  set_num_of_wheels() {\n    return 4;\n  }\n\n  set_num_of_passengers() {\n    return 4;\n  }\n\n  has_gas() {\n    return true;\n  }\n}\n```\n\n```\nclass Plane implements PlaneInterface {\n  set_num_of_wheels() {\n    return 14;\n  }\n\n  set_num_of_passengers() {\n    return 100;\n  }\n\n  has_gas() {\n    return false;\n  }\n}\n```\n\n\u003cbr /\u003e\n\nAfter that, the `VehicleFactory` class is implemented and it decides which class should be created.\n\n```\nclass VehicleFactory implements VehicleFactoryInterface {\n  createCar(): Car {\n    return new Car();\n  }\n\n  createPlane(): Plane {\n    return new Plane();\n  }\n}\n```\n\n\u003cbr /\u003e\n\nAnd now, it's time to create `Car` and `Plane` from the Abstract Factory Design Pattern.\n\n```\nimport { Table } from \"console-table-printer\";\nconst chalk = require(\"chalk\");\n\nconst vehicleFactory: VehicleFactory = new VehicleFactory();\nconst ourCar: Car = vehicleFactory.createCar();\nconst ourPlane: Plane = vehicleFactory.createPlane();\n\nconst table = new Table();\n\ntable.addRow(\n  {\n    Product: \"Car\",\n    set_num_of_wheels: ourCar.set_num_of_wheels(),\n    set_num_of_passengers: ourCar.set_num_of_passengers(),\n    has_gas: ourCar.has_gas(),\n  },\n  { color: \"red\" }\n);\n\ntable.addRow(\n  {\n    Product: \"Plane\",\n    set_num_of_wheels: ourPlane.set_num_of_wheels(),\n    set_num_of_passengers: ourPlane.set_num_of_passengers(),\n    has_gas: ourPlane.has_gas(),\n  },\n  { color: \"green\" }\n);\n\nconsole.log(\n  chalk.bold.red(\n    \"\\n### These products are created using [\" +\n      chalk.underline(\"Abstract Factory Pattern\") +\n      \"] ###\"\n  )\n);\n\ntable.printTable();\n```\n\n### Output\n\n![Abstract-Factory-Pattern-Output-by-Showrin](https://i.imgur.com/dPaLwj3.png)\n\n\u003cbr/\u003e\n\n\u003e **Actually, `Abstract Factory Pattern` is similar to `Factory Pattern`. The difference is that all products have different interfaces in this design pattern.**\n\n\u003cbr /\u003e\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshowrin%2Fdesign-pattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshowrin%2Fdesign-pattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshowrin%2Fdesign-pattern/lists"}