Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Question about Factory Method pattern

Nikita.MospanMay 20 2016 — edited Jun 23 2016

Hello all.

I started learning Java concepts by a book "Thinking in Java, 4th Edition". In the chapter about Interfaces I did not grasp the idea of the "Factory Method pattern". The pattern is described below :

"An interface is intended to be a gateway to multiple implementations, and a typical way to produce objects that fit the interface is the Factory Method design pattern. Instead of calling a constructor directly, you call a creation method on a factory object which produces an implementation of the interface."

//: interfaces/Factories.java

interface Service {
void method1();
void method2();
}

//define interface that works with Classes that implement Service interface
interface ServiceFactory {
Service getService();
}

class Implementation1 implements Service {
Implementation1() {}
@Override public void method1() {System.out.println("Implementation1 method1"); }
@Override public void method2() {System.out.println("Implementation1 method2"); }
}

class Implementation1Factory implements ServiceFactory {
@Override public Service getService() {
  return new Implementation1();
}
}

class Implementation2 implements Service {
Implementation2() {}
@Override public void method1() {System.out.println("Implementation2 method1"); }
@Override public void method2() {System.out.println("Implementation2 method2"); }
}

class Implementation2Factory implements ServiceFactory {
@Override public Service getService() {
  return new Implementation2();
}
}

public class Factories {
public static void serviceConsumer(ServiceFactory fact) {
  Service s = fact.getService();
  s.method1();
  s.method2();
}

public static void main(String[] args) {
  serviceConsumer(new Implementation1Factory());
  serviceConsumer(new Implementation2Factory());
}
}

The necessity of the Factory classes is explained as

"Without the Factory Method, your code would somewhere have to specify the exact type of Service being created, so that it could call the appropriate constructor."

But it sounds strange for me, because we still specify the exact type of the Factory class (Implementation1Factory, Implementation2Factory). So from the logical perspective I currently see no difference using the following code which looks more concise:

//: interfaces/FactoriesTest.java

interface Service {
void method1();
void method2();
}

class Implementation1 implements Service {
Implementation1() {}
@Override public void method1() {System.out.println("Implementation1 method1"); }
@Override public void method2() {System.out.println("Implementation1 method2"); }
}

class Implementation2 implements Service {
Implementation2() {}
@Override public void method1() {System.out.println("Implementation2 method1"); }
@Override public void method2() {System.out.println("Implementation2 method2"); }
}

public class FactoriesTest {
public static void serviceConsumer(Service s) {
  s.method1();
  s.method2();
}

public static void main(String[] args) {
  serviceConsumer(new Implementation1());
  serviceConsumer(new Implementation2());
}
}/* Output:
Implementation1 method1
Implementation1 method2
Implementation2 method1
Implementation2 method2
*///:~

Of course, I understand that I miss something, but I can't find out what exactly. I would really appreciate if someone presents an example, where the Factory Method is indeed justified.

Thank you very much in advance.

This post has been answered by unknown-7404 on May 20 2016
Jump to Answer

Comments

top.gun

So there is nothing to vote for?

William Robertson

This seems to be a tip rather than a suggestion for Oracle to implement something new.

Chris Hunt

And your suggestion is?

I wouldn't bank on any of these options "improving the performance." The Oracle optimiser is pretty clever, and may well use the same query plan in all three cases.

Akshs

And your suggestion is?

I wouldn't bank on any of these options "improving the performance." The Oracle optimiser is pretty clever, and may well use the same query plan in all three cases.

Could you please go through the below link where I attached the explain plan.

Martin Preiss

as others I fail to see here a proposal of a new feature - since the optimizer is already able to do corresponding transforms internally.

Furthermore I think it's common knowledge that NOT IN will not return a result set when the subquery contains NULL values.

unknown-1040115

This may not be an actionable idea for the Database Ideas list...

May I suggest we adjourn to the space to continue the discussion.

LKR

Akshs

This may not be an actionable idea for the Database Ideas list...

May I suggest we adjourn to the space to continue the discussion.

LKR

Thanks for guiding me.

But I tried to publish in SQL PLSQL Space..But because of permission issue I'.m not able to publish...

unknown-1040115

Thanks for guiding me.

But I tried to publish in SQL PLSQL Space..But because of permission issue I'.m not able to publish...

I can move this on your behalf.

Regards,

LKR

Akshs

I can move this on your behalf.

Regards,

LKR

Thank you....

Akshs

I can move this on your behalf.

Regards,

LKR

Even I'm not able to send any message. Could you please publish the below document in SQL PL\SQL space.

Could  please accept the request, So I'm able to message you.

Akshs

I can move this on your behalf.

Regards,

LKR

Hi Laura,

I have just published a document. Kindly move it to SQL and PLSQL space.

Document path : -

Many Many Thanks

Akshs

1 - 11
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 21 2016
Added on May 20 2016
11 comments
2,365 views