[SOLVED] Aim of Factory pattern is to stop us from over-riding or re-writing the functions which instantiate?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
`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?
>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 Aquarius_Girl; 01-12-2017 at 01:46 AM.
>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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.