実現したいこと
いつもお世話になっております。プログラム初心者です。
chatGPTで作成したPowershell用ストラテジパターンを実行したいです。
恐らく以下のコードを実行するとThis is ConcreteStrategy1.が出力されるはずなのですが、実行するとエラーが出てしまいます。
色々調べたりしていますが、よくわからず出ているエラーについてのアドバイスなど頂けないでしょうか?何が原因で出ているか等。
発生している問題・エラーメッセージ
+ [void] Execute(); + ~~~~~~~ 式またはステートメントのトークン 'Execute' を使用できません + [void] Execute(); + ~ 式が '(' の後に必要です。 + IStrategy strategy; + ~ クラス メソッドのパラメーター一覧に '(' がありません。 + IStrategy strategy; + ~ function 宣言に function 本文が存在しません。 + "Strategy1" = new ConcreteStrategy1(); + ~ 式が '(' の後に必要です。 + "Strategy2" = new ConcreteStrategy2(); + ~ 式が '(' の後に必要です。 + class ConcreteStrategy1 : IStrategy { + ~~~~~~~~~ 型 [IStrategy] が見つかりません。 + class ConcreteStrategy2 : IStrategy { + ~~~~~~~~~ 型 [IStrategy] が見つかりません。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken
該当のソースコード
Powershell
1# The Strategy interface defines the behavior that all strategies must implement. 2interface IStrategy 3{ 4 # Executes the strategy. 5 [void] Execute(); 6} 7# The ConcreteStrategy1 class implements the Strategy interface. 8class ConcreteStrategy1 : IStrategy { 9 # Executes the strategy. 10 [void] Execute() { 11 Write-Host "This is ConcreteStrategy1." 12 } 13} 14# The ConcreteStrategy2 class implements the Strategy interface. 15class ConcreteStrategy2 : IStrategy { 16 # Executes the strategy. 17 [void] Execute() { 18 Write-Host "This is ConcreteStrategy2." 19 } 20} 21# The Context class stores a reference to a strategy object. 22class Context { 23 # The strategy object. 24 IStrategy strategy; 25 # Constructor. 26 Context([object]$args) { 27 # Get the strategy type from the JSON object. 28 $strategyType = $args.strategy_type; 29 # Get the strategy object from the dictionary. 30 $strategy = $strategies[$strategyType]; 31 # If the strategy object is not found, throw an exception. 32 if ($strategy -eq $null) { 33 throw "Invalid strategy type." 34 } 35 # Set the strategy object on the context. 36 $this.strategy = $strategy; 37 } 38 # Executes the strategy. 39 [void] Execute() { 40 $this.strategy.Execute(); 41 } 42} 43# The following code demonstrates how to use the Strategy design pattern. 44# Create a dictionary of strategies. 45$strategies = @{ 46 "Strategy1" = new ConcreteStrategy1(); 47 "Strategy2" = new ConcreteStrategy2(); 48}; 49# Create a Context object and pass a JSON object to the constructor. 50$context = new Context @{ 51 "strategy_type" = "Strategy1"; 52 "args_1" = "1111"; 53 "args_2" = "2222"; 54 "args_3" = "3333"; 55}; 56# Execute the strategy. 57$context.Execute();
試したこと
エラーメッセージの調査等
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
クイズなら ChatGPT に聞いてみては?

回答1件
あなたの回答
tips
プレビュー