Тёмный
No video :(

Javascript Design Patterns #1 - Factory Pattern 

DevSage
Подписаться 19 тыс.
Просмотров 162 тыс.
50% 1

What is the Factory design pattern?
The factory pattern is a creational design pattern that uses factory methods to create objects - rather than by calling a constructor.
📚Materials/References:
GitHub: github.com/pke...
"Design Patterns Explained Simply" Ebook: payhip.com/b/MLtJ
🌎 Find Me Here:
Twitter: / realdevsage
Ebooks: payhip.com/dev...
Discord: / discord
Merch: cottonbureau.c...

Опубликовано:

 

27 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 178   
@DevSage
@DevSage 3 года назад
📕"Design Patterns Explained Simply" Ebook: payhip.com/b/MLtJ 🤖 GitHub: github.com/pkellz/devsage/blob/master/DesignPatterns/FactoryDesignPattern.js 💙 Twitter: twitter.com/realDevSage 📙 Ebooks: payhip.com/devsage 💥 Discord: discord.gg/BP8wPv6raA
@Tubingonline1
@Tubingonline1 3 года назад
Great video. Your channel is superb. Just one request, please make the code legible for mobile screens. I usually watch these kind of videos on mobile when I am taking a break or something. I am sure there are others who prefer mobile as well.
@DevSage
@DevSage 3 года назад
I'm glad it helped you!
@realtechbro7982
@realtechbro7982 5 лет назад
I'm just getting into software patterns as a junior developer and concise nature of these videos are amazing. thank you.
@seanweber4252
@seanweber4252 4 года назад
whoah, your implementation of say() and use of say.call() made me realize things that I guess I already knew but didn't understand the practical uses of. I'm really looking forward to watching the rest of these design pattern videos, fantastic work!
@DevSage
@DevSage 4 года назад
Appreciate you
@onlyme0349
@onlyme0349 4 года назад
@@DevSage Why aren't you passing it in as a param? I feel like that gives you more of an overview of what the function does. Well.. maybe not in this example but I think you get what I mean right
@DevSage
@DevSage 4 года назад
@@onlyme0349 I suppose either way could work
@alecjordan6100
@alecjordan6100 4 года назад
My BROTHA you don’t know how happy I am to find these videos. Keep up the great work. 👌🏾
@DevSage
@DevSage 4 года назад
💪🏾💪🏾💪🏾
@lronSausage
@lronSausage 4 года назад
His way of writing curly brackets is driving me crazy.
@RobertMcHalffey
@RobertMcHalffey 3 года назад
It's probably because of his Java background (or C, or C++).
@MarianoGianni1
@MarianoGianni1 3 года назад
Taht's how its done in C#, I find it very elegant and its easier to read
@nerdalotdulac8552
@nerdalotdulac8552 3 года назад
I came to the comment section to find this comment xD
@xyz-ey7ul
@xyz-ey7ul 2 года назад
I think I used to see or do this when I was working with java/c#. And probably the ides would format it that way
@lronSausage
@lronSausage 2 года назад
A long time ago back in school there was actually 2 competing sides and this was 1 of them, but I think eventually no one does it anymore. I dont know anyone who does this now lol
@cody_codes
@cody_codes 3 года назад
I’m just getting started with modern JS after working with Drupal & PHP for the past three years without an understanding of any design patterns, so I’ve gotta day I’m stoked on your series here. Thank you for putting this out there!
@dragonballmysteries7580
@dragonballmysteries7580 Год назад
Thanks alot dude, I was looking all over the internet for a simple demonstation of how to use Factory Patterns and your video was it. Be blessed man. 😁
@robertp3596
@robertp3596 3 года назад
Damn, underrated channel. Thanks for all this!
@lucasmendonca3841
@lucasmendonca3841 4 года назад
No words to describe this video other than simple, concise and straight to the point. The simplicity in your way to explain code makes it entertaining for those who, like me, are taking their first steps into the development world. Awesome video!
@DevSage
@DevSage 4 года назад
Glad I could help! 😁
@alejandrovillanueva4562
@alejandrovillanueva4562 3 года назад
This is the best explanation of design patterns I have ever seen. Thanks!
@MrUmmIus
@MrUmmIus 2 года назад
This is great. Short but affective. Thank you very much.
@DevSage
@DevSage 2 года назад
You're welcome!
@rue6695
@rue6695 3 года назад
Nice way about your explanations. Great tone of voice and such too. Very calming 😌
@SimPwear84
@SimPwear84 4 года назад
I am so glad I found your channel bro! Your channel full of gems. Love and blessings from South Africa
@DevSage
@DevSage 4 года назад
Appreciate it. Nice to meet you 😁
@subhamtripathi4514
@subhamtripathi4514 3 года назад
You should also add when NOT to use this pattern & common gotchas. Every pattern has its pros & cons. Nice video btw :) Thanks!
@dolicious
@dolicious 3 года назад
this was so good !! I'm starting to see how this important especially as a junior developer on the job hunt !
@kubataiupov1245
@kubataiupov1245 Год назад
The video and explanation are very comprehensive and easy to follow. Thank you!
@470tayloraveaptc
@470tayloraveaptc 4 года назад
to be honest: you by far have the best vids. keep them coming please!!
@qber83
@qber83 5 лет назад
Awesome video! Very easy to follow and I actually feel like I understand the factory pattern now. A follow up describing the best use cases for the factory design pattern would be great also! Keep up the good work!
@DevSage
@DevSage 5 лет назад
I appreciate you
@neuodev
@neuodev 2 года назад
Awesome introductory video!
@salinajohn370
@salinajohn370 4 года назад
Your delivery is outstanding. Much appreciated
@DevSage
@DevSage 4 года назад
Thanks!
@traveltechtaste41
@traveltechtaste41 Год назад
Thank you explaining this in such a simple way
@raminkhodaie4063
@raminkhodaie4063 Год назад
great video, I read this concept from a book and the video helps me to get the point very well.
@AlexTechie
@AlexTechie 4 года назад
Good video and I also enjoyed your video on building a library. One thing to note: you shouldn't need a break statement if you're returning a value. Sub'd and looking forward to more videos.
@DevSage
@DevSage 4 года назад
You're right. Never noticed that until now haha. Thanks for the sub
@projectrevolution8012
@projectrevolution8012 3 года назад
Man you just explain it so easily same with your recursion video kudos to you 👌😎😎
@DevSage
@DevSage 3 года назад
Thanks 👍🏽
@isaacbamidele9899
@isaacbamidele9899 3 года назад
Gang of four was just giving me headache...Thanks man
@perschonca
@perschonca 2 года назад
hello bud... you just made things click for me in a huge way. wow. thank you
@DevSage
@DevSage 2 года назад
Glad I could help
@alananakhaev4922
@alananakhaev4922 Год назад
Good shit even tho it was 3 year ago. Learnt something new, easy and fast. Thank you
@procydev1372
@procydev1372 3 года назад
Great tutorial! You should look into a pop filter so your P's and B's don't make such a loud wind-like sound. Just a suggestion, keep up the super helpful stuff! :)
@tomdevisser4295
@tomdevisser4295 2 года назад
How is this entire thing better than just using new Developer(“Patrick”)? Seems way more concise to me.
@kshitizgautam3200
@kshitizgautam3200 3 года назад
this video was very helpful to understand factory design pattern
@DevSage
@DevSage 3 года назад
Glad it was helpful!
@Stiefel1990
@Stiefel1990 3 года назад
Great! Very instructive material! Thanks a lot!
@danser_theplayer01
@danser_theplayer01 9 месяцев назад
A class is a factory, typeof anything "new" is "object", typeof Object is "function" because it's constructed via function, meaning it's made by a factory. Welcome to javascript ladies and gentlemen.
@AndrewRusinas
@AndrewRusinas 2 года назад
So, what's wrong with just employees.push(new Developer('Patrick')), employees.push(new Tester('Mike'))? I don't get what problem exactly this pattern solves because, in my opinion, it do exactly the opposite and create more problems because you have to keep in mind type constants. Every time you want to create new object, you have to check your factory method and find needed type.
@AndrewRusinas
@AndrewRusinas 2 года назад
Also, every time when you are reading the code, you probably don't know what that numbers is
@hunorvadasz-perhat6001
@hunorvadasz-perhat6001 4 года назад
Fantastic! A simple yet very easy to understand explanation! Thank you =D Please make more videos on more design patterns =)
@DevSage
@DevSage 4 года назад
I appreciate it
@devinlauderdale9635
@devinlauderdale9635 3 года назад
I find your lack of default case in your switch statement disturbing
@TarekFaham
@TarekFaham Год назад
This is very confusing... The create method must be static, and the type must be of type enum, a map or similar. Finally, the call method will add an insult to the injury... It must be part of a parent class. And, why you are not using the class keyword to create objects?
@1ju598
@1ju598 3 года назад
why you didn't put the semicolon at the end of code?
@DevSage
@DevSage 3 года назад
Didn't feel like it
@hariswirabrata6740
@hariswirabrata6740 3 года назад
This is very helpful. Thank you!
@DevSage
@DevSage 3 года назад
You're welcome!
@danielderese3170
@danielderese3170 4 года назад
Thanks brother. very clear presentation.
@DevSage
@DevSage 4 года назад
You're welcome
@WorstDeveloper
@WorstDeveloper 3 года назад
I don't see how this helps though. The only thing it did was to obfuscate what type of employee you created. No one is going to remember if they need type 1, 2, 3, 4, 5, 6, 7, etc.
@DevSage
@DevSage 3 года назад
Yes, using numbers would get a little confusing. This pattern is more useful when you're using a strongly typed language like TypeScript where you could make use of enums instead of raw numbers.
@Addybhoot93
@Addybhoot93 Месяц назад
So that means Abstract Factory is when we don’t want to modify the values from outside the function using object creation?
@riccarrasquilla379
@riccarrasquilla379 10 месяцев назад
thanks for the tips and explanation
@alphacentauri8035
@alphacentauri8035 7 месяцев назад
I think the 'break' statement inside the switch cases is irrelevant because you have a return statement which also exists the switch.
@DevSage
@DevSage 2 месяца назад
You are correct
@andreazappa412
@andreazappa412 4 года назад
So this remind me when teacher explained me OOP concepts , the Developer and other entities are called Model or "Pojo" in java.
@aliciapeter562
@aliciapeter562 3 года назад
thanks for posting ~ great explanation
@DevSage
@DevSage 3 года назад
Glad you enjoyed it!
@DevLearn-lv7nr
@DevLearn-lv7nr 7 месяцев назад
hey dev thank you for this - also as a side point in the above example - there's a boundless array being populated right ? with like in each Element will be [{"Patrick" ,"Developer"}],[{"John","Tester"}] is that how the outputted array will look ?
@Dragonten312
@Dragonten312 Год назад
Could you have used Class syntax for this instead?
@matheuscostapaiva
@matheuscostapaiva 4 года назад
Great video. What is the advantage of using this pattern instead of a Class? It seems to do the same with a good quality code as well.
@rutwickgangurde3247
@rutwickgangurde3247 4 года назад
I don't think he realised he had to answer this question! Lol.
@matheuscostapaiva
@matheuscostapaiva 4 года назад
@@rutwickgangurde3247 yeah, it's not a rhetorical question, haha
@nicholasleask1159
@nicholasleask1159 3 года назад
I really hope this gets answered as I'm wondering why he doesn't create classes for the developers and testers instead of functions
@hk_build
@hk_build 3 года назад
clean and neat thanks for great content
@DevSage
@DevSage 3 года назад
Thanks for watching!
@simetric6551
@simetric6551 2 года назад
Excellent
@MarshMakesComics
@MarshMakesComics Год назад
Just learning Javascript so I'm probably missing something but you are talking about objects in this but it seems to me you are using functions. Except as far as I know "this" is usually used with objects so yeah. Just a bit confused.
@camilotello3296
@camilotello3296 6 месяцев назад
I LOVED THAT.
@DevSage
@DevSage 2 месяца назад
Glad you liked it!
@omirrrr
@omirrrr 4 года назад
I'm not sure how it's better than just "employees.push(new Developer('Patrick'))
@Auzep
@Auzep 4 года назад
Maybe it's just good to practice structured code?
@omirrrr
@omirrrr 4 года назад
Hmn i don't like over complicating for the sake of it, it would be nice to see some situations where the benefits of this structure can be seen
@Auzep
@Auzep 4 года назад
@@omirrrr True, tbh I'm just starting into design patterns because I just had a job interview where they asked me about them, so I'm not sure either.
@benjaminbaer5485
@benjaminbaer5485 4 года назад
Because you have a centralized factory for a type of objects. If you for instance get a json with multiple employees you can then just call employeeFactory.create(name, type) instead of implementing the switch for everytime you import data. And if you change Developer to take (name, salary) you just have to change it in one place. As soon as multiple devs work on it, its easier to have one reference then to dig trough everyones code if they called new Developer(n), as an example.
@sashn1
@sashn1 3 года назад
@@benjaminbaer5485 honestly your examples made the penny drop for me. i feel a lot of design pattern explanations (or maybe even tech explanations in general) focus only on how to do the thing but not what the benefits are when compared to other solutions.
@lisangolachristian6676
@lisangolachristian6676 2 года назад
No need to have break in the switch because it'a inside a function and there is a "return"...But it's a very good good video👏🏾
@DevSage
@DevSage 2 года назад
Correct! And thank you!
@Tubingonline1
@Tubingonline1 3 года назад
Great video. Your channel is superb. Just one request, please make the code legible for mobile screens. I usually watch these kind of videos on mobile when I am taking a break or something. I am sure there are others who prefer mobile as well.
@ritsk4338
@ritsk4338 4 года назад
Saw... Liked... Subscribed
@LibraryOfTheOligarchs
@LibraryOfTheOligarchs 11 месяцев назад
Appreciate you! Know of any projects or step by step walkthroughs to help a person practice this?
@ajp-sd4mx
@ajp-sd4mx 3 года назад
why does an instance of the factory function need to be created? Would you not be able to create employees directly from the factory function itself?
@DevSage
@DevSage 3 года назад
I guess you don't actually need the factory function. Yeah you could return employees from the factory itself
@ajp-sd4mx
@ajp-sd4mx 3 года назад
@@DevSage ok. What's the benefit of the instance of the factory function then? Just curious as I'm a beginner with JS 😛
@user-oq6jg3gl8k
@user-oq6jg3gl8k 2 года назад
when you return in switch, you don't need to break.
@alute5532
@alute5532 Год назад
Best for building many types and many objects Factory an object that manufactues many objects
@taniadanceclasses9883
@taniadanceclasses9883 2 года назад
Thanks
@aashirkhan5172
@aashirkhan5172 3 года назад
I love your way of teaching, here I have tried factory pattern in a functional way. const baseData = (name, type) => { return { name, type }; }; const factory = (name, type) => { switch (type) { case "Developer": return baseData(name, type); case "Tester": return baseData(name, type); } }; const instnace = factory("Aashir", "Developer"); console.log("instance: ", instnace); Please let me know if you found anything wrong with it, Thanks :)
@justsomeguy8385
@justsomeguy8385 Год назад
This does not scale. For instance, if more unique properties are added to either employee type, they would need to be passed into the factory, which is just too much redundant code. Much better to have an object for each employee type that can be expanded upon.
@justsomeguy8385
@justsomeguy8385 Год назад
Also your switch does literally nothing lol. Hopefully after 2 years you figured this all out on your own though.
@esnova
@esnova 4 года назад
which javascript books and course have help you the most
@renren2145
@renren2145 Год назад
no need brake after return
@buchdev
@buchdev 4 года назад
Well explained
@user-qg1hs8pp2d
@user-qg1hs8pp2d Год назад
Understand well 💗
@janguardian
@janguardian 6 месяцев назад
You do not need a break if you return from each case in switch, right?
@DevSage
@DevSage 2 месяца назад
You are right.
@laxmikanthgurram1074
@laxmikanthgurram1074 9 месяцев назад
EmplopyeeFactory is declared as a function ,but being called just like a constructor using new EmployeeFactory().slightly confused with this.
@andre.unsal.13
@andre.unsal.13 2 года назад
A flaw here is that you case types are not communicative in your code. You should be able to pass in 'tester' or 'developer'
@eelguneezmemmedov1671
@eelguneezmemmedov1671 Год назад
Vau it is around 6 month i am working with redux state management.I didnt know that i was using Factory pattern :)
@coffeymay6775
@coffeymay6775 3 года назад
Couldnt you just write say(emp) instead of say.call(emp)?
@Selam-fr4lf
@Selam-fr4lf 3 года назад
“This” inside say would refer to the window object. Using call or apply would pass in what “this” you are implying
@PleoPleo1
@PleoPleo1 4 года назад
Great video. Thanks man :D
@DevSage
@DevSage 4 года назад
Thanks!!
@TheMdmYT
@TheMdmYT 3 года назад
Thanks this is great stuff
@DevSage
@DevSage 3 года назад
Glad you like it
@abdulkadirbala2339
@abdulkadirbala2339 7 месяцев назад
I think this breaks the OCP It would be better to create an EmployeeFactory that returns a EmployeeType object in the then let the EmployeeType Class handle how users are created, that way you only have to modify the Employee Types not the Employee Factory
@gabberfrombcn
@gabberfrombcn 2 года назад
Amazing video. Would it not be better to define EmployeFactory as an object with a method called "create"?
@DevSage
@DevSage 2 года назад
You could certainly do that, yes. That might actually be an easier way to do it. I just wanted to make sure the general idea of the pattern is understood
@preetamvarun9219
@preetamvarun9219 9 месяцев назад
Thank you
@dimitro.cardellini
@dimitro.cardellini 11 месяцев назад
Hey, @DevSage, could you please provide reference to the defintion of the "Factory Pattern" you presented in the video? The classic Gang of Four Patterns LIst doesn't contain "Factory", it contains "Abstract Factory" and "Factory Method", your solution doesn't match any of them. So, have you invented the new one? Sorry for the question, but a lot of developers could be confused and fail the interview just due to the misunderstanding in terms and definitions ...
@chintore
@chintore 7 месяцев назад
Yes, this is not clean factory pattern. In this example factory has dependency on Developer and Tester. Actually EmployeeFactory have to be abstract class and we need two subclass like DeveloperFactory and TesterFactory. So abstract factory class not depends on concrete Developer or Tester implementation. And we can easy add another class like Designer, and create DesignerFactory implementation of abstract factory class. So our code is open to extension and close to modification. In the video example factory is not close to modification and open for extension. And also Factory need some additional logic(interface) inside factory itself. Otherwise it useless.We can add some methods to Factory like, work() and each class Designer Developer and Tester will works, but in they way.
@taniakedrova
@taniakedrova 2 года назад
thanks
@DevSage
@DevSage 2 года назад
You're welcome!
@rose123998
@rose123998 2 года назад
i dont understand the need of EmployeeFactory and inner create function. can't we have createEmployee function ?
@lowercaseguy3578
@lowercaseguy3578 4 года назад
Hey, i have noticed it's similar to C programming way of creating function .....
@71GA
@71GA 4 года назад
JS factory functions are not used with a keyword "new"...
@DevSage
@DevSage 4 года назад
Explain it to me
@sharathtelu1904
@sharathtelu1904 3 года назад
Yes. The functions used here are Constructor Functions. In JavaScript Factory Functions are regular functions. JavaScript is not an Object Oriented Programming language like Java but it supports OOP. Some programmers do not like using Constructor Functions in JavaScript because of how the 'this' keyword works and how it's reference changes depending on the program. Below is an example of how we could rewrite the code using Factory Functions : function developer(name) { let newDev = { } newDev.name = name; newDev.type = "Developer"; return newDev; } function tester(name) { let newTester = { } newTester.name = name; newTester.type = "Tester"; return newTester; } function employee( ) { const create = (name, type) => { switch(type) { case 1: return developer(name); case 2: return tester(name); } } const say = ( obj ) => { console.log(" Hi I am " +obj.name+ "and I am " +obj.type); } return { create, say }; } const emp = employee( ); const employees = [ ]; employees.push( emp.create("Patrick", 1) ); employees.push( emp.create("John", 2) ); employees.forEach( ( obj ) => { emp.say(obj) }); Apart from this, I think the explanation in the video is pretty good!
@ixuz07
@ixuz07 4 года назад
There has to be a better example use case of factory patterns than this. This just made the code much less readable for no benefit.
@GauravPatelVlog
@GauravPatelVlog 3 года назад
This is call Constructor Pattern not Factory Pattern Please correct this The Constructor Pattern The Module Pattern The Revealing Module Pattern The Singleton Pattern The Observer Pattern The Mediator Pattern The Prototype Pattern The Command Pattern The Facade Pattern The Factory Pattern The Mixin Pattern The Decorator Pattern Flyweight
@Saudavelpro
@Saudavelpro 4 года назад
Do you can recommend me a book about this content ?
@DevSage
@DevSage 4 года назад
www.dofactory.com/javascript/design-patterns addyosmani.com/resources/essentialjsdesignpatterns/book/
@Saudavelpro
@Saudavelpro 4 года назад
DevSage thanks, great playlist.
@kareemmostafa7838
@kareemmostafa7838 2 года назад
It's very similar to Redux Action creators and Reducers, right?
@manibharathipattilingam3582
@manibharathipattilingam3582 3 года назад
Is really factory method often used in real time applications?
@DevSage
@DevSage 3 года назад
Yes!
@vinciwang9703
@vinciwang9703 3 года назад
return and break. there is no need to break gain
@DevSage
@DevSage 3 года назад
You're right!
@aleksfanded6550
@aleksfanded6550 Год назад
Didn't worked :(
@27sosite73
@27sosite73 6 месяцев назад
nice!
@DevSage
@DevSage 2 месяца назад
Thanks!
@SquaredbyX
@SquaredbyX 4 года назад
Cosnts... the special case mutables
@trapslime16
@trapslime16 4 года назад
Great
@mediationbreaksessions524
@mediationbreaksessions524 2 года назад
great way to describe it, i sub'd!
@DevSage
@DevSage 2 года назад
Thank you 😊
@tenebrae1191
@tenebrae1191 3 месяца назад
👏
@codedealers7501
@codedealers7501 4 года назад
thanks bro
@DevSage
@DevSage 4 года назад
You're welcome!
@user-xc8ou1fn8j
@user-xc8ou1fn8j 4 года назад
Isn't it the strategy pattern?
@DevSage
@DevSage 4 года назад
No the strategy pattern is this -> ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-SicL4fYCz8w.html
@robertgao9241
@robertgao9241 4 года назад
Isn’t it a constructor pattern?
@DevSage
@DevSage 4 года назад
It's a creational pattern
@SdotFive
@SdotFive 3 года назад
bro do you have another channel? Cause ya voice sounds like Computer Science channel lol
@DevSage
@DevSage 3 года назад
Haha well no that's not my channel. But after going to that channel and listening for myself I can see/hear EXACTLY what you mean lol
@developerAKX
@developerAKX 3 года назад
Ya bro how are you
@DevSage
@DevSage 3 года назад
I'm doing good man. How about you?
@ragtop63
@ragtop63 Год назад
Very strange design pattern. Interesting though.
@joshipiano
@joshipiano Год назад
Seems unnecessarily complicated.... no need to write so much code for a simple factory pattern example
Далее
Javascript Design Patterns #2 - Singleton Pattern
7:35
10 Design Patterns Explained in 10 Minutes
11:04
Просмотров 2,2 млн
НЕ ИГРАЙ В ЭТУ ИГРУ! 😂 #Shorts
00:28
STOP Using Classes In JavaScript | Prime Reacts
14:02
Просмотров 234 тыс.
5 JavaScript Concepts You HAVE TO KNOW
9:38
Просмотров 1,4 млн
8 Design Patterns | Prime Reacts
22:10
Просмотров 406 тыс.
I Cannot Believe TypeScript Recommends You Do This!
7:45
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
PROOF JavaScript is a Multi-Threaded language
8:21
Просмотров 275 тыс.
5 Design Patterns Every Engineer Should Know
11:51
Просмотров 937 тыс.
JavaScript Pro Tips - Code This, NOT That
12:37
Просмотров 2,5 млн