Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6242827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:01:37+00:00 2026-05-24T12:01:37+00:00

I am searching for a design pattern to generate objects from a template object.

  • 0

I am searching for a design pattern to generate objects from a template object. In my game i have different kind of items which will be generated out of a template object.

Currently i solved it by inheritance where each template entity implements the method:

   public Item generate(EntityManager em);

My inheritance structure is:

    ItemTemplate                  -> creates an Item
         |_ ArmorTemplate         -> creates and Armor
         |_ WeaponTemplate        -> creates an Weapon
         |_ ....and more ....

When i create a item i pass the entity manager to the template and i get a persisted object.
E.g. an ArmorTemplate returns me a persisted Armor and so on.

If the item generation needs any specific entities i have to do a lookup inside the generation method. I would like to solve it with a container based solution where i can inject my needed EJBs and do not have to pass my entity manager through the creation workflow.

From Hibernate i get a List of template objects and i do not want to do a instanceof to call the correct factory method.

My current solution works, but feels not very elegant. Any input would be fine. 😉

Thanks in advance.
greetings, m

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:01:38+00:00Added an answer on May 24, 2026 at 12:01 pm

    It is not possible to inject EJBs into the template objects as long they are domain objects with Java EE:

    Inject EJB into domain object with Java EE 6

    It is possible with spring:

    http://java.dzone.com/articles/domain-object-dependency-injection-with-spring

    I would however probably try to split the template domain object from the object persiting part. (just for my understanding: the ArmorTemplate is some kind of armor and there are multiple different with varying attributes, each ArmorItem is one instance of exactly one ArmorTemplate and there can be multiple items for one template)

    Here is a suggestion. The complicated generic part is to enable, that an ArmorItem can only by build by an ArmorTemplate and that you can access specific properties from this template without cast.

    static abstract class Item<I extends Item<I, T>, T extends ItemTemplate<I, T>> {
        T template;
    }
    static abstract class ItemTemplate<I extends Item<I, T>, T extends ItemTemplate<I, T>> {
        abstract I createItem();
    }
    
    // JPA Domain Objects:
    static class Armor extends Item<Armor, ArmorTemplate> {}
    static class ArmorTemplate extends ItemTemplate<Armor, ArmorTemplate> {
        public final int hitpoints;
    
        public ArmorTemplate(int hitpoints) {
            this.hitpoints = hitpoints;
        }
    
        @Override
        Armor createItem() {
            return new Armor();
        }
    }
    
    // This POJO can be CDI managed
    static class ItemDao {
        // @Inject public EntityManager em;
    
        public <I extends Item<I, T>, T extends ItemTemplate<I, T>> 
        List<I> createItemsFromTemplates(List<T> templates) {
            List<I> result = new ArrayList<I>();
            for (T template : templates) {
                I item = template.createItem();
                item.template = template;
                // em.persist(item);
                result.add(item);
            }
            return result;
        }
    }
    
    public static void main(String[] args) {
        List<ArmorTemplate> armorTemplates = Arrays.asList(
                new ArmorTemplate(100), new ArmorTemplate(80));
    
        List<Armor> armorItems = new ItemDao().createItemsFromTemplates(armorTemplates);
    
        for (Armor armor : armorItems) {
            System.out.println(armor.template.hitpoints);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When searching online for object oriented concepts such as the composite design pattern I
i'm coming into C++ from Java, and have a common design situation in which
Problem: I have to design an algorithm, which does the following for me: Say
I am searching for a design pattern, open source platform or what can else
Searching from google.com, like www.abc.com Search Result Rank the pages like Title..... Description... www.abc.com
Searching online, I have found the following routine for calculating the sign of a
Im searching a tool which allows me to specify some folders as bookmarks and
I am searching for a good mode to develop a application which shows 7
I have this drupal website that revolves around a document database. By design you
I have an application where a multiple shops will share the data. There is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.