java - Extending a static factory method pattern -


i have existing code base uses static factory method design instantiating object types:

public static createsomething(int x, int y, .....) {     // creates object } 

i'd extend method allowing initializing object in different way, based on new parameter(s) dictate how object created.

the simplest (and least scalable) way add new parameters factory method.

this doesn't seem right way doing things:

  1. there's more 1 factory method needs updated, not one.
  2. creating new method force changing client code calls factory. sort of defeats purpose of having single location generates objects.
  3. adding more parameters makes long parameter lists uneasy use.

is there better approach extending factory method ?

create new method, , use parameter object argument (is named pattern?). way, don't change previous implementation, , open new implementation (you change parameter object without affecting existing calls method).

additional thoughts : yes, way, have 2 methods, if right, it's not problem :

public static createsomething(int x, int y, .....) {     /// 1. implementation     /// or     /// 2. return createsomething(new somethingdescription(parameters ...)); }  public static createsomething(somethingdescription description) {     /// 1. return createsomething(description.x, description.y, ...);     /// or     /// 2. implementation } 

but if object complex, , can build in lot of ways, should rather switch builder pattern, suggested sotirios delimanolis.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -