Skip to content

Conversation

@gvatsal60
Copy link
Owner

This pull request introduces a complete implementation and documentation of the Factory Method design pattern in C++. It adds well-structured header and source files, including clear class definitions, usage example, and comprehensive documentation. Additionally, it provides a concise Markdown overview explaining the pattern, its use cases, benefits, and drawbacks. There is also a minor update to the devcontainer configuration.

Factory Method Pattern Implementation:

  • Added a new header file factory_method.hpp containing:
    • Abstract product (Transport) and concrete products (Truck, Ship)
    • Abstract creator (Logistics) and concrete creators (RoadLogistics, SeaLogistics)
    • A LogisticService class to demonstrate usage
    • Extensive documentation and license header
  • Added a new source file factory_method.cpp with:
    • Example usage of the factory method pattern, instantiating both road and sea logistics and planning deliveries

Documentation:

  • Added index.md with an overview of the Factory Method pattern, including key concepts, when to use it, benefits, drawbacks, and a conclusion

Development Environment:

  • Removed the llvm-vs-code-extensions.vscode-clangd extension from .devcontainer/devcontainer.json to update the recommended VS Code extensions

@gvatsal60 gvatsal60 self-assigned this Nov 2, 2025
Copilot AI review requested due to automatic review settings November 2, 2025 14:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a Factory Method design pattern implementation with documentation, example code, and removes an unused extension from the devcontainer configuration.

  • Adds comprehensive documentation explaining the Factory Method pattern's concepts, use cases, benefits, and drawbacks
  • Implements the Factory Method pattern in C++ with Transport/Logistics example demonstrating the pattern
  • Removes the unused llvm-vs-code-extensions.vscode-clangd extension from devcontainer configuration

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
design_patterns/creational/factory_method/index.md Documentation for the Factory Method pattern including key concepts, use cases, benefits, and drawbacks
design_patterns/creational/factory_method/factory_method.hpp Header file implementing the Factory Method pattern with Transport and Logistics classes
design_patterns/creational/factory_method/factory_method.cpp Implementation file with main function demonstrating the pattern usage
.devcontainer/devcontainer.json Removes unused clangd extension from the devcontainer

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +77 to +78
public:
Transport *createTransport() const override {
Copy link

Copilot AI Nov 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a raw pointer from a local std::shared_ptr causes a dangling pointer. When truckPtr goes out of scope, the managed object is destroyed, leaving the returned pointer invalid. Return new Truck() directly or change the return type to std::unique_ptr<Transport>.

Copilot uses AI. Check for mistakes.
Comment on lines +88 to +89
if (logistics == nullptr) {
throw std::invalid_argument("Logistics pointer cannot be null");
Copy link

Copilot AI Nov 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a raw pointer from a local std::shared_ptr causes a dangling pointer. When shipPtr goes out of scope, the managed object is destroyed, leaving the returned pointer invalid. Return new Ship() directly or change the return type to std::unique_ptr<Transport>.

Copilot uses AI. Check for mistakes.
* Class Methods Definitions
**************************************************************************************/

int main(int argc, char const *argv[]) {
Copy link

Copilot AI Nov 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argc and argv parameters are declared but never used. Since this is a simple demonstration with no command-line argument processing, use int main() instead to match the style in the singleton example.

Suggested change
int main(int argc, char const *argv[]) {
int main() {

Copilot uses AI. Check for mistakes.
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 2, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants