1コードのチュートリアル
1.1コードチュートリアル1
1コードのチュートリアル
1.1コードチュートリアル1
要件:
ウェブサイトのノートには、ロールバック機能を実現するために、スナップショットを保存する必要があります。
注意:
デザイン、それはスナップショットスナップショット管理クラスクラスのノート、クラスのノートやメモに三つのクラスを分けることができます。
B 注意スナップショット管理および集約関係ノートは、スナップショットは、スナップショット管理は、ゼロ以上のノートスナップショットであることができます。
スナップショットの道のスタックとCのスナップショット管理クラスのストレージ、最後のアウト、LIFO。
UMLクラス図:(右はテストクラスです)
テストカテゴリ:
パッケージcom.geely.design.pattern.behavioral.memento; ** / 吉利によって作成* * / パブリック クラスのテスト{ 公共の 静的な 無効メイン(文字列[] argsが){ ArticleMementoManager articleMementoManager = 新新ArticleMementoManager(); この記事は、この記事があった条 = 新しい新しいこの記事は記事でした( "潜ん設計パターンA"、 "NotesコンテンツA"、 "注意画像A" ); articleMemento articleMemento = article.saveToMemento(); articleMementoManager.addMemento(articleMemento) のSystem.out.println("タイトル:" + article.getTitle()+ "内容:" + article.getContent()+ "ピクチャー:" + article.getImgs()+ "の成功をステージング" ); System.out.printlnは(「完全な情報についての注意事項: "+ Articleこの記事は)だった; System.out.printlnは( "レビューノートスタート" ); article.setTitle( "潜んでいるデザインパターンB " ); article.setContent( " NotesコンテンツB " ); article.setImgs( "ノートの画像B " ); System.out.printlnは( "レビューノートエンド" ); System.out.printlnは( "完全な情報注意:「+ Articleこの記事はあった); articleMemento = article.saveToMemento(); articleMementoManager.addMemento(articleMemento)。 article.setTitle( "のように影のデザインパターンC" ); article.setContent( "NotesコンテンツC" ); article.setImgs( "メモピクチャーC" ); System.out.printlnは( "スクラッチロールバックの開始" ); System.out.printlnは( "バック出口スタック1" ); articleMemento = articleMementoManager.getMemento(); article.undoFromMemento(articleMemento); System.out.printlnは( "バック回スタックを終了する" ); articleMemento = articleMementoManager .getMemento(); article.undoFromMemento(articleMemento) のSystem.out。println( 「一時的なロールバック・エンド」); System.out.printlnは( "完全な情報注意:" + Articleこの記事ました); } }
クラスのノート:
パッケージcom.geely.design.pattern.behavioral.memento。 / ** *吉利によって作成 * / パブリック クラス条{ プライベート文字列のタイトル。 プライベート文字列の内容。 プライベート文字列のIMGS。 公共条(文字列のタイトル、文字列のコンテンツ、文字列IMGS){ この .TITLE = タイトル。 この .content = コンテンツ; この .imgs = IMGS。 } パブリック文字列あるgetTitle(){ 戻りタイトル。 } 公共 のボイドsetTitle(文字列のタイトル){ この .TITLE = タイトル。 } パブリック文字列のgetContent(){ 戻り含量; } 公共 ボイドのsetContent(文字列コンテンツ){ この .content = コンテンツ。 } パブリック文字列getImgs(){ 戻りIMGS。 } 公共 ボイドsetImgs(文字列IMGS){ この .imgs = IMGS。 } 公共ArticleMemento saveToMemento(){ ArticleMemento articleMemento =新しい ArticleMemento(この .TITLE、この .content、この.imgs)。 返すarticleMementoを。 } 公共 ボイドundoFromMemento(ArticleMemento articleMemento){ この .TITLE = articleMemento.getTitle()。 この .content = articleMemento.getContent()。 この .imgs = articleMemento.getImgs()。 } @Override 公共の文字列のtoString(){ リターン "の記事{" + "タイトル= '" +タイトル+ '\'' + "コンテンツ= '" +コンテンツ+ '\'' + "IMGS = '" + IMGS + '\'' + '}' 。 } }
スナップショットの種類に注意してください。
パッケージcom.geely.design.pattern.behavioral.memento。 / ** *吉利によって作成 * / パブリック クラスArticleMemento { プライベート文字列のタイトル。 プライベート文字列の内容。 プライベート文字列のIMGS。 公共ArticleMemento(文字列のタイトル、文字列コンテンツ、文字列IMGS){ この .TITLE = タイトル。 この .content = コンテンツ; この .imgs = IMGS。 } パブリック文字列あるgetTitle(){ 戻りタイトル。 } パブリック文字列のgetContent(){ 戻り含量; } パブリック文字列getImgs(){ 戻りIMGS。 } @Override パブリック文字列のtoString(){ リターン "ArticleMemento {" + "タイトル= '" +タイトル+ '\'' + "コンテンツ= '" +コンテンツ+ '\'' + 」、IMGS = '」+ IMGS + '\'」+ '}' ; } }
注意スナップショット管理:
パッケージcom.geely.design.pattern.behavioral.memento。 輸入java.util.Stackを; / ** *吉利によって作成 * / パブリック クラスArticleMementoManager { 民間最終スタック<ArticleMemento> ARTICLE_MEMENTO_STACK =新しいスタック<ArticleMemento>(); 公共ArticleMemento getMemento() { ArticleMemento articleMemento = ARTICLE_MEMENTO_STACK.pop()。 返すarticleMementoを。 } 公共 ボイドaddMemento(ArticleMemento articleMemento) { ARTICLE_MEMENTO_STACK.push(articleMemento)。 } }
ログを出力します:
タイトル:デザインパターンにコンテンツを潜ん:NotesコンテンツAの画像:ノートピクチャーA成功したステージング 完全な情報についての注意事項:Articleこの記事は、{タイトルだった = '潜んでいるデザインパターンA'、コンテンツ= 'NotesコンテンツA'、IMGS = 'イメージノート' } レビューノートスタート レビューノートエンド ノートに完全な情報を:Articleこの記事だった{タイトル =、コンテンツ= 'NotesコンテンツB'、IMGS = '注意画像B' 'デザインパターンB潜んでいる' } 一時的なフォールバックをスタート を押し出口スタック1 2を押して、スタック終了し 、ステージングバックオフエンド ノートに完全な情報を:記事はこの記事だった{タイトル =「デザインパターンに潜んでいる」、コンテンツ=「NotesコンテンツA」、IMGS =「注意画像A」} コードの終了プロセスで仕上げた 0