Skip to content

Conversation

@itsmnx
Copy link

@itsmnx itsmnx commented Oct 21, 2025

Description

This PR implements the Factory Method Design Pattern in Java, addressing issue #3367.
The Factory Method pattern allows subclasses to decide which class to instantiate, improving code flexibility, reusability, and maintainability.


Issue

  • Problem: The repository requested a working example of the Factory Method pattern to demonstrate how a class can delegate object creation to subclasses.
  • There was no concrete implementation for the Blacksmith & Weapon example, which was described in the issue.
  • Without this, learners and contributors could not see a practical Java implementation of the pattern.

How This PR Resolves the Issue

  • Created the Blacksmith interface with the factory method manufactureWeapon(WeaponType weaponType).
  • Implemented concrete factories:
    • ElfBlacksmith.java — creates Elven weapons
    • OrcBlacksmith.java — creates Orcish weapons
  • Created Weapon.java and WeaponType.java to represent the product and its types
  • Added Main.java to demonstrate usage, showing that each blacksmith produces the correct weapons dynamically
  • This implementation provides a working, compilable, and testable example of the Factory Method pattern in Java

Key Highlights

  • Subclasses decide the type of object to create
  • Encapsulates object creation logic, improving maintainability
  • Demonstrates real-world analogy: Blacksmiths produce weapons according to their type

Files Added

FactoryMethodPattern/
├── Blacksmith.java
├── ElfBlacksmith.java
├── OrcBlacksmith.java
├── Weapon.java
├── WeaponType.java
└── Main.java


## Example Output
Orc Blacksmith manufactured: Orcish Spear
Orc Blacksmith manufactured: Orcish Axe
Elf Blacksmith manufactured: Elven Spear
Elf Blacksmith manufactured: Elven Axe

Additional Notes

  • Code follows Java best practices and is self-contained for easy testing
  • Ready for inclusion under creational design patterns
  • Contributes to Hacktoberfest 2025 — kindly label as hacktoberfest-accepted

Closes

Closes issue #3367

@github-actions
Copy link

Analyzing changes in this PR...

This might take a few minutes, please wait

📥 Commits

Analyzing changes from base (ede37bd) to latest commit (aacb9bd):

📁 Files being considered (6)

➕ FactoryMethodPattern/Blacksmith.java (1 hunk)
➕ FactoryMethodPattern/ElfBlacksmith.java (1 hunk)
➕ FactoryMethodPattern/Main.java (1 hunk)
➕ FactoryMethodPattern/OrcBlacksmith.java (1 hunk)
➕ FactoryMethodPattern/Weapon.java (1 hunk)
➕ FactoryMethodPattern/WeaponType.java (1 hunk)


autogenerated by presubmit.ai

@sonarqubecloud
Copy link

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.

1 participant