質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PowerShell

Windows PowerShellはコマンドラインインターフェースであり、システム管理を含むWindowsタスク自動化のためのスクリプト言語です。

Q&A

解決済

1回答

1021閲覧

デザインパターンのpowershellを実行したい

riho456

総合スコア22

PowerShell

Windows PowerShellはコマンドラインインターフェースであり、システム管理を含むWindowsタスク自動化のためのスクリプト言語です。

0グッド

0クリップ

投稿2023/04/26 08:41

実現したいこと

いつもお世話になっております。プログラム初心者です。
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/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2023/04/26 10:30 編集

クイズなら ChatGPT に聞いてみては?
guest

回答1

0

ベストアンサー

Interface がないので Abstract で。

powershell

1class AbstractStrategy { 2 [string] $strategy 3 4 AbstractStrategy ([string] $strategy) { 5 if ($this.GetType() -eq [AbstractStrategy]) { 6 throw("Invalid class.") 7 } 8 $this.strategy = $strategy 9 } 10} 11 12class ConcreteStrategy1 : AbstractStrategy { 13 ConcreteStrategy1 () : base("Strategy1") {} 14 15 [void] Execute() { 16 Write-Host "This is ConcreteStrategy1." 17 } 18} 19 20class ConcreteStrategy2 : AbstractStrategy { 21 ConcreteStrategy2 () : base("Strategy2") {} 22 23 [void] Execute() { 24 Write-Host "This is ConcreteStrategy2." 25 } 26} 27 28class Context { 29 [AbstractStrategy] $strategy; 30 Context([object]$params) { 31 try { 32 $this.strategy = $params.strategy; 33 } catch { 34 throw("Invalid class.") 35 } 36 37 if (-Not (Get-Member -inputobject $this.strategy -name "Execute" -Membertype Methods)) { 38 throw("Not found `Execute()' method.") 39 } 40 } 41 42 [void] Execute() { 43 $this.strategy.Execute(); 44 } 45} 46 47$strategies = @{ 48 Strategy1 = [ConcreteStrategy1]::new(); 49 Strategy2 = [ConcreteStrategy2]::new(); 50} 51 52$context = [Context]::new(@{ 53 strategy = $strategies["Strategy1"]; 54 args_1="1111"; 55 args_2="2222"; 56 args_3="3333"; 57}) 58 59$context.Execute();

投稿2023/04/26 12:22

編集2023/04/26 12:26
melian

総合スコア19703

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

melian

2023/04/26 12:51

ChatGPT の回答?は何だか C# のコードを繋ぎ合わせた様な感じですね。こちらの回答は Abstract Factory design pattern モドキです。(実際には Factory でも Abstract クラスにもなっていませんが)
riho456

2023/04/27 00:06

melianさん、ありがとうございます。実行出来ました。これを元に勉強させていただきます、感謝です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問