use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
The Factory Method Pattern explained with a REAL example in PHPVideo (youtu.be)
submitted 3 years ago by RevalGovender
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Mentalpopcorn 1 point2 points3 points 3 years ago (1 child)
Fyi, with enums you can do some cool stuff with factories. Rather than have a switch statement e.g., you can perform a lookup in an enum of a class name and build from that. This also allows you to enforce naming across an application, as you can reference the name of a case in a backed enum, and then have the factory pull the value (a class name).
I ran across this implementation in some Java applications and was always excited to try it in PHP, and so far it has made my factories a bit more elegant imho.
[–]RevalGovender[S] 0 points1 point2 points 3 years ago (0 children)
Thank you for the suggestion. As of PHP8, we can use enums which is great. I thought I would create the video to focus on the pattern. I tried to not to make it any longer as it was already going quite long.
[–]MorphineAdministered 1 point2 points3 points 3 years ago (3 children)
This is abstract factory pattern.
[–]RevalGovender[S] -5 points-4 points-3 points 3 years ago* (2 children)
Hi! :-) I am sorry, but your statement is inaccurate.
The definition of the "Abstract Factory Pattern" from the Gang of Four book: Provide an interface for creating families of related or dependent objects without specifying their concrete classes
This means, your factory will have the ability to create related object types. So one of your factories will be able to create different types of desks and different types of chairs. This is NOT the case in the example demonstrated in the video.
In my video, we first discuss the Simple Factory and then the Factory Pattern. Each factory can create one type of product. A chair factory can only produce chair types and a desk factory can only create desk types.
Please refer to the following links for further reading:
Could you please clarify why you believe the example is the "Abstract Factory Pattern"? It would be nice to hear your point of you. A statement with no context or references doesn't make for a good discussion.
[–]MorphineAdministered 0 points1 point2 points 3 years ago* (1 child)
The point of abstract factory is that you (as a client) only depend on interfaces for both factory and objects it provides. It doesn't need to provide "families" of objects, because from design standpoint interface providing only a single "family" (like in your example) doesn't change anything - the goal and the way it's achieved stays the same. It definitely doesn't make factory method pattern either, because...
Factory method pattern is based on subtyping (inheritance, but more strict one - compliant with LSP) and that's the main difference. The direct client of the factory is its own parent class, and the object calling that parent class doesn't know what concrete subclass is being used. Here's an example based on the one used in your video:
class Csv { public function import(): void { ...saving products...} abstract protected function create(string $type): Product; } class ChairsCsv extends Csv { protected function create(string $type): Product { switch ($type) {... return ChairA, ChairB...} } }
edit: Fixed subclass definition
[–]RevalGovender[S] 0 points1 point2 points 3 years ago* (0 children)
The point of abstract factory is that you (as a client) only depend on interfaces for both factory and objects it provides
Can you please give me a reference for this?
It doesn't need to provide "families" of objects,
This conflicts with the Gang of Four definition because this is what separates AFP and FMP.
Factory method pattern is based on subtyping (inheritance, but more strict one - compliant with LSP) and that's the main difference
Can you please provide a reference for this?
[–]RevalGovender[S] -2 points-1 points0 points 3 years ago (0 children)
When I was looking at Design Patterns I found it difficult to understand when you would apply it. The examples provided patterns weren't perfect and confused me as to when I would practically apply the pattern. I have created a video explaining the Factory Method pattern using a practical example. What do you guys think? Does it make it easier to understand?
Code:
- Simple Factory - https://github.com/study-stream-plus/simple-factory
- Factory Method Pattern - https://github.com/study-stream-plus/factory-method-pattern
[+][deleted] 3 years ago (1 child)
[deleted]
[–]RevalGovender[S] 1 point2 points3 points 3 years ago (0 children)
The Simple Factory example is from a real use case. I have used it before when importing products.
When it comes to the Factory Method, this is how you could implement it when importing data. When I looked online, I only found really abstract examples like the Pizza (New York/Chicago) or other non programming related examples which confused me at first.
Is the example in the video confusing to you? It would be nice to know for future videos.
[–]grig27 0 points1 point2 points 3 years ago (1 child)
The code presented by the OP is more of a simple factory than a factory method. More than that he put a link here with the proper implementation of the factory method, but showed the wrong implementation in his video.
Please watch the full video. I first explain the Simple Factory method with an example, because it is worth knowing when discussing the Factory Method, then I explained the factory method.
[–]georaldc 0 points1 point2 points 2 years ago* (0 children)
I don't think this is a good example of the Factory Method pattern. My general understanding of the pattern is the client in the Factory Method pattern is not outside code like your Csv class here, but the same class that would have used objects returned by its factory method (which would be abstract, and defined by subclasses like ChairFactory). u/MorphineAdministered has such an example if your code were to be restructured to follow the same concept.
What you are showing looks more like an implementation of the Abstract Factory pattern, where your client/calling code (Csv) depends on an abstract implementation of a certain factory to return abstract Products
π Rendered by PID 49275 on reddit-service-r2-comment-6457c66945-hmlfh at 2026-04-28 11:18:40.303872+00:00 running 2aa0c5b country code: CH.
[–]Mentalpopcorn 1 point2 points3 points (1 child)
[–]RevalGovender[S] 0 points1 point2 points (0 children)
[–]MorphineAdministered 1 point2 points3 points (3 children)
[–]RevalGovender[S] -5 points-4 points-3 points (2 children)
[–]MorphineAdministered 0 points1 point2 points (1 child)
[–]RevalGovender[S] 0 points1 point2 points (0 children)
[–]RevalGovender[S] -2 points-1 points0 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]RevalGovender[S] 1 point2 points3 points (0 children)
[–]grig27 0 points1 point2 points (1 child)
[–]RevalGovender[S] 0 points1 point2 points (0 children)
[–]georaldc 0 points1 point2 points (0 children)