user19320502
user19320502

Reputation:

Correct Use Of Factory Pattern?

I have been doing research on the factory design pattern and was wondering if the below example, while not "textbook" is technically a correct use of it. Is there another design pattern that may fit the situation better?

public class MySimpleObjectFactory {

    private SomeTransformer someTransformer;

    public MySimpleObjectFactory(SomeTransformer someTransformer){
       this.someTransformer = someTransformer;
    }

    public SimpleObject getSimpleObject(SomeObject someObject){
            PropA propA = someTransformer.transform(someObject);      
            return SimpleObject.builder()
                  .propA(propA)
                  .build();
    }


    public SimpleObject getSimpleObject(SomeObject someObject, AnotherObject anotherObject){
            PropA propA = someTransformer.transform(someObject, anotherObject);      
            return SimpleObject.builder()
                  .propA(propA)
                  .build();
    }

    public SimpleObject getSimpleObject(SomeObject someObject, AnotherObject anotherObject, YetAnotherObject yetAnotherObject){
            PropA propA = someTransformer.transform(someObject, anotherObject, yetAnotherObject);      
            return SimpleObject.builder()
                  .propA(propA)
                  .build();
    }

}

Upvotes: 1

Views: 160

Answers (4)

StepUp
StepUp

Reputation: 38094

There is no pattern like simple factory. In view, this approach reminds Facade pattern.

As wiki says:

Facade is an object that serves as a front-facing interface masking more complex underlying or structural code.

So you've masked calling an API of builder pattern by using simpler methods.

Upvotes: 0

daniu
daniu

Reputation: 14999

The factory pattern's purpose is to abstract away the creation of an object. I don't see how you apply abstraction here.

I mostly agree with Pradeep's assessment that this is an adapter of some kind. To bring a factory into the mix, you'd do something like

interface MyObjectFactory { createMyObject(); }

// adapt the Transformer to a factory
MyObjectFactory fromTransformer(Transformer tr, SomeObject to Transform) {
  return () -> // transform, build
}
MyObjectFactory fromTransformer(Transformer tr, SomeObject to Transform, Other Object other) {
  return () -> // transform, build
}

The usage is where the factory comes into play.

void doWithObject(MyObjectFactory factory) {
  MyObject object = factory.create()
  // do stuff with the created object
}

Note that the last bit of code doesn't care how the object got created, which is the point of the factory pattern.

Also note that design patterns came out before Java 8; you don't need to define your own interfaces for these kind of things anymore, you can use Supplier<> for a factory and Function<,> for a transformer. Which still means you're using the pattern of course, it's just that it might be somewhat easier to miss.

Upvotes: 0

Pradeep Kumar
Pradeep Kumar

Reputation: 1491

I hope you are doing well.

Adapter Pattern suit more here. In this case, sometransformer acts as a adapter which convert aonother obects to someobject. this design pattern comes under structural pattern.

The below lines are suit for your case. two incompatible interfaces (someobject and anotherobject)

Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.

For More Details :-Adapter Design Pattern

On other hand Factory design pattern is creating object of same type without exposing the creation logic to the client and refer to newly created object using a common interface. It comes under creation pattern. In this pattern we dont transform or convert one object to another object just creating fresh object.

Upvotes: 0

Yurii Panasenko
Yurii Panasenko

Reputation: 69

IMO: Looks like the given code example actually implemented the Factory Method pattern with using the Method Object solution https://refactoring.guru/replace-method-with-method-object.

Because for the client code, it will look like one method that can build different objects based on the parameters.

The Factory pattern solve specific kind of problem (when you need to build different groups of objects based on some condition), you can see more information here https://refactoring.guru/design-patterns/abstract-factory

Upvotes: 1

Related Questions