From 86fabf8a7b98c6131d22a670f00a9229931b83f7 Mon Sep 17 00:00:00 2001 From: PhucVR <59957741+nguyenphuc22@users.noreply.github.com> Date: Mon, 11 Mar 2024 23:17:03 +0700 Subject: [PATCH] Refactor code to implement Template Method Pattern --- .idea/workspace.xml | 40 +++++++-------- Writerside/topics/Strategy.md | 93 ++++++++++++++++++++--------------- 2 files changed, 72 insertions(+), 61 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2dde76b..35ec51b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,7 +6,7 @@ - + - { - "keyToString": { - "RunOnceActivity.OpenProjectViewOnStart": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "git-widget-placeholder": "main", - "kotlin-language-version-configured": "true", - "last_opened_file_path": "/Users/phucnguyen/Documents/GitHub/Design-Patterns", - "settings.editor.selected.configurable": "fileTemplates" + +}]]> diff --git a/Writerside/topics/Strategy.md b/Writerside/topics/Strategy.md index c09b426..bca9fe6 100644 --- a/Writerside/topics/Strategy.md +++ b/Writerside/topics/Strategy.md @@ -107,83 +107,94 @@ classDiagram Mục đích của Strategy Pattern là cho phép thuật toán thay đổi độc lập với các client sử dụng thuật toán đó. -## Cách triển khai Strategy Pattern +## Cách triển khai Template Method Pattern -Để triển khai Strategy Pattern, chúng ta sẽ cần các thành phần sau: +Để triển khai Template Method Pattern, chúng ta sẽ cần các thành phần sau: -### 1. Strategy Interface +### 1. Abstract Class -Đây là interface cho các chiến lược khác nhau trong context. Mỗi chiến lược sẽ cài đặt các hành động cụ thể. +Đây là lớp trừu tượng định nghĩa phương thức template. Phương thức này chứa một loạt các bước, một số trong đó có thể được triển khai ở lớp này hoặc bị hoãn lại để các lớp con triển khai. ```java -public interface Strategy { - void executeStrategy(); +public abstract class Game { + abstract void initialize(); + abstract void startPlay(); + abstract void endPlay(); + + // Template method + public final void play(){ + // Initialize the game + initialize(); + + // Start game + startPlay(); + + // End game + endPlay(); + } } ``` -### 2. Concrete Strategy Classes +### 2. Concrete Classes -Các lớp này cài đặt các hành động cụ thể cho một chiến lược cụ thể. +Các lớp này triển khai các phần của phương thức template cần được tùy chỉnh. ```java -public class ConcreteStrategyA implements Strategy { +public class Cricket extends Game { @Override - public void executeStrategy() { - System.out.println("Executing Strategy A"); + void initialize() { + System.out.println("Cricket Game Initialized! Start playing."); } -} -public class ConcreteStrategyB implements Strategy { @Override - public void executeStrategy() { - System.out.println("Executing Strategy B"); + void startPlay() { + System.out.println("Cricket Game Started. Enjoy the game!"); } -} -``` - -### 3. Context -Lớp này duy trì một tham chiếu đến một đối tượng Strategy và cho phép Client thay đổi strategy. - -```java -public class Context { - private Strategy strategy; + @Override + void endPlay() { + System.out.println("Cricket Game Finished!"); + } +} - public Context(Strategy strategy) { - this.strategy = strategy; +public class Football extends Game { + @Override + void initialize() { + System.out.println("Football Game Initialized! Start playing."); } - public void setStrategy(Strategy strategy) { - this.strategy = strategy; + @Override + void startPlay() { + System.out.println("Football Game Started. Enjoy the game!"); } - public void executeStrategy() { - strategy.executeStrategy(); + @Override + void endPlay() { + System.out.println("Football Game Finished!"); } } ``` -### 4. Sử dụng Pattern +### 3. Sử dụng Pattern -Đây là cách chúng ta có thể sử dụng Strategy Pattern trong một ứng dụng. +Đây là cách chúng ta có thể sử dụng Template Method Pattern trong một ứng dụng. ```java -public class StrategyPatternDemo { +public class TemplatePatternDemo { public static void main(String[] args) { - Context context = new Context(new ConcreteStrategyA()); - // The context is using ConcreteStrategyA. - context.executeStrategy(); // Executing Strategy A + Game game = new Cricket(); + game.play(); + System.out.println(); - // Change strategy to ConcreteStrategyB - context.setStrategy(new ConcreteStrategyB()); - - // Now the context is using ConcreteStrategyB. - context.executeStrategy(); // Executing Strategy B + game = new Football(); + game.play(); } } ``` +Trong ví dụ trên, `Cricket` và `Football` là các lớp con của `Game` và chúng triển khai theo phương thức template được định nghĩa trong lớp trừu tượng `Game`. Phương thức `play()` là phương thức template và nó gọi đến ba phương thức khác mà các lớp con phải cung cấp cài đặt. + ## Ví dụ Dưới đây là một ví dụ minh họa cho việc sử dụng Strategy Pattern trong Java, được cấu trúc theo yêu cầu của bạn: