From b2d80610db6beda38573890ed169815e495bc663 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 24 May 2020 16:34:31 +0700 Subject: [usth/ICT2.7] Engineer software --- .../1 - Lesson Overview - lang_en_vs4.srt | 31 ++ .../10 - Choosing a Pattern - lang_en_vs5.srt | 95 ++++ .../11 - Choosing a Pattern Quiz - lang_en_vs4.srt | 31 ++ ...osing a Pattern Quiz Solution - lang_en_vs4.srt | 79 ++++ ...13 - Negative Design Patterns - lang_en_vs4.srt | 51 ++ ... - History of Design Patterns - lang_en_vs6.srt | 179 ++++++++ .../3 - Patterns Catalogue - lang_en_vs4.srt | 79 ++++ .../4 - Pattern Format - lang_en_vs5.srt | 71 +++ .../5 - Factory Method Pattern - lang_en_vs5.srt | 179 ++++++++ ...actory Method Pattern Example - lang_en_vs5.srt | 127 +++++ .../7 - Strategy Pattern - lang_en_vs5.srt | 143 ++++++ ...rategy Pattern Example & Demo - lang_en_vs5.srt | 511 +++++++++++++++++++++ .../9 - Other Common Patterns - lang_en_vs5.srt | 295 ++++++++++++ 13 files changed, 1871 insertions(+) create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/1 - Lesson Overview - lang_en_vs4.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/10 - Choosing a Pattern - lang_en_vs5.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/11 - Choosing a Pattern Quiz - lang_en_vs4.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/12 - Choosing a Pattern Quiz Solution - lang_en_vs4.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/13 - Negative Design Patterns - lang_en_vs4.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/2 - History of Design Patterns - lang_en_vs6.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/3 - Patterns Catalogue - lang_en_vs4.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/4 - Pattern Format - lang_en_vs5.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/5 - Factory Method Pattern - lang_en_vs5.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/6 - Factory Method Pattern Example - lang_en_vs5.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/7 - Strategy Pattern - lang_en_vs5.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/8 - Strategy Pattern Example & Demo - lang_en_vs5.srt create mode 100644 usth/ICT2.7/P3L3 Design Patterns Subtitles/9 - Other Common Patterns - lang_en_vs5.srt (limited to 'usth/ICT2.7/P3L3 Design Patterns Subtitles') diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/1 - Lesson Overview - lang_en_vs4.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/1 - Lesson Overview - lang_en_vs4.srt new file mode 100644 index 0000000..152b961 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/1 - Lesson Overview - lang_en_vs4.srt @@ -0,0 +1,31 @@ +1 +00:00:00,230 --> 00:00:04,560 +In the last lesson, we talked about design, and we saw how difficult it can + +2 +00:00:04,560 --> 00:00:10,010 +be to come up with a good and effective design for a given software system. To + +3 +00:00:10,010 --> 00:00:12,620 +help address these difficulties, we will discuss + +4 +00:00:12,620 --> 00:00:16,560 +design patterns, which can support design activities by + +5 +00:00:16,560 --> 00:00:20,710 +providing general, reusable solutions to commonly occurring design + +6 +00:00:20,710 --> 00:00:25,410 +problems. Similar to architectural styles, design patterns can + +7 +00:00:25,410 --> 00:00:30,400 +help developers build better designed systems by reusing design solutions that + +8 +00:00:30,400 --> 00:00:33,730 +worked well in the past and by building on those solutions. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/10 - Choosing a Pattern - lang_en_vs5.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/10 - Choosing a Pattern - lang_en_vs5.srt new file mode 100644 index 0000000..f273441 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/10 - Choosing a Pattern - lang_en_vs5.srt @@ -0,0 +1,95 @@ +1 +00:00:00,150 --> 00:00:03,200 +But with so many patterns, how do we choose a pattern? So + +2 +00:00:03,200 --> 00:00:05,970 +this is a possible approach that you can follow. First of all, you + +3 +00:00:05,970 --> 00:00:09,140 +want to make sure that you understand your design context. You understand + +4 +00:00:09,140 --> 00:00:12,800 +what you're designing and what are the issues involved with this design. What + +5 +00:00:12,800 --> 00:00:15,200 +are the problems that you need to solve. At this point, you + +6 +00:00:15,200 --> 00:00:17,160 +can examine the patterns catalog, or,if + +7 +00:00:17,160 --> 00:00:18,750 +you're already familiar with the catalog, just + +8 +00:00:18,750 --> 00:00:22,430 +think about the possible patterns that you could use. Once you identify + +9 +00:00:22,430 --> 00:00:25,440 +the patterns that you can use, you also want to study them and + +10 +00:00:25,440 --> 00:00:29,090 +study the related patterns. So normally if you look at any pattern catalog, + +11 +00:00:29,090 --> 00:00:32,229 +for each pattern there will also be a list of related patterns. So + +12 +00:00:32,229 --> 00:00:35,010 +you can also look at those to see whether maybe some of those + +13 +00:00:35,010 --> 00:00:38,370 +might be more applicable. And finally, once you identify the pattern that you + +14 +00:00:38,370 --> 00:00:41,360 +think is appropriate, you will apply that pattern. When you do that, just + +15 +00:00:41,360 --> 00:00:44,850 +be mindful that there are pitfalls in the use of patterns. One obvious + +16 +00:00:44,850 --> 00:00:47,490 +one is the fact that you might select the wrong pattern and make + +17 +00:00:47,490 --> 00:00:50,460 +your design worse instead of better. The second one is that if you + +18 +00:00:50,460 --> 00:00:52,560 +get too excited about patterns, then you + +19 +00:00:52,560 --> 00:00:54,850 +might be abusing patterns, so just using too + +20 +00:00:54,850 --> 00:00:56,370 +many patterns, and end up with a design + +21 +00:00:56,370 --> 00:00:58,980 +that is more complicated rather than less complicated. + +22 +00:00:58,980 --> 00:01:01,890 +So always be careful, spend the time to figure out which one is the right + +23 +00:01:01,890 --> 00:01:03,577 +pattern to apply, and make sure that you + +24 +00:01:03,577 --> 00:01:05,190 +don't use patterns that you don't actually need. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/11 - Choosing a Pattern Quiz - lang_en_vs4.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/11 - Choosing a Pattern Quiz - lang_en_vs4.srt new file mode 100644 index 0000000..76a78e0 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/11 - Choosing a Pattern Quiz - lang_en_vs4.srt @@ -0,0 +1,31 @@ +1 +00:00:00,160 --> 00:00:02,780 +Now that we've discussed how to choose a pattern. Imagine that + +2 +00:00:02,780 --> 00:00:05,210 +you have to write a class that can have only one + +3 +00:00:05,210 --> 00:00:08,090 +instance. So to satisfy this requirement, I would like for you + +4 +00:00:08,090 --> 00:00:10,720 +to pick one of the design patterns that we discussed in + +5 +00:00:10,720 --> 00:00:14,580 +this lesson, and write the code here that satisfies that requirement. + +6 +00:00:14,580 --> 00:00:16,270 +And when you write the code, please make sure that your + +7 +00:00:16,270 --> 00:00:21,140 +class has only one method, without counting possible constructors, and that + +8 +00:00:21,140 --> 00:00:25,120 +the class is called Singleton. And write your class right here. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/12 - Choosing a Pattern Quiz Solution - lang_en_vs4.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/12 - Choosing a Pattern Quiz Solution - lang_en_vs4.srt new file mode 100644 index 0000000..c348a2c --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/12 - Choosing a Pattern Quiz Solution - lang_en_vs4.srt @@ -0,0 +1,79 @@ +1 +00:00:00,220 --> 00:00:01,750 +As we discussed in the class the right thing to + +2 +00:00:01,750 --> 00:00:05,050 +do here was to use the factory pattern. So here is + +3 +00:00:05,050 --> 00:00:07,910 +a possible code to solve the problem. Of course that there + +4 +00:00:07,910 --> 00:00:11,220 +are different possible solutions. So what we did for this code + +5 +00:00:11,220 --> 00:00:16,210 +was to first create a private, static, Singleton object called + +6 +00:00:16,210 --> 00:00:19,340 +instance, which is the one that will keep track of the + +7 +00:00:19,340 --> 00:00:22,250 +only instance that can be created on the class. Then we + +8 +00:00:22,250 --> 00:00:25,750 +define the default constructor, the constructor that doesn't take any parameter + +9 +00:00:25,750 --> 00:00:29,900 +as private. In this way other classes cannot create instances + +10 +00:00:29,900 --> 00:00:33,310 +of Singleton without calling our factory method, and finally we + +11 +00:00:33,310 --> 00:00:35,650 +create the factory method. And the factory method is very + +12 +00:00:35,650 --> 00:00:38,600 +simple. The method will first check whether an instance of + +13 +00:00:38,600 --> 00:00:40,970 +the class was already created. If it was created, it + +14 +00:00:40,970 --> 00:00:44,010 +would just return that instance. Otherwise, it will create a + +15 +00:00:44,010 --> 00:00:47,550 +new instance and assign it to that instance member variable + +16 +00:00:47,550 --> 00:00:50,960 +and then return the newly created instance. So with this code + +17 +00:00:50,960 --> 00:00:53,540 +you're guaranteed that other classes cannot bypass the factory + +18 +00:00:53,540 --> 00:00:56,530 +method, because the default constructor is private. And the that + +19 +00:00:56,530 --> 00:00:59,600 +the factory method will create one and only one instance + +20 +00:00:59,600 --> 00:01:02,020 +of the class, which is exactly what our requirements were. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/13 - Negative Design Patterns - lang_en_vs4.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/13 - Negative Design Patterns - lang_en_vs4.srt new file mode 100644 index 0000000..6433019 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/13 - Negative Design Patterns - lang_en_vs4.srt @@ -0,0 +1,51 @@ +1 +00:00:00,100 --> 00:00:02,210 +To conclude this lesson, I want to discuss the + +2 +00:00:02,210 --> 00:00:06,170 +concept of negative design patterns, that is, patterns that should + +3 +00:00:06,170 --> 00:00:09,600 +be avoided. Interestingly, negative patterns were also mentioned in + +4 +00:00:09,600 --> 00:00:12,950 +Christopher Alexander's book, so in the first formulation of patterns. + +5 +00:00:12,950 --> 00:00:16,219 +So negative design pattern are basically guidelines on how + +6 +00:00:16,219 --> 00:00:19,689 +not to do things. In consoles with patterns, the guidelines + +7 +00:00:19,689 --> 00:00:21,920 +on how to do things. So basically, what the negative + +8 +00:00:21,920 --> 00:00:25,170 +design patterns do is, they enable recurring design defects to + +9 +00:00:25,170 --> 00:00:28,070 +be avoided. And as we will see in this class extensively, + +10 +00:00:28,070 --> 00:00:30,080 +in mini-course four, negative patterns are + +11 +00:00:30,080 --> 00:00:33,380 +also called anti-patterns or bad smells, + +12 +00:00:33,380 --> 00:00:36,540 +or bad code smells. So in mini-course four we will see several + +13 +00:00:36,540 --> 00:00:39,480 +examples of bad smells and what we can do to eliminate them. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/2 - History of Design Patterns - lang_en_vs6.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/2 - History of Design Patterns - lang_en_vs6.srt new file mode 100644 index 0000000..5fbb886 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/2 - History of Design Patterns - lang_en_vs6.srt @@ -0,0 +1,179 @@ +1 +00:00:00,160 --> 00:00:02,780 +Let's start our decision of design patterns by looking + +2 +00:00:02,780 --> 00:00:05,280 +at the history of patterns. As you know, I like + +3 +00:00:05,280 --> 00:00:07,810 +to give this sort of historical perspective on how and + +4 +00:00:07,810 --> 00:00:10,600 +when concepts were defined. In this case, we have to + +5 +00:00:10,600 --> 00:00:14,830 +go back to 1977, when Christopher Alexander, an American professor + +6 +00:00:14,830 --> 00:00:18,300 +of architecture at UC Berkeley, introduces the idea of patterns, + +7 +00:00:18,300 --> 00:00:21,700 +successful solutions to problems, in his book called a Pattern + +8 +00:00:21,700 --> 00:00:25,640 +Language. The book contains about 250 patterns. And the idea + +9 +00:00:25,640 --> 00:00:27,700 +is that occupants of a building should be able + +10 +00:00:27,700 --> 00:00:30,110 +to design it. And the patterns in the book provide + +11 +00:00:30,110 --> 00:00:32,368 +a way to do that. And this idea of design + +12 +00:00:32,368 --> 00:00:35,964 +patterns, so, a formal way of documenting successful solutions to + +13 +00:00:35,964 --> 00:00:41,200 +problems, inspired several other disciplines. In particular, in 1987, + +14 +00:00:41,200 --> 00:00:44,810 +Ward Cunningham and Kent Beck leveraged this idea of Alexander's + +15 +00:00:44,810 --> 00:00:48,360 +patterns in the context of an object oriented language. + +16 +00:00:48,360 --> 00:00:50,840 +And in this specific the language was Smalltalk. + +17 +00:00:50,840 --> 00:00:52,666 +Some of you might know the language. So what Cunningham + +18 +00:00:54,492 --> 00:00:56,320 +and Beck did, was to create a 5 pattern + +19 +00:00:56,320 --> 00:00:59,880 +language for guiding novice Smalltalk programmers. So they did + +20 +00:00:59,880 --> 00:01:03,090 +an experiment and had several developers using their patterns, and + +21 +00:01:03,090 --> 00:01:06,330 +the experiment was extremely successful. The users were able to + +22 +00:01:06,330 --> 00:01:09,940 +create elegant designs using the provided patterns. And in case + +23 +00:01:09,940 --> 00:01:12,210 +you are interested in reading about it, Cunningham and Beck + +24 +00:01:12,210 --> 00:01:15,660 +reported the results in the article, Using Pattern Languages for + +25 +00:01:15,660 --> 00:01:17,940 +Object Oriented Programs, which was published at the + +26 +00:01:17,940 --> 00:01:21,854 +International Conference on Object Oriented Programming, Systems, Languages, and + +27 +00:01:21,854 --> 00:01:25,390 +Applications, also called OOPSLA, in 1987. At the + +28 +00:01:25,390 --> 00:01:28,480 +same time, Eric Gamma was working on his dissertation, + +29 +00:01:28,480 --> 00:01:31,030 +whose topic was the importance of patterns and + +30 +00:01:31,030 --> 00:01:34,430 +how to capture them. Between 1987 and 1992, there + +31 +00:01:34,430 --> 00:01:37,520 +were several workshops related to design patterns. And + +32 +00:01:37,520 --> 00:01:40,740 +in 1992, Jim Coplien compiled a catalog of C++ + +33 +00:01:40,740 --> 00:01:43,140 +items, which are some sort of patterns, and + +34 +00:01:43,140 --> 00:01:45,130 +he listed this catalog of patterns in his + +35 +00:01:45,130 --> 00:01:48,720 +book, which was titled Advanced C++ Programming Styles + +36 +00:01:48,720 --> 00:01:52,952 +and Idioms. Finally, in 1993 and 1994, there were + +37 +00:01:52,952 --> 00:01:56,160 +several additional workshops focused on patterns. And this + +38 +00:01:56,160 --> 00:01:59,625 +workshop brought together many patterns folks, including these + +39 +00:01:59,625 --> 00:02:03,040 +4 guys, Erich Gamma, Richard Helm, Ralph Johnson, + +40 +00:02:03,040 --> 00:02:06,040 +and John Vlissides. These guys are also known as + +41 +00:02:06,040 --> 00:02:08,970 +the gang of 4. And the result of this collaboration was the + +42 +00:02:08,970 --> 00:02:11,840 +famous book Design Patterns: Elements of + +43 +00:02:11,840 --> 00:02:14,320 +Reusable Object Oriented Software. So this + +44 +00:02:14,320 --> 00:02:17,640 +is basically The Book on design patterns. If you want to buy + +45 +00:02:17,640 --> 00:02:19,780 +a book on design pattern, this is the one you should get. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/3 - Patterns Catalogue - lang_en_vs4.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/3 - Patterns Catalogue - lang_en_vs4.srt new file mode 100644 index 0000000..764a309 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/3 - Patterns Catalogue - lang_en_vs4.srt @@ -0,0 +1,79 @@ +1 +00:00:00,230 --> 00:00:03,380 +This book contains a patterns catalog which is + +2 +00:00:03,380 --> 00:00:06,725 +a number of design patterns classified by purpose. And + +3 +00:00:06,725 --> 00:00:09,122 +there are five main classes of patterns. There are + +4 +00:00:09,122 --> 00:00:11,890 +fundamental patterns which are the basic patterns. There are + +5 +00:00:11,890 --> 00:00:15,170 +creational patterns which are the patterns that support object + +6 +00:00:15,170 --> 00:00:18,290 +creation. Then there are structural patterns and these are + +7 +00:00:18,290 --> 00:00:22,090 +patterns that help compose objects, put objects together. The + +8 +00:00:22,090 --> 00:00:25,280 +next class of patterns are behavioral patterns and these + +9 +00:00:25,280 --> 00:00:28,840 +are patterns that are mostly focused on realizing interactions + +10 +00:00:28,840 --> 00:00:32,820 +among different objects. Finally, there are concurrency patterns and these + +11 +00:00:32,820 --> 00:00:36,400 +are patterns that support, as the name says, concurrency, so + +12 +00:00:36,400 --> 00:00:39,700 +they're more related to concurrency aspects. And for each of + +13 +00:00:39,700 --> 00:00:43,560 +these classes there are a number of specific patterns, and + +14 +00:00:43,560 --> 00:00:46,130 +here I'm just listing some of them. Clearly we cannot + +15 +00:00:46,130 --> 00:00:48,740 +cover in one lesson all of these patterns, but what + +16 +00:00:48,740 --> 00:00:50,440 +I want to do is to cover at least a few + +17 +00:00:50,440 --> 00:00:52,670 +of those to give an idea of what patterns are and + +18 +00:00:52,670 --> 00:00:55,430 +how they can be used. In particular, we will see in + +19 +00:00:55,430 --> 00:00:59,200 +detail the Factory Method Pattern and the Strategy Pattern. And we + +20 +00:00:59,200 --> 00:01:01,590 +will also discuss a few more patterns at a higher level. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/4 - Pattern Format - lang_en_vs5.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/4 - Pattern Format - lang_en_vs5.srt new file mode 100644 index 0000000..7df86c0 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/4 - Pattern Format - lang_en_vs5.srt @@ -0,0 +1,71 @@ +1 +00:00:00,180 --> 00:00:02,711 +So let's start by seeing how patterns are defined. So + +2 +00:00:02,711 --> 00:00:05,620 +what is the format of the pattern definitions. If we look + +3 +00:00:05,620 --> 00:00:08,000 +at the Gang of Four's book we can see that these + +4 +00:00:08,000 --> 00:00:11,630 +definitions contain a lot of information. In fact, what I'm listing + +5 +00:00:11,630 --> 00:00:14,980 +here is just a subset of this information. In this lesson, + +6 +00:00:14,980 --> 00:00:17,510 +what I want to do is to focus on four essential + +7 +00:00:17,510 --> 00:00:21,480 +elements of a design pattern. It's name, the intent which is + +8 +00:00:21,480 --> 00:00:25,270 +the goal of the pattern. The pattern's applicability which is the + +9 +00:00:25,270 --> 00:00:28,040 +list of situations or context in which the + +10 +00:00:28,040 --> 00:00:31,560 +pattern is applicable. I also want to cover the structure + +11 +00:00:31,560 --> 00:00:34,700 +and participants. Which is the static model that describes + +12 +00:00:34,700 --> 00:00:37,870 +the elements, so normally the classes or the object + +13 +00:00:37,870 --> 00:00:39,900 +involved in the pattern. In addition to that + +14 +00:00:39,900 --> 00:00:41,400 +the structure also describes + +15 +00:00:41,400 --> 00:00:44,420 +the relationships, responsibilities and collaborations + +16 +00:00:44,420 --> 00:00:47,264 +among these classes or objects. Finally what I want + +17 +00:00:47,264 --> 00:00:50,560 +to cover is sample code. So examples that illustrate + +18 +00:00:50,560 --> 00:00:51,490 +the use of patterns. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/5 - Factory Method Pattern - lang_en_vs5.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/5 - Factory Method Pattern - lang_en_vs5.srt new file mode 100644 index 0000000..c7ebddd --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/5 - Factory Method Pattern - lang_en_vs5.srt @@ -0,0 +1,179 @@ +1 +00:00:00,012 --> 00:00:02,350 +Let's now look at the first design pattern that we + +2 +00:00:02,350 --> 00:00:05,700 +will discuss, the factory method pattern. And I'm going to start + +3 +00:00:05,700 --> 00:00:09,220 +by discussing the intent of the pattern and its applicability. + +4 +00:00:09,220 --> 00:00:12,210 +As far as the intent is concerned, the factory method pattern + +5 +00:00:12,210 --> 00:00:16,690 +allows for creating objects without specifying their class, by invoking + +6 +00:00:16,690 --> 00:00:19,370 +what we call a factory method. And what that is, is + +7 +00:00:19,370 --> 00:00:22,680 +a method whose main goal is to create class instances. + +8 +00:00:22,680 --> 00:00:25,510 +So when is this pattern useful? So when is it applicable? + +9 +00:00:25,510 --> 00:00:28,080 +For example, it is applicable in cases in which a class + +10 +00:00:28,080 --> 00:00:31,890 +cannot anticipate the type of object it must create. That is, + +11 +00:00:31,890 --> 00:00:34,800 +the type of an object is not known at compile time, + +12 +00:00:34,800 --> 00:00:37,800 +is not known until the code runs. A typical example of + +13 +00:00:37,800 --> 00:00:40,500 +this, are frameworks. So if you ever used a framework, you + +14 +00:00:40,500 --> 00:00:44,280 +will know that, normally, frameworks only know about interfaces and abstract + +15 +00:00:44,280 --> 00:00:47,450 +classes. So the exact type of the objects of these classes + +16 +00:00:47,450 --> 00:00:50,840 +is only known at runtime. The second case in which the factory + +17 +00:00:50,840 --> 00:00:53,835 +method pattern is applicable, is when a class wants its + +18 +00:00:53,835 --> 00:00:57,160 +subclasses to specify the type of objects it creates. And we'll + +19 +00:00:57,160 --> 00:00:59,920 +see an example of this in a minute. Finally, factory + +20 +00:00:59,920 --> 00:01:03,580 +method patterns are applicable when a class needs control over the + +21 +00:01:03,580 --> 00:01:06,760 +creation of its objects. And in this case, one possible + +22 +00:01:06,760 --> 00:01:09,380 +example is when there is a limit on the number of + +23 +00:01:09,380 --> 00:01:12,930 +objects that can be created. Special example, it's a singleton. If + +24 +00:01:12,930 --> 00:01:15,840 +you're familiar with a singleton, a singleton is a class for + +25 +00:01:15,840 --> 00:01:18,930 +which only one instance can be created. The factory method pattern + +26 +00:01:18,930 --> 00:01:21,760 +is perfect in these cases, because it allows to control how many + +27 +00:01:21,760 --> 00:01:24,640 +objects get created. So in this case, it would allow the creation + +28 +00:01:24,640 --> 00:01:27,290 +only of a single object. And from the second time that it + +29 +00:01:27,290 --> 00:01:30,040 +is invoked, it will just return the object that was previously + +30 +00:01:30,040 --> 00:01:33,700 +created. Now let's go ahead and see how this pattern actually works, + +31 +00:01:33,700 --> 00:01:37,100 +and let's do that by discussing the structure and the participants for + +32 +00:01:37,100 --> 00:01:41,330 +the pattern. The structure that is represented here, using the UML notation, + +33 +00:01:41,330 --> 00:01:45,530 +includes three classes, the Creator, the ConcreteCreator, + +34 +00:01:45,530 --> 00:01:48,000 +and the Product. The Creator provides the + +35 +00:01:48,000 --> 00:01:50,710 +interface for the factory method. So this + +36 +00:01:50,710 --> 00:01:53,200 +here, is the interface for the factory method + +37 +00:01:53,200 --> 00:01:55,950 +that, when invoked, returns an object of + +38 +00:01:55,950 --> 00:01:59,440 +type Product. The ConcreteCreator provides the actual + +39 +00:01:59,440 --> 00:02:02,350 +method for creating the Product. So this + +40 +00:02:02,350 --> 00:02:06,170 +method is a concrete implementation of this interface. + +41 +00:02:06,170 --> 00:02:10,630 +Finally, the Product is the object created by the factory method. So + +42 +00:02:10,630 --> 00:02:12,350 +summarizing, we have the interface for + +43 +00:02:12,350 --> 00:02:14,300 +the factory method, the actual implementation of + +44 +00:02:14,300 --> 00:02:17,540 +the summary method, and the object that is created by the factory method, + +45 +00:02:17,540 --> 00:02:21,020 +when it is invoked. So let's look at an example of this pattern. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/6 - Factory Method Pattern Example - lang_en_vs5.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/6 - Factory Method Pattern Example - lang_en_vs5.srt new file mode 100644 index 0000000..2812c39 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/6 - Factory Method Pattern Example - lang_en_vs5.srt @@ -0,0 +1,127 @@ +1 +00:00:00,220 --> 00:00:03,260 +The example I'm going to use consists of a class called + +2 +00:00:03,260 --> 00:00:07,380 +ImageReaderFactory which provides the factory method which is this one; + +3 +00:00:07,380 --> 00:00:10,250 +createImageReader. As you can see the method takes an InputStream + +4 +00:00:10,250 --> 00:00:13,570 +as input and returns an object of type ImageReader, and it's + +5 +00:00:13,570 --> 00:00:15,645 +static so that we can invoke it even if we + +6 +00:00:15,645 --> 00:00:18,390 +don't have an instance of the ImageReaderFactory. So what does the + +7 +00:00:18,390 --> 00:00:22,275 +method do? Well the method first invokes, getImageType, passing the + +8 +00:00:22,275 --> 00:00:25,780 +InputStream as a parameter and this method figures out the type + +9 +00:00:25,780 --> 00:00:28,820 +of the image that is stored in this Inputstream and it's + +10 +00:00:28,820 --> 00:00:32,740 +an integer. Then, based on the value of this integer, the + +11 +00:00:32,740 --> 00:00:35,352 +method does one of several things. If the image type is + +12 +00:00:35,352 --> 00:00:38,970 +a GIF, it will invoke the constructor for GifReader passing the + +13 +00:00:38,970 --> 00:00:41,610 +stream as a parameter. And what will happen is that the + +14 +00:00:41,610 --> 00:00:44,450 +GIF reader will read a GIF from the stream, create a + +15 +00:00:44,450 --> 00:00:47,887 +corresponding object and return it. So in this case, the ImageReader + +16 +00:00:47,887 --> 00:00:51,460 +object return will be the object representing a GIF as appropriate. + +17 +00:00:51,460 --> 00:00:54,610 +Similarly, if the image type is JPEG, then the method will + +18 +00:00:54,610 --> 00:00:58,579 +invoke the constructor for JPEG Reader and in this case, this constructor + +19 +00:00:58,579 --> 00:01:01,981 +will read from the stream a JPEG, create a corresponding object + +20 +00:01:01,981 --> 00:01:05,640 +and return it. And so on for different types of images. So + +21 +00:01:05,640 --> 00:01:07,880 +why is this a situation in which it is appropriate to + +22 +00:01:07,880 --> 00:01:11,100 +use the factory method pattern? One, because it corresponds exactly to the + +23 +00:01:11,100 --> 00:01:14,250 +cases that we saw before, of applicability. This is a case + +24 +00:01:14,250 --> 00:01:16,560 +in which we don't know the type of the object that we + +25 +00:01:16,560 --> 00:01:20,080 +need to create until we run the code, because it depends + +26 +00:01:20,080 --> 00:01:22,530 +on the value of the InputStream. It depends on the content + +27 +00:01:22,530 --> 00:01:25,590 +of the InputStream. So, until we read the InputStream, we cannot + +28 +00:01:25,590 --> 00:01:28,380 +figure out whether we need to create a GIF, a JPEG or + +29 +00:01:28,380 --> 00:01:30,780 +some other type of image. So in this case, we want to + +30 +00:01:30,780 --> 00:01:33,630 +do, we want to simply delegate to this classes the creation of + +31 +00:01:33,630 --> 00:01:35,610 +the object, once we know what type of object needs to + +32 +00:01:35,610 --> 00:01:38,790 +be created. So perfect example of application of a factory method pattern. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/7 - Strategy Pattern - lang_en_vs5.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/7 - Strategy Pattern - lang_en_vs5.srt new file mode 100644 index 0000000..288b8d6 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/7 - Strategy Pattern - lang_en_vs5.srt @@ -0,0 +1,143 @@ +1 +00:00:00,230 --> 00:00:02,110 +The second pattern I want to discuss is the + +2 +00:00:02,110 --> 00:00:05,050 +strategy pattern, which provides a way to configure a + +3 +00:00:05,050 --> 00:00:07,900 +class with one of many behaviors. What does that + +4 +00:00:07,900 --> 00:00:11,040 +mean? Well, more precisely, this pattern allows for defining + +5 +00:00:11,040 --> 00:00:15,330 +a family of algorithms, encapsulating them into separate classes, + +6 +00:00:15,330 --> 00:00:17,900 +so each algorithm in one class, and making these + +7 +00:00:17,900 --> 00:00:21,490 +classes interchangeable, but providing a common interface for all + +8 +00:00:21,490 --> 00:00:25,350 +the encapsulated algorithms. So in essence, the intent of + +9 +00:00:25,350 --> 00:00:29,250 +a strategy pattern is to allow for switching between + +10 +00:00:29,250 --> 00:00:33,490 +different algorithms for accomplishing a given task. For example, imagine + +11 +00:00:33,490 --> 00:00:36,610 +having different sorting algorithms with different space or time + +12 +00:00:36,610 --> 00:00:38,800 +tradeoffs. You might want to be able to have them + +13 +00:00:38,800 --> 00:00:42,670 +all available and use different ones in different situations. + +14 +00:00:42,670 --> 00:00:44,820 +And this pattern is applicable not only when we have + +15 +00:00:44,820 --> 00:00:47,260 +different variants of an algorithm, but also when we + +16 +00:00:47,260 --> 00:00:51,690 +have many related classes that differ only in their behavior. + +17 +00:00:51,690 --> 00:00:53,640 +So let's get more concrete and see how this is + +18 +00:00:53,640 --> 00:00:55,960 +done. And I'm going to do it as before, by + +19 +00:00:55,960 --> 00:00:59,700 +discussing the structure and the participants for this strategy pattern. + +20 +00:00:59,700 --> 00:01:02,540 +In this case, we have 3 types of participants for this + +21 +00:01:02,540 --> 00:01:07,210 +pattern, the context, the algorithm, and the concrete strategies. There + +22 +00:01:07,210 --> 00:01:09,580 +can be as many as the number of behaviors that + +23 +00:01:09,580 --> 00:01:12,300 +I need to implement. So, let's see what those are. + +24 +00:01:12,300 --> 00:01:16,690 +The context is the interface to the outside world. It maintains + +25 +00:01:16,690 --> 00:01:19,180 +a reference to the current algorithm and allows for + +26 +00:01:19,180 --> 00:01:22,860 +updating this reference at run time. So, basically the outside + +27 +00:01:22,860 --> 00:01:26,370 +world will invoke the functionality provided by the different algorithms, + +28 +00:01:26,370 --> 00:01:29,170 +by using this interface. And depending on which algorithm is + +29 +00:01:29,170 --> 00:01:31,640 +currently selected, that's the one that will be executed when + +30 +00:01:31,640 --> 00:01:35,920 +the functionality is involved. The algorithm, also called the strategy, + +31 +00:01:35,920 --> 00:01:37,970 +so that's where the pattern gets its name, Is the + +32 +00:01:37,970 --> 00:01:42,130 +common interface for the different algorithims. So all the algorithms + +33 +00:01:42,130 --> 00:01:46,690 +implement this interface. Finally, the concrete strategies are the + +34 +00:01:46,690 --> 00:01:49,920 +actual implementations of the algorithms. So if I have 10 + +35 +00:01:49,920 --> 00:01:53,030 +different variants of my algorithm, I will implement 10 different + +36 +00:01:53,030 --> 00:01:56,550 +concrete strategies. They will all be implementations of this interface. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/8 - Strategy Pattern Example & Demo - lang_en_vs5.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/8 - Strategy Pattern Example & Demo - lang_en_vs5.srt new file mode 100644 index 0000000..6d298d0 --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/8 - Strategy Pattern Example & Demo - lang_en_vs5.srt @@ -0,0 +1,511 @@ +1 +00:00:00,130 --> 00:00:03,320 +Now let's see how this whole thing works in practice by + +2 +00:00:03,320 --> 00:00:06,800 +using an example. We're going to consider a program that takes as + +3 +00:00:06,800 --> 00:00:10,450 +input a text file and produce it as output, a filtered + +4 +00:00:10,450 --> 00:00:14,170 +file. So basically it outputs a subset of the content of + +5 +00:00:14,170 --> 00:00:16,928 +this text file based on some filter. And we're going to have + +6 +00:00:16,928 --> 00:00:19,440 +four different types of filters. So the first one is + +7 +00:00:19,440 --> 00:00:21,680 +not filtering which means that the whole content of the text + +8 +00:00:21,680 --> 00:00:25,320 +file will be produced on the output. The second filter will output + +9 +00:00:25,320 --> 00:00:27,990 +only words that starts with t. So you'll take the text file + +10 +00:00:27,990 --> 00:00:30,540 +and simply ignore all of the words that do not start with + +11 +00:00:30,540 --> 00:00:33,130 +t. So in the output we'll have only those words that starts + +12 +00:00:33,130 --> 00:00:36,030 +with letter t. The third filter will produce in the output only + +13 +00:00:36,030 --> 00:00:39,180 +words that are longer than five characters. So all the other words + +14 +00:00:39,180 --> 00:00:43,740 +will be simply disregarded. And finally, the four filter will produce as + +15 +00:00:43,740 --> 00:00:47,630 +output only words in the text file that are palindromes, and in + +16 +00:00:47,630 --> 00:00:50,590 +case you don't know what a palindrome is, a palindrome is a word + +17 +00:00:50,590 --> 00:00:52,700 +that is the same whether you read it from left + +18 +00:00:52,700 --> 00:00:55,800 +to right or from right to left. For example, the + +19 +00:00:55,800 --> 00:00:58,480 +word kayak, you can read it in this direction, or + +20 +00:00:58,480 --> 00:01:00,740 +in this direction, and it's exactly the same word. So + +21 +00:01:00,740 --> 00:01:03,560 +let's see how this program could be implemented using a + +22 +00:01:03,560 --> 00:01:05,980 +strategy pattern. And let's do it for real as a + +23 +00:01:05,980 --> 00:01:10,100 +demo. What we're looking at here is the editor page + +24 +00:01:10,100 --> 00:01:15,520 +for Eclipse, open with the strategy pattern implementation for our example. + +25 +00:01:15,520 --> 00:01:17,130 +So what I'm going to do is that, I'm going to look at a + +26 +00:01:17,130 --> 00:01:20,310 +different part of implementation. And you will see that, you know, despite + +27 +00:01:20,310 --> 00:01:23,420 +the fact that it's slightly longer, it's really fairly simple, it's kind + +28 +00:01:23,420 --> 00:01:26,230 +of a straightforward implementation of what we just saw. As I just + +29 +00:01:26,230 --> 00:01:29,820 +said, what we are doing is basically building the strategy patterns that + +30 +00:01:29,820 --> 00:01:34,330 +allows for changing the strategies with which we're filtering an input file. + +31 +00:01:34,330 --> 00:01:37,380 +And we have different strategies, we'll look at those in detail, and + +32 +00:01:37,380 --> 00:01:41,050 +we said that the three participants for this pattern are the context, + +33 +00:01:41,050 --> 00:01:43,650 +the algorithm, which is the general interface and then the concrete + +34 +00:01:43,650 --> 00:01:47,270 +strategies, which are the concrete implementations of this algorithm. So let's + +35 +00:01:47,270 --> 00:01:49,790 +start by looking at the context. Which is this class here. + +36 +00:01:49,790 --> 00:01:52,960 +And as you can see it contains a reference at the current + +37 +00:01:52,960 --> 00:01:56,240 +strategy. We call this the check strategy, which is basically our + +38 +00:01:56,240 --> 00:01:59,910 +filter, and when the context is created by default it sets a + +39 +00:01:59,910 --> 00:02:02,890 +strategy to the old strategy. The old strategy is the one + +40 +00:02:02,890 --> 00:02:06,380 +that accepts all the input, so basically it doesn't filter out anything. + +41 +00:02:06,380 --> 00:02:08,320 +And we said that the context is the interface to the + +42 +00:02:08,320 --> 00:02:10,889 +outside world, right? So it has to provide the outside world + +43 +00:02:10,889 --> 00:02:14,140 +with a way of selecting the strategy, the specific algorithm to + +44 +00:02:14,140 --> 00:02:16,850 +be used, and it does that in this case by providing + +45 +00:02:16,850 --> 00:02:21,360 +this change strategy method. This method takes a strategy as input, + +46 +00:02:21,360 --> 00:02:24,930 +and simply replaces the current strategy with the one specified as + +47 +00:02:24,930 --> 00:02:28,035 +a parameter. And at this point, the context also will perform + +48 +00:02:28,035 --> 00:02:31,830 +the filtering. The filtering is pretty straightforward, so what it does is + +49 +00:02:31,830 --> 00:02:34,620 +that it opens a file that is passed as a parameter + +50 +00:02:34,620 --> 00:02:37,450 +so that this the file, the input file to be filtered. And + +51 +00:02:37,450 --> 00:02:40,560 +then it reads the file line by line and then splits + +52 +00:02:40,560 --> 00:02:43,620 +the lines into its composing words and then for each word in + +53 +00:02:43,620 --> 00:02:46,480 +each line, what it will do, it will basically invoke the + +54 +00:02:46,480 --> 00:02:50,270 +check method in the current strategy, which is basically the filtering method + +55 +00:02:50,270 --> 00:02:53,580 +and if the check method returns true, which basically means that + +56 +00:02:53,580 --> 00:02:57,150 +the word should be printed, it prints the word. Otherwise, it'll just + +57 +00:02:57,150 --> 00:03:00,480 +skip it. So basically the filter will return false for all the + +58 +00:03:00,480 --> 00:03:03,470 +words that have to be filtered out. Okay? This is the basic + +59 +00:03:03,470 --> 00:03:06,770 +way in which context works. Let's see how this is used in + +60 +00:03:06,770 --> 00:03:10,660 +our main method. The main method simply creates the context, reads the input file + +61 +00:03:10,660 --> 00:03:12,910 +from the arguments, and then what he does is simply as a + +62 +00:03:12,910 --> 00:03:16,720 +demonstration, it will perform the filtering using all the different filters. So + +63 +00:03:16,720 --> 00:03:19,310 +starting from the default one, which is the one that basically doesn't + +64 +00:03:19,310 --> 00:03:22,150 +do any filtering that reports all words, then it will switch to the + +65 +00:03:22,150 --> 00:03:25,400 +algorithm, that only considers the words that start with t, and + +66 +00:03:25,400 --> 00:03:28,880 +it will do that by invoking a change strategy and passing this + +67 +00:03:28,880 --> 00:03:30,890 +strategy as the argument, and then + +68 +00:03:30,890 --> 00:03:32,760 +performing the actual filtering through context. + +69 +00:03:32,760 --> 00:03:35,040 +And it will do exactly the same for the strategy that only + +70 +00:03:35,040 --> 00:03:37,540 +prints words that are longer than five and the one that + +71 +00:03:37,540 --> 00:03:40,460 +only prints words that are palindromes. So now let's look at the + +72 +00:03:40,460 --> 00:03:44,090 +actual algorithm. This is the interface, the algorithm interface. And you can + +73 +00:03:44,090 --> 00:03:47,080 +see that the only thing that the interface provides is this method, + +74 +00:03:47,080 --> 00:03:49,760 +which is the check method, that takes a string as input and will + +75 +00:03:49,760 --> 00:03:52,470 +return a boolean. So, basically, it's the boolean that we were seeing before. + +76 +00:03:52,470 --> 00:03:55,010 +The one that is true for the words that have to be printed + +77 +00:03:55,010 --> 00:03:57,490 +and false for the ones that have to be filtered out. Now, we have + +78 +00:03:57,490 --> 00:04:01,250 +all the different implementations of the algorithm, the simplest one is the all + +79 +00:04:01,250 --> 00:04:05,110 +algorithm, the simple return is always true, so all the words will be printed. + +80 +00:04:05,110 --> 00:04:08,740 +The second one starts with t, and again, without looking at the details + +81 +00:04:08,740 --> 00:04:10,660 +of implementations that don't really matter, what + +82 +00:04:10,660 --> 00:04:12,390 +it does is basically check that + +83 +00:04:12,390 --> 00:04:15,111 +the first character is t, and returns true in that case and + +84 +00:04:15,111 --> 00:04:17,720 +false otherwise. Similarly, for the LongerThan5 + +85 +00:04:17,720 --> 00:04:19,380 +algorithm, also in this case, this + +86 +00:04:19,380 --> 00:04:23,000 +will implement the check strategy interface, and the check will be performed + +87 +00:04:23,000 --> 00:04:25,980 +by checking that the word is longer than five characters and returning + +88 +00:04:25,980 --> 00:04:29,440 +true in that case and false otherwise. And finally the Palindrome check + +89 +00:04:29,440 --> 00:04:32,240 +is a little more complicated, but basically it just checks whether the + +90 +00:04:32,240 --> 00:04:35,190 +word is a Palindrome and returns true in that case. Okay, so + +91 +00:04:35,190 --> 00:04:37,950 +as I said, it doesn't really matter too much what is the specific + +92 +00:04:37,950 --> 00:04:40,630 +implementations of these matters. What matters is that we have + +93 +00:04:40,630 --> 00:04:44,150 +a general interface for the algorithm and then any different concrete + +94 +00:04:44,150 --> 00:04:48,130 +implementations of the algorithm that implement different strategies. So again, + +95 +00:04:48,130 --> 00:04:50,730 +this allows you to change the behavior of your class without + +96 +00:04:50,730 --> 00:04:53,420 +changing class. So we have this context class that does + +97 +00:04:53,420 --> 00:04:57,015 +different things when the filter method in invoked, depending on what + +98 +00:04:57,015 --> 00:04:59,930 +is the current strategy. So the behavior of the class can + +99 +00:04:59,930 --> 00:05:03,310 +change dynamically, and it changes dynamically every time that we change + +100 +00:05:03,310 --> 00:05:06,300 +the strategy. At this point, the way this whole thing works should + +101 +00:05:06,300 --> 00:05:08,430 +be clear, so what we're going to do is that we're going to go to + +102 +00:05:08,430 --> 00:05:12,010 +our console, and we're actually going to run the strategy pattern and see + +103 +00:05:12,010 --> 00:05:15,710 +what happens. So here I have a file, it's called foo.txt. And if + +104 +00:05:15,710 --> 00:05:18,290 +we look at the content of foo, you can see that it + +105 +00:05:18,290 --> 00:05:21,190 +says that this is just a test to assess how well this program + +106 +00:05:21,190 --> 00:05:24,430 +performs when used on files of text. And since it checks for + +107 +00:05:24,430 --> 00:05:28,560 +palindromes, we will also insert one such word, level. Level is a palindrome, + +108 +00:05:28,560 --> 00:05:31,042 +because you can read it from both sides. Okay, so let's + +109 +00:05:31,042 --> 00:05:33,657 +see what happens when we run our code. So we're going to + +110 +00:05:33,657 --> 00:05:36,900 +run java pattern.strategy.StrategyPattern which is + +111 +00:05:36,900 --> 00:05:38,550 +our class, and we going to fetch + +112 +00:05:38,550 --> 00:05:41,460 +foo.txt as an input, and let's go back to the beginning + +113 +00:05:41,460 --> 00:05:43,980 +of the output to see what happened exactly. You can see + +114 +00:05:43,980 --> 00:05:48,040 +here that for the default strategy, which was the old strategy, + +115 +00:05:48,040 --> 00:05:50,810 +the whole file is printed, so every word is printed. This + +116 +00:05:50,810 --> 00:05:53,485 +is just a test to assess and so on and so forth, + +117 +00:05:53,485 --> 00:05:57,290 +as expected. For the filter that only prints words that + +118 +00:05:57,290 --> 00:06:00,230 +start with t, only words that start with t are printed, + +119 +00:06:00,230 --> 00:06:04,180 +again, as expected. Similarly, for the filter that only prints words + +120 +00:06:04,180 --> 00:06:06,970 +that are longer than 5, and finally for the one that prints + +121 +00:06:06,970 --> 00:06:09,540 +palindromes. And here you can see that we actually have two + +122 +00:06:09,540 --> 00:06:12,410 +because the way in which this is implemented we'll also consider + +123 +00:06:12,410 --> 00:06:15,300 +single letter words as palindromes because you can read them from + +124 +00:06:15,300 --> 00:06:18,450 +both sides. But you definitely will also have level in the output. + +125 +00:06:18,450 --> 00:06:21,040 +And in case you want to play with this code yourself, I + +126 +00:06:21,040 --> 00:06:24,600 +have made this code and also the implementation for examples of other + +127 +00:06:24,600 --> 00:06:28,440 +design partners available as a compressed archive. And the archive is accessible + +128 +00:06:28,440 --> 00:06:31,120 +through a URL that is provided in the notes for the cost. diff --git a/usth/ICT2.7/P3L3 Design Patterns Subtitles/9 - Other Common Patterns - lang_en_vs5.srt b/usth/ICT2.7/P3L3 Design Patterns Subtitles/9 - Other Common Patterns - lang_en_vs5.srt new file mode 100644 index 0000000..756f38f --- /dev/null +++ b/usth/ICT2.7/P3L3 Design Patterns Subtitles/9 - Other Common Patterns - lang_en_vs5.srt @@ -0,0 +1,295 @@ +1 +00:00:00,060 --> 00:00:03,460 +Before concluding this lesson, let's look at a few more patterns. And + +2 +00:00:03,460 --> 00:00:05,880 +although it will take too long to cover them in detail, I + +3 +00:00:05,880 --> 00:00:08,986 +would like to at least mention and quickly discuss a few more + +4 +00:00:08,986 --> 00:00:12,080 +of these more commonly-used patterns. In fact, some of the patterns that + +5 +00:00:12,080 --> 00:00:15,400 +I will discuss, you might have used yourself. Maybe without knowing their + +6 +00:00:15,400 --> 00:00:18,300 +name or the fact that they were design patterns. So let's start + +7 +00:00:18,300 --> 00:00:21,660 +with a Visitor pattern, which is a way of separating an algorithm + +8 +00:00:21,660 --> 00:00:25,150 +from an object structure on which it operates. And a practical result + +9 +00:00:25,150 --> 00:00:28,010 +of this separation is the ability to add the new operation + +10 +00:00:28,010 --> 00:00:31,680 +to exist in object structures, without modifying the structures. So, basically + +11 +00:00:31,680 --> 00:00:34,540 +what this pattern does, is to allow for defining and easily + +12 +00:00:34,540 --> 00:00:37,870 +modifying set of operations to perform on the objects of the collection. + +13 +00:00:37,870 --> 00:00:40,570 +And the typical usage of this is, for example, when you're + +14 +00:00:40,570 --> 00:00:43,140 +visiting a graph, or a set of objects, and you want + +15 +00:00:43,140 --> 00:00:46,090 +to perform some operations on these objects. By using a visitor + +16 +00:00:46,090 --> 00:00:48,410 +pattern, you can decouple the operation + +17 +00:00:48,410 --> 00:00:50,830 +from the objects. Although not straightforward, + +18 +00:00:50,830 --> 00:00:53,360 +this pattern is very, very useful. So, I really encourage you + +19 +00:00:53,360 --> 00:00:56,060 +to look at it in more detail and get familiar with it. + +20 +00:00:56,060 --> 00:00:59,040 +The second pattern I want to mention is the decorator pattern. + +21 +00:00:59,040 --> 00:01:02,820 +The decorator pattern is basically a wrapper that adds functionality to a + +22 +00:01:02,820 --> 00:01:05,030 +class. So the way in which it works, is that you + +23 +00:01:05,030 --> 00:01:08,230 +will take a class, you will build a class that basically wraps + +24 +00:01:08,230 --> 00:01:12,250 +this class. So it reproduces the functionality of the original class, but + +25 +00:01:12,250 --> 00:01:15,900 +it also adds some functionality. And for all the functionality that was + +26 +00:01:15,900 --> 00:01:18,750 +already in the original class, it will simply invoke this + +27 +00:01:18,750 --> 00:01:21,080 +functionality and for the new one, you will implement it + +28 +00:01:21,080 --> 00:01:24,510 +using the services of the class. And a nice property + +29 +00:01:24,510 --> 00:01:26,760 +of the decorator pattern is that it's stackable. So you can + +30 +00:01:26,760 --> 00:01:30,210 +add decorators on decorators on decorators, and further increase the + +31 +00:01:30,210 --> 00:01:34,052 +functionality provided by your class. The iterator is another very + +32 +00:01:34,052 --> 00:01:37,810 +commonly-used pattern. And, you probably use this one yourself because, + +33 +00:01:37,810 --> 00:01:41,090 +it's also part of many standard libraries. What the iterator allows + +34 +00:01:41,090 --> 00:01:44,220 +you to do, is basically to access elements of a collection + +35 +00:01:44,220 --> 00:01:47,490 +without knowing the underlying representation. So the iterator will allow you + +36 +00:01:47,490 --> 00:01:50,630 +to just go through a set of objects without worrying about + +37 +00:01:50,630 --> 00:01:53,200 +how the objects are stored. So you basically just ask the + +38 +00:01:53,200 --> 00:01:55,810 +iterator to give you the first object, the next object and + +39 +00:01:55,810 --> 00:02:00,130 +so on. Another very commonly-used pattern is the observer pattern. And + +40 +00:02:00,130 --> 00:02:02,650 +this pattern is very useful when you have an object of + +41 +00:02:02,650 --> 00:02:06,190 +interest and a set of other objects that are interested in + +42 +00:02:06,190 --> 00:02:09,240 +the changes that might occur in this first object. So + +43 +00:02:09,240 --> 00:02:12,690 +what the observer pattern allows you to do is to register + +44 +00:02:12,690 --> 00:02:15,460 +these objects, so that they let the system know that + +45 +00:02:15,460 --> 00:02:18,690 +they're interested in changes in this first object. And then, every + +46 +00:02:18,690 --> 00:02:20,840 +time that there is a change, these other objects will + +47 +00:02:20,840 --> 00:02:23,030 +be automatically notified. So basically, + +48 +00:02:23,030 --> 00:02:25,290 +the observer pattern allows for notifying + +49 +00:02:25,290 --> 00:02:29,310 +dependents when an object of interest changes. If you want + +50 +00:02:29,310 --> 00:02:32,020 +an example of this, just think about the file system and + +51 +00:02:32,020 --> 00:02:35,870 +imagine having a folder. All the views of this folder will + +52 +00:02:35,870 --> 00:02:37,970 +want to be notified every time that there's a change in + +53 +00:02:37,970 --> 00:02:40,720 +the folder because they need to refresh. So instead of continuously + +54 +00:02:40,720 --> 00:02:44,390 +checking the state of the folder, they will just register and basically + +55 +00:02:44,390 --> 00:02:47,430 +say, hey, we're interested in knowing when something changes in this + +56 +00:02:47,430 --> 00:02:50,320 +folder. And when something changes in the folder, they will be automatically + +57 +00:02:50,320 --> 00:02:53,300 +notified. So it will be some sort of a push notification + +58 +00:02:53,300 --> 00:02:56,590 +instead of a pull notification, if you are familiar with that terminology. + +59 +00:02:56,590 --> 00:03:00,020 +Finally the proxy pattern is a pattern in which a surrogate + +60 +00:03:00,020 --> 00:03:04,370 +controls access to an object. In other words, we have our object, + +61 +00:03:04,370 --> 00:03:07,220 +and we have our proxy here. So all the requests to the + +62 +00:03:07,220 --> 00:03:09,950 +object will go through the proxy that will then forward them. And + +63 +00:03:09,950 --> 00:03:12,020 +all the responses from the object will also go through the + +64 +00:03:12,020 --> 00:03:15,580 +proxy. They will then forward them to the original requester. So what + +65 +00:03:15,580 --> 00:03:18,710 +the proxy allows you to do is to control how this object, + +66 +00:03:18,710 --> 00:03:22,180 +that is behind the proxy, is actually accessed, for example, by filtering + +67 +00:03:22,180 --> 00:03:24,500 +some calls. So in a sense, the proxy allows use + +68 +00:03:24,500 --> 00:03:27,470 +for masking some of the functionality of the object that + +69 +00:03:27,470 --> 00:03:31,070 +is behind the proxy. And there's many, many, many more + +70 +00:03:31,070 --> 00:03:34,424 +useful patterns. That can help you when designing and implementing + +71 +00:03:34,424 --> 00:03:37,220 +the system. So once more, I really encourage you to + +72 +00:03:37,220 --> 00:03:38,740 +have a look at the book, to look at the + +73 +00:03:38,740 --> 00:03:41,570 +resources online, and to really get more familiar with these + +74 +00:03:41,570 --> 00:03:43,890 +patterns, and to try to use them in your everyday work. -- cgit 1.4.1