LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2017, 01:43 AM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,720
Blog Entries: 29

Rep: Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930
Arrow Aim of Factory pattern is to stop us from over-riding or re-writing the functions which instantiate?


http://www.cs.unc.edu/~stotts/GOF/hires/pat3cfso.htm

`CreateMaze` is the function which instantiates the objects.
IMO, according to factory pattern we are not supposed to overload
or modify or re-write the function which instantiates objects.

But in the example, the CreateMaze function returns a Maze*.
So, now if we have to write an EnchangedMaze class, will we have
to re-write CreateMaze function to return a pointer of EnchantedMaze?

From: http://www.cs.unc.edu/~stotts/GOF/hires/chap3fso.htm

>Changing the layout means changing this member function, either by
overriding it—which means reimplementing the whole thing—or by changing
parts of it—which is error-prone and doesn't promote reuse.

Isn't this what factory pattern wants to avoid?

What point am I missing?

Last edited by TheIndependentAquarius; 01-12-2017 at 01:46 AM.
 
Old 01-12-2017, 07:35 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,590

Rep: Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908
Quote:
Originally Posted by TheIndependentAquarius View Post
>Changing the layout means changing this member function, either by
overriding it—which means reimplementing the whole thing—or by changing
parts of it—which is error-prone and doesn't promote reuse.

Isn't this what factory pattern wants to avoid?

What point am I missing?
That example isn't using the factory pattern. Near the bottom of that page:
Quote:
If CreateMaze calls virtual functions instead of constructor calls to create the rooms, walls, and doors it requires, then you can change the classes that get instantiated by making a subclass of MazeGame and redefining those virtual functions. This approach is an example of the Factory Method (107) pattern.
 
1 members found this post helpful.
Old 01-12-2017, 08:31 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Obviously I don't like being asked obvious homework questions, but this is a fairly good one.

The essential notion of "a 'factory'" is that you have a case where, in order to 'manufacture' an instance of an object, you need information that a simple constructor would not have, in order to populate the instance properly. (For instance, if a "room" is part of a "maze," don't design a "room" constructor that is given a "maze" reference: design a "create-room" method of a "maze" which generates "'my' rooms." Why? Because, "the maze knows best." And, furthermore, for years to come, as the project changes and grows, it will still "know best" what to do "today.")

If a "maze" needs a factory, then an "enchanted" maze might need another factory ... which might, behind the scenes, avail itself of the same logic needed to produce "mazes in-general."

But the crucial idea here is that "you always want to be able to find the factory/construction logic," in one predictable place: "factory row." It's very tempting for computer programmers to "try to solve 'the problem of the moment,'" doing something "on the spot" that "works for them, now, to close this ticket," without fully considering the big picture. This is the sort of thing that causes their colleagues to throw darts at their picture.

Last edited by sundialsvcs; 01-12-2017 at 08:34 AM.
 
1 members found this post helpful.
Old 01-12-2017, 10:59 PM   #4
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,720

Original Poster
Blog Entries: 29

Rep: Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930
@ntubski

I know there is a separate thing called Abstract factory pattern. But,
that's not what we are talking about here.

Do you mean to say that Factory pattern and Factory method pattern are
different things?

Assuming the above example is of Factory method pattern, can you explain
what is Factory pattern then?
 
Old 01-13-2017, 10:15 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,590

Rep: Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908
Quote:
Originally Posted by TheIndependentAquarius View Post
Do you mean to say that Factory pattern and Factory method pattern are
different things?
No, I meant to say that the example you were holding up as something the factory pattern was supposed to avoid, had not yet had any factory pattern applied to it, i.e., it was purposely constructed to demonstrate those problems.

Regarding "Factory" vs "Factory method" vs "Abstract Factory" see https://en.wikipedia.org/wiki/Factor...rn#Terminology

Quote:
Terminology differs as to whether the concept of a factory is itself a design pattern – in the seminal book Design Patterns there is no "factory pattern", but instead two patterns (factory method pattern and abstract factory pattern) that use factories. Some sources refer to the concept as the factory pattern,[2][3] while others consider the concept itself a programming idiom,[4] reserving the term "factory pattern" or "factory patterns" to more complicated patterns that use factories, most often the factory method pattern; in this context, the concept of a factory itself may be referred to as a simple factory.[4] In other contexts, particularly the Python language, "factory" itself is used, as in this article.[5] More broadly, "factory" may be applied not just to an object that returns objects from some method call, but to a subroutine that returns objects, as in a factory function (even if functions are not objects) or factory method.[6] Because in many languages factories are invoked by calling a method, the general concept of a factory is often confused with the specific factory method pattern design pattern.
 
1 members found this post helpful.
Old 01-17-2017, 08:42 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
A "pattern" is necessarily a generalization, meant for illustration and description. It is not a diktat. You probably won't find any application in the real world that religiously adheres to any pattern, but patterns are a useful way to describe algorithms and software designs that have worked in real cases for real people.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to stop egrep Pattern in Bash Script from catching what it is not suppose to? BW-userx Programming 10 12-11-2015 01:37 PM
stop popa3d from writing to syslog? fmillion Linux - Server 1 02-24-2008 01:28 PM
PHP: Cannot instantiate non-existent class: ziparchive vxc69 Programming 3 04-11-2007 05:25 AM
stop writing logs on harddisk lajner Linux - General 4 08-24-2006 02:57 PM
Cannot instantiate class from JSP Jose Muñiz Programming 2 07-19-2004 12:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration