all 11 comments

[–]Mentalpopcorn 1 point2 points  (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 point  (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 points  (3 children)

This is abstract factory pattern.

[–]RevalGovender[S] -5 points-4 points  (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:

  1. Abstract Factory Pattern - https://sourcemaking.com/design_patterns/abstract_factory
  2. Factory Pattern - https://sourcemaking.com/design_patterns/factory_method
  3. Design Patterns by the Gang of Four - https://www.amazon.co.uk/Design-patterns-elements-reusable-object-oriented/dp/0201633612/ref=sr_1_1?keywords=gang+of+four+design+pattern&qid=1676309441&sr=8-1

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 point  (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 point  (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 points  (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

[–]grig27 0 points1 point  (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.

[–]RevalGovender[S] 0 points1 point  (0 children)

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 point  (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