0

I am designing a solution in Java, and I have to create n objects, based on property values. I am using factory design pattern.

Using the classic example of shape and shape types

String myShapes="circle,square,circle,rectangle"  // This is from a property file
for ( shapeType : myShapes.split("," ) {
   ShapeFactory shapeFactory = new ShapeFactory();
   Shape shape1 = shapeFactory.getShape(shapeType ); 
   shape1.doSomething(); 
}

Can someone tell me whether the following is right approach in creating the objects? I don't know how many objects I will have to create during compile time.

3
  • 1
    Looks ok... Can you please clarify why do you think it is a problem? Commented May 26, 2015 at 1:54
  • 1
    Can u post the code for ShapeFactory ? Commented May 26, 2015 at 1:56
  • Its correct provided your getShape method returns a specific sub-type of Shape based on the String passed to it Commented May 26, 2015 at 2:04

1 Answer 1

0

The design looks ok but it needs to include code to handle exceptional situations. I have listed few steps. You will know the limitation of your existing design as you go along with the task.

  • When you need to create excessively large number of objects, your design needs to be memory efficient.

  • Keeping track of the objects (tracking similar objects with same properties)

  • The design needs to account for destroying the objects after their use, since large number of objects (creating them in for loop! is
    going to be memory intensive!).

  • Is it ok if Garbage Collector picks up the object and destroys anytime during the execution? Implementing a finalize block is needed when dealing with large number of objects.

Here are few links on few related topics.

add objects with different name through for loop

http://www.java-forums.org/new-java/25967-loop-create-objects.html

http://www.java-forums.org/new-java/56024-create-new-objects-loop.html

https://www.java.net/node/675897

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.