Полная расшифровка видео «[Behavioral 6] Memento design pattern with TypeScript example». Нажмите на таймкод, чтобы открыть видео с этого момента. Нужно другое видео? Воспользуйтесь генератором расшифровки YouTube.
0:04 hello hello and welcome to @IntegratorDima channel another Behavior design pattern named Moto I would like to say it like momento but no it's momento it's very useful in case you need to store some state of your object and then access state in previous stage or in previous time so you can think also about functionality like undo Redo for example if you are doing some editor and you would like to revert some steps this is actually what we will do today in this example but also you can think of gaming where you need to access State at a certain moment of time so you can use momento there as well okay so let's check usage and then we will just uh
0:57 display implementation of uh this in typescript so we would like to interact with a text editor and where you would be able to type some something it will be a string and we will add more strings uh and it will be somehow stored in our history object so we will have like an editor history and momento will be dis that will save the moment and the state a certain moment we would type some strings and then we would like to uh save it to editor and then we will display the current state and revert few steps back and we we'll see what's going on so let's start from the
1:49 momento class that only stores our state okay so that's it we have a state as a private property we initialize momento in Pass State there and at any moment we can access this state using this get State method so let's move on the next actor in this example will be originator class it will be actually the text editor and it can save its states to momento and actually do different manipulations with the state so let's uh display it so here is text editor class actually it um interact with a Content so you can type some
2:40 words it will be string and we will concatenate string with new words so we can uh create a momento snapshot of our current state and uh here is where we return editor momento uh and pass some context text that should be saved also we can restore so we would like to access get state of the momento at that stage so we we should pass momento object and then uh get it State and um yeah at any moment we can get content to display current content of our
3:26 text editor so the last piece of this example will be history object caretaker class that keep keeps track of our MOS so it will be just a simple array where we will push new items and we will be able to access each item so yeah editor history so yeah this is an area of editor momento and yeah we will be just pushing elements here and then at certain moment we can access Memento by index so let's go again and see what we have here so we will clear console so we we create an editor we create a history object then we type some string and um
4:24 we save it so save the first state then we type another string and um save it as well and then we display results so this is third sentence this is the first second and third so you can see that current content so this line is actually three three sentence and um we made one undo took the get momento of element with index one we can imagine index uh 0 one and then I think we we executed type one more time so then we rever like reverted one stage
5:11 and uh after undo we can see this is the first sentence and this is the second sentence and if we will do one more undo we will have just this is the first sentence so yeah let's do it okay okay and run so you can see after second undo we have only the first sentence so this is actually it the basic idea try it in action please like And subscribe and write in comments if you haven't seen momento design pattern or if you have used it in your work projects see you in the next one bye