mercredi 10 août 2016

How to create different objects depending on the parameter passed? [duplicate]

This question already has an answer here:

I have a use case like this -

Based on the parameter passed - i have to create an object corresponding to it but the underlying functionality remains same.

public void selectType ()
{
  String type = "ABC";
  publishType(type);
}

public void publishType(String type)
{
if (type =="ABC")
ABCtype publishObject = new ABCtype();

if (type =="XYZ")
XYZtype publishObject = new XYZtype();

publishObject.setfunctionality();
}

What is a better way to approach this? Which design pattern does it fall in?

Another doubt I have is - how to initialize publishObject? It gives an error like this.

Aucun commentaire:

Enregistrer un commentaire