Factory Pattern - Object Oriented PHP Tutorial

preview_player
Показать описание
Here we'll go over one of the most popular design patterns called the Factory Pattern.

Download this video's files:

Upgrade your Clever Techie learning experience:

``````````````````````````````````````````````````````````````````````````````````````````````

``````````````````````````````````````````````````````````````````````````````````````````````
Рекомендации по теме
Комментарии
Автор

This is silly how simply and nicely he explained. This is exactly around the difficulty level I'm on right now. Cheers

burekmali
Автор

after spending 2 days to get the notion of factory pattern you are the only one who give it to me as simple as possible and easy to understand. thank you so much bro i will follow your videos to get all the design pattern thanks again :)

abdqz
Автор

i dont know technical English but your video was undestand by me very easy, i am beginner in English,
thank you very much for great work!

мистерикс-рл
Автор

Thanks for sharing, a simple and concrete explanation

LuisPerez-mwkl
Автор

I just saw your BR constant right away and thought, GENIUS!! I've been appending them to my troubleshooting/debugging output and that constant would make that SO MUCH simpler. (Now, on to watching the actual video lol)

TroyNiemeier
Автор

super clear and concise. impressive . thank you sir

kenjohnsiosan
Автор

I don't get the point using Factory in your example. Why using BookFactory::create($name, $author) instead new Book($name, $author)? It's the same purpose!!!

ziat
Автор

I was wondering why use the factory when you can create the object directly? Whats the idea behind using the factory, a particular use case would be great.

souvikghosh
Автор

Thank you... I think the double colon :: is scope resolution operator, which is used to call static methods and access static properties of a class

ashikmehermobin
Автор

That heavy typewriter sound: mesmerizing. Can you mention the product maker and model?

sanjv
Автор

A very clean and precise one. Thank you for the great upload.

sudipto.m
Автор

i'm still wondering why do you need the BookFactory class ? Why not write the create function in the Book class ? the you do $book1 = new Book($name, $author). What am i missing ? besides the fact that you are writing extra code.

TheLordOfWolves
Автор

Great job dude, thank you
keep up for more design patterns

mansoorkochy
Автор

May i know what is advantage of using factory pattern?

adflyadfly
Автор

Very nice Video. This one deserves more views!! Thanks

fragxz
Автор

I can not understand example, why do not create static method create in Book class. Why need factory ?

dfadfsdfsdsa
Автор

Thank you Clever Techie for your great explanation. but why we should use a factory design pattern? why is it useful?

bevedel
Автор

Why don't just create the book object directly instead of a middleman?

JacksonMarshal
Автор

I'm sorry but while this shows the basics of how to make a very simple factory, the design and other things are teaching newcomers very bad practices. Your intention may have been to just show the very basics, but it's not much more work to show 2 classes and not HTML in them, and show the data being returned and how to use the getters somewhere. Sometimes people take things literally, or if they learn badly from they start they adopt the bad habits and keep them.

1.
Never have View (BR and hyphen) in the backend. If I want to do something else with that data you stop me by forcing a BR and hyphen. What if I wanted to separate them with pipe "|" instead or something else, or display bookName in one part of the page and bookAuther further down? Object and factory in the backend should always simply return the data the object defines and let places that are concerned about display worry about hyphen and BR.
Also, the BR is applied to every book, which means I have to fight a new line in my output after the last book is displayed.

2.
A file should never have 2 classes in. Argue all you want, there's no need for it. The factory and the object it creates are two separate concerns and should be in separate files - probably even separate DIRs and areas of the app entirely.


3.
Static methods have their place but should be very rare. There's no reason to use them here. Instead, dependency inject your factory into your controller constructor (or other class)
and simply use it like: $book = $this->bookFactory->create('name', 'author')


4.
Why does your object getter decide to merge the two object properties into one? What if I need to show the two sets of data separately on the page in a list, or I only need one of them?
You should honour the object's blueprint and have getters for both properties and let the factory return an instance of the object with public getter access to data on each property.
I mean, you create it all this way through factory with separate property setting (via constructor) but then hinder usability by only having one getter for both sets of data.

5.
While arguably not part of this tutorial, your factory does no sanity checking. What if name or author are empty or numbers or an array? I mean type declaration (hint or docblock) would fix this. I know it's a basic tutorial, but to be honest, code as basic as this is not really showing anything other than a class that makes another. Factory (and object classes) deserve much more than this.

6.
Subjective but arguably makes easier to read code - there's no need for the word "book" on your object properties. The class is called book, this is generally cleaner:
class Book {
private $name;
private $author;
}
And before a debate ensues about "name is ambiguous" it can't be as it's on "Book" class, so it's the book's name. "Author" by definition is a "writer of a book".

jamesfoo
Автор

CleverTechie, you sure can simplify stuff, man. You made this strange concept look very simple. Thanks, Bro.
But, why do we in PHP Community complicate stuff for ourselves?
Does it not take less code to create my own Object from a class, than 'creating' a 'Factory' to do it?

NedumEze