0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

インターフェースに静的メソッドを作りたくなったら

Last updated at Posted at 2022-02-15

インターフェース

ガチのinterfaceだと無理なのでclassをインターフェースとして作成する

public class ISomething {
	protected delegate void StaticFuncDelegate(string arg);
	protected static StaticFuncDelegate func;

	public static void StaticFunc(string arg) {
		func?.Invoke(arg);
	}
}

インターフェースを実装する

public class SomethingImpl : ISomething  {
	public SomethingImpl {
        Initialize();
    }
    public void Initialize() {
        func = StaticFuncImpl;
    }
    private static void StaticFuncImpl(string arg) {
        System.Diagnostics.Debug.WriteLine("called.");
    }
}

使う人

public class Caller{
    public void CallFunc() {
        ISomething.StaticFunc("なんか");
    }
}

何がやりたかったのか

各部品からメインループにメッセージをポストしたい。が
メインループと部品は切り離したかった。
部品側でインスタンス持ち回りはしたくない。
メッセージキューの実体はメインループ実装先に置きたい。

SendMessage(msg)の実装をメインループ実装先に置いて
部品からSendMessage(msg)とすれば
メインのメッセージキューに積まれるようになる。

0
1
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?