Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between factory method and utility classes?

Tags:

java

What is the difference between factory method and utility classes? When we use factory method and when we use utility classes?

like image 210
Areeba Moin Avatar asked Jan 18 '15 17:01

Areeba Moin


People also ask

What is the difference between the factory method and a simple factory?

Use the Factory method when you want to make a Framework where you want to take a control from the creation of the Object to the management of that Object. That's unlike the Simple factory, where you only care about the creation of a product, not how to create and manage it.

How do you define a utility class?

What is Utility Class? Utility Class, also known as Helper class, is a class, which contains just static methods, it is stateless and cannot be instantiated. It contains a bunch of related methods, so they can be reused across the application.

How factory method is different from factory method design pattern?

The factory method is a creational design pattern, i.e., related to object creation. In the Factory pattern, we create objects without exposing the creation logic to the client and the client uses the same common interface to create a new type of object.

What is difference between factory method and abstract factory?

The main difference between a “factory method” and an “abstract factory” is that the factory method is a single method, and an abstract factory is an object. The factory method is just a method, it can be overridden in a subclass, whereas the abstract factory is an object that has multiple factory methods on it.


2 Answers

From Wikipedia

In object-oriented programming, a factory is an object for creating other objects – formally a factory is simply an object that returns an object from some method call, which is assumed to be "new". Factory method Pattern

In computer programming, a utility class is a class that defines a set of methods that perform common, often re-used functions. Most utility classes define these common methods under static (see Static variable) scope. Utility Pattern

I think you can guess the answer :)

like image 169
pearpages Avatar answered Oct 08 '22 05:10

pearpages


Basicaly, factory is used to create object (view for example), you can create different object with one factory by different requests that u can perform

Utility class it's a class with static methods that you call to perform some operation (for example some complex operation with number or strings manipulation) but without dependence with other class.

I read that use Utility class it's bad programming but I'm not agree with this

like image 38
Ivan Avatar answered Oct 08 '22 05:10

Ivan