1
00:00:00,810 --> 00:00:05,790
That guys in this video we're going to look at the factory pattern also called the factory method in

2
00:00:05,790 --> 00:00:13,020
javascript which is a way of creating an interface for creating objects but we can let subclasses define

3
00:00:13,020 --> 00:00:21,600
which classes to instantiate and factory methods are often used in applications to manage and maintain

4
00:00:21,900 --> 00:00:24,840
and manipulate collections of objects that are different.

5
00:00:24,840 --> 00:00:30,740
But at the same time have many common characteristics and a good example of that would be a member.

6
00:00:30,840 --> 00:00:36,480
Let's say some kind of paid membership application or web site where your members may have different

7
00:00:36,480 --> 00:00:40,460
types but still have the same properties and methods.

8
00:00:40,470 --> 00:00:42,540
So that's what we're going to get into in this video.

9
00:00:42,540 --> 00:00:48,930
We're going to create a member factory that takes a member type in creates a new object based on that

10
00:00:48,930 --> 00:00:49,630
type.

11
00:00:49,830 --> 00:00:53,760
OK so let's go ahead and create a function here.

12
00:00:53,760 --> 00:01:01,640
This is just a blank blank app J S file and we're going to call this member factory and we're going

13
00:01:01,640 --> 00:01:02,550
to set this.

14
00:01:02,550 --> 00:01:07,500
Actually we're just going to say function member factory it's just a declaration and then we're going

15
00:01:07,500 --> 00:01:08,730
to do this stot

16
00:01:11,220 --> 00:01:12,340
create member.

17
00:01:12,360 --> 00:01:15,280
And that will be a function expression.

18
00:01:19,190 --> 00:01:24,110
So this function is going to take in two things it's going to take in a name and it's going to take

19
00:01:24,110 --> 00:01:26,040
in a membership type.

20
00:01:26,390 --> 00:01:26,860
OK.

21
00:01:26,870 --> 00:01:31,850
Now here we're going to define a member variable.

22
00:01:32,360 --> 00:01:38,870
And we want to check the types here that are passed in or the type that's passed in and basically going

23
00:01:38,870 --> 00:01:46,610
by that type we want to create a new object or a certain type of object so we'll have a simple membership

24
00:01:46,940 --> 00:01:49,140
a standard and a super membership.

25
00:01:49,160 --> 00:01:54,020
Maybe it's a web hosting application web site or something like that.

26
00:01:54,050 --> 00:02:05,600
So let's test the type and we'll see if it's equal to simple then we're going to set member equal to

27
00:02:05,630 --> 00:02:09,230
new and then we're going to instantiate simple

28
00:02:11,930 --> 00:02:16,320
membership and we'll pass in the name.

29
00:02:16,320 --> 00:02:23,770
All right and then we'll do an LCF.

30
00:02:24,620 --> 00:02:30,070
So LCF that I'm just going to copy this up.

31
00:02:30,070 --> 00:02:31,210
This should be else.

32
00:02:31,340 --> 00:02:34,460
Type is equal to standard

33
00:02:37,160 --> 00:02:45,070
then we want to instantiate new standard membership.

34
00:02:45,230 --> 00:02:46,190
Else if

35
00:02:49,990 --> 00:03:06,970
the type is equal to Super then we want the member equal to new super membership.

36
00:03:06,970 --> 00:03:14,270
So we're testing out whatever that type is now now down here we want to set after the if statement.

37
00:03:14,310 --> 00:03:27,390
We want to set the member Daut type equal to type and then I'm just going to have one function or one

38
00:03:27,390 --> 00:03:33,450
method associated with this object called define that that's going to spit out the name the type the

39
00:03:33,450 --> 00:03:36,210
membership type and the cost of the membership.

40
00:03:36,210 --> 00:03:39,190
So let's call that define.

41
00:03:39,270 --> 00:03:42,900
So say member define equals function

42
00:03:46,540 --> 00:03:53,760
an inside here we're just going to console log on to Prince and back text we use template literals and

43
00:03:53,760 --> 00:03:54,990
all say the name.

44
00:03:55,110 --> 00:04:04,130
So they start name in and right after that let's put in parentheses the membership type so will say

45
00:04:04,170 --> 00:04:10,940
this type and then let's put a colon right here and then we'll put the cost of the number of ships of

46
00:04:10,950 --> 00:04:17,770
this dot cost and then we just want to return the member.

47
00:04:18,450 --> 00:04:24,960
So now we need to do is create our constructors for these subclasses.

48
00:04:24,960 --> 00:04:27,740
The simple standard and super membership's.

49
00:04:27,750 --> 00:04:35,970
So we're going to go down here create a variable called simple membership and we're going to set that

50
00:04:35,970 --> 00:04:41,950
to a function that's going to take in a name.

51
00:04:42,720 --> 00:04:45,990
And we're going to set this dog name

52
00:04:48,990 --> 00:04:51,350
equal to the name that's passed in.

53
00:04:51,360 --> 00:05:02,250
And then we're also going to set the cost of this to let's say $15 actual to $5 and then I'm just going

54
00:05:02,250 --> 00:05:07,740
to copy this and this one will be the standard membership

55
00:05:10,880 --> 00:05:19,010
and this one will be $15 this one will be the super and this one will be $25.

56
00:05:19,070 --> 00:05:26,080
So now let's go down here and let's create a variable called members and just set it to an empty array

57
00:05:26,960 --> 00:05:28,860
and then we want to define our factory.

58
00:05:28,940 --> 00:05:37,460
So I will say Konst factory equals new member factory.

59
00:05:38,330 --> 00:05:45,610
And then what we can do is we can add members by simply saying members Daut push.

60
00:05:46,490 --> 00:05:54,620
And then we can pass in that factory variable and we want to call create member.

61
00:05:55,560 --> 00:06:03,080
And what does create member take is right here takes in the name and it takes in the type ok and the

62
00:06:03,080 --> 00:06:06,530
type should be the simple standard or super.

63
00:06:07,190 --> 00:06:21,520
So let's say John Doe signs up for a simple membership and then down here we'll go ahead and cons. dot

64
00:06:21,580 --> 00:06:27,070
blog the members so save.

65
00:06:28,200 --> 00:06:36,840
All right so if we if we look at it this way it's just going to show us membership in the index it'll

66
00:06:36,840 --> 00:06:43,980
show us the cost the name simple what I'm going to do though is loop through the members so we'll say

67
00:06:43,980 --> 00:06:54,480
members Daut for each and put a function in here and then and here we'll see a member.

68
00:06:55,350 --> 00:06:59,460
And then let's call our member define K.M..

69
00:06:59,520 --> 00:07:05,190
Remember we have that defined method right here and that's going to log the name the type the cost and

70
00:07:05,190 --> 00:07:05,760
so on.

71
00:07:05,760 --> 00:07:13,590
So let me just comment this log out and save and there we go John Doe has a simple membership which

72
00:07:13,590 --> 00:07:17,670
is five dollars and we can create as many of these as we want.

73
00:07:17,670 --> 00:07:32,130
So if we want to create some more of these let's say Chris Jackson and let's say he purchased a super

74
00:07:32,490 --> 00:07:46,140
membership we have Janice Williams who will say she got a simple membership and Tom Smith got a standard

75
00:07:47,520 --> 00:07:49,200
membership so we'll save that.

76
00:07:49,200 --> 00:07:51,120
Now you can see that they're all getting added.

77
00:07:51,120 --> 00:07:54,380
We have the name the membership type and the price.

78
00:07:54,540 --> 00:08:00,210
So this is a pretty good example of a fact of how the Factory pattern works.

79
00:08:00,390 --> 00:08:06,570
It's really good for when you have you know different objects they're different but they have the they

80
00:08:06,570 --> 00:08:10,580
share a lot of the same properties and methods and all of that.

81
00:08:10,620 --> 00:08:14,400
So we have just different memberships.

82
00:08:14,400 --> 00:08:18,530
All right guys that's it for the factory pattern and I will see you in the next video.

