Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Factory Method Pattern Java Implementation #3367#3379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
itsmnx wants to merge1 commit intoiluwatar:master
base:master
Choose a base branch
Loading
fromitsmnx:factory-method-java

Conversation

@itsmnx
Copy link

Description

This PR implements theFactory Method Design Pattern in Java, addressing issue#3367.
The Factory Method pattern allows subclasses to decide which class to instantiate, improvingcode flexibility,reusability, andmaintainability.


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 theBlacksmith & Weapon example, which was described in the issue.
  • Without this, learners and contributors could not see apractical Java implementation of the pattern.

How This PR Resolves the Issue

  • Created theBlacksmith interface with the factory methodmanufactureWeapon(WeaponType weaponType).
  • Implementedconcrete factories:
    • ElfBlacksmith.java — creates Elven weapons
    • OrcBlacksmith.java — creates Orcish weapons
  • CreatedWeapon.java andWeaponType.java to represent the product and its types
  • AddedMain.java todemonstrate usage, showing that each blacksmith produces the correct weapons dynamically
  • This implementation provides aworking, 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
  • Demonstratesreal-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 OutputOrc Blacksmith manufactured: Orcish SpearOrc Blacksmith manufactured: Orcish AxeElf Blacksmith manufactured: Elven SpearElf Blacksmith manufactured: Elven Axe

Additional Notes

  • Code follows Java best practices and is self-contained for easy testing
  • Ready for inclusion undercreational design patterns
  • Contributes toHacktoberfest 2025 — kindly label ashacktoberfest-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 bypresubmit.ai

@sonarqubecloud
Copy link

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

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@itsmnx

[8]ページ先頭

©2009-2025 Movatter.jp