473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Confusion in factory method pattern.

sumittyagi
202 Expert 100+
Hi all,
I was reading factory method pattern from wikipedia link.

There are some limitations mentioned three limitations mentioned there.
These are all fine. But there is a conclusion statement written, which I am not able to get.
"All three problems can be alleviated by making factories first-class class members rather than using the design pattern"

Limitations mentioned there are as follows.

1*The first limitation is that refactoring an existing class to use factories breaks existing clients. For example, if class Complex was a standard class, it might have numerous clients with code like:
Expand|Select|Wrap|Line Numbers
  1. Complex c = new Complex(-1, 0);
Once we realize that two different factories are needed, we change the class (to the code shown above). But since the constructor is now private, the existing client code no longer compiles.

2*The second limitation is that, since the pattern relies on using a private constructor, the class cannot be extended. Any subclass must invoke the inherited constructor, but this cannot be done if that constructor is private.

3*The third limitation is that, if we do extend the class (e.g., by making the constructor protected -- this is risky but possible), the subclass must provide its own re-implementation of all factory methods with exactly the same signatures. For example, if class StrangeComplex extends Complex, then unless StrangeComplex provides its own version of all factory methods, the call StrangeComplex.fromPolar(1, pi) will yield an instance of Complex (the superclass) rather than the expected instance of the subclass.


Any assistance will be greatly appreciated.

Regards,
Sumit
Aug 22 '07 #1
5 1677
r035198x
13,262 8TB
Hi all,
I was reading factory method pattern from wikipedia link.

There are some limitations mentioned three limitations mentioned there.
These are all fine. But there is a conclusion statement written, which I am not able to get.
"All three problems can be alleviated by making factories first-class class members rather than using the design pattern"

Limitations mentioned there are as follows.

1*The first limitation is that refactoring an existing class to use factories breaks existing clients. For example, if class Complex was a standard class, it might have numerous clients with code like:
Expand|Select|Wrap|Line Numbers
  1. Complex c = new Complex(-1, 0);
Once we realize that two different factories are needed, we change the class (to the code shown above). But since the constructor is now private, the existing client code no longer compiles.

2*The second limitation is that, since the pattern relies on using a private constructor, the class cannot be extended. Any subclass must invoke the inherited constructor, but this cannot be done if that constructor is private.

3*The third limitation is that, if we do extend the class (e.g., by making the constructor protected -- this is risky but possible), the subclass must provide its own re-implementation of all factory methods with exactly the same signatures. For example, if class StrangeComplex extends Complex, then unless StrangeComplex provides its own version of all factory methods, the call StrangeComplex.fromPolar(1, pi) will yield an instance of Complex (the superclass) rather than the expected instance of the subclass.


Any assistance will be greatly appreciated.

Regards,
Sumit
So which part are you getting confused with?

P.S It is best to study patterns using the patterns bible itself (Gang of four)
Aug 22 '07 #2
sumittyagi
202 Expert 100+
So which part are you getting confused with?

P.S It is best to study patterns using the patterns bible itself (Gang of four)
I am confused in, what the writer wants to say by this statement.

"All three problems can be alleviated by making factories first-class class members rather than using the design pattern"

*what he want to say by first-class class members?
Aug 22 '07 #3
r035198x
13,262 8TB
I am confused in, what the writer wants to say by this statement.

"All three problems can be alleviated by making factories first-class class members rather than using the design pattern"

*what he want to say by first-class class members?
See the wiki entry for first-class members
Aug 22 '07 #4
JosAH
11,448 Expert 8TB
*what he want to say by first-class class members?
Nothing much; he explained what could go wrong and what would be difficult to
implement. If some hypothetical language supports factories *in the language itself*,
as if they were first class citizens, the language itself would (or should) solve
these problems. Just wishful thinking.

kind regards,

Jos
Aug 22 '07 #5
sumittyagi
202 Expert 100+
Thanks a lot.
I got the point.
Aug 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
4
by: Eric | last post by:
Perhaps this question has been posed before (I'd be surprised if it hasn't) but I just gotta know... Is it possible to combine the Singleton and Factory Method design patterns in the same class?...
10
by: Chris Croughton | last post by:
What do people call their factory functions? 'new' is not an option (yes, it can be overloaded but has to return void*). The context is: class MyClass { public: // Factory functions...
4
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
1
by: =?Utf-8?B?RXJpYw==?= | last post by:
I am using the factory method to solve a problem where a factory can produce product. I have a base factory class and a base product class. The problem that I am having is that for every product...
2
by: Duy Lam | last post by:
Hi everyone, Sorry, I don't know what group to post this problem, I think may be this group is suitable. I'm styduing DAO (Data Access Object) pattern in this link...
4
by: Pallav singh | last post by:
Hi , when should i select Factory Method / Prototype Design Pattern during my design phase ?? as both look similar to me Thanks in Advance Thanks Pallav
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.