R.Tanaka.Ichiro's Blog

主にC# な話題です

目次

Blog 利用状況

ニュース

とても似ているメソッドの共通化

今リアルで書いてある 4 つのメソッドは、とても似ている訳だ。
基本的に、先頭と末尾に同じ処理がダラダラとあって、その真ん中に各メソッド特有の処理が来る。

例えば、こんな感じ。(あんまり良いサンプルじゃないけど)


public Test1() {
  Console.WriteLine(this.A(true));
  Console.WriteLine(this.B(1, true));
  Console.WriteLine(this.C("R", 2, false));
  Console.ReadKey();
}

public int A(bool b) {
  Console.WriteLine("----------------------------------");
  Console.WriteLine(DateTime.Now.Ticks);
  Console.WriteLine("----------------------------------");
  Console.WriteLine("A : {0}", b);
  Console.WriteLine("--");
  return 1;
}

public string B(int i, bool b)  {
  Console.WriteLine("----------------------------------");
  Console.WriteLine(DateTime.Now.Ticks);
  Console.WriteLine("----------------------------------");
  Console.WriteLine("B:{0}, {1}", i, b);
  Console.WriteLine("--");
  return "R";

}

public bool C(string s, int i, bool b)  {
  Console.WriteLine("----------------------------------");
  Console.WriteLine(DateTime.Now.Ticks);
  Console.WriteLine("----------------------------------");
  Console.WriteLine("C:{0}, {1}, {2}", s, i, b);
  Console.WriteLine("--");
  return true;
}


引数の型や数、戻り値の型が同じならデリゲートで共通化できるのになぁ・・・と悩んでいたのですが、よくよく考えてみればデリゲートって外側の変数をそのまま使えちゃうんでしたっけ。
なんだ、簡単じゃん・・・orz

以下にように、

とても似ているメソッドの共通化

してみた。


public Test2() {
  Console.WriteLine(this.A(true));
  Console.WriteLine(this.B(1, true));
  Console.WriteLine(this.C("R", 2, false));
  Console.ReadKey();
}

public int A(bool b) {
  int r = 0;
  D(() => { Console.WriteLine("A : {0}", b); r = 1; });
  return r;
}

public string B(int i, bool b)  {
  string r = string.Empty;
  D(() => { Console.WriteLine("B:{0}, {1}", i, b); r = "R"; });
  return r;
}

public bool C(string s, int i, bool b)  {
  bool r = false;
  D(() => { Console.WriteLine("C:{0}, {1}, {2}", s, i, b); r = true; });
  return r;
}


public void D(Action a)  {
  Console.WriteLine("----------------------------------");
  Console.WriteLine(DateTime.Now.Ticks);
  Console.WriteLine("----------------------------------");
  a();
  Console.WriteLine("--");
}


なんか、気に入らないのでジェネリクスを使ってみた。


public Test3() {
  Console.WriteLine(this.A(true));
  Console.WriteLine(this.B(1, true));
  Console.WriteLine(this.C("R", 2, false));
  Console.ReadKey();
}

public int A(bool b) {
  return D(() => { Console.WriteLine("A : {0}", b); return 1; });
}

public string B(int i, bool b)  {
  return D(() => { Console.WriteLine("B:{0}, {1}", i, b); return "R"; });
}

public bool C(string s, int i, bool b)  {
  return D(() => { Console.WriteLine("C:{0}, {1}, {2}", s, i, b); return true; });
}

public T D<T>(Func<T> a)  {
  Console.WriteLine("----------------------------------");
  Console.WriteLine(DateTime.Now.Ticks);
  Console.WriteLine("----------------------------------");
  var r = a();
  Console.WriteLine("--");
  return r;
}


うーん、こんな感じで本番のコードも共通化できるかな。

投稿日時 : 2008年9月19日 14:30

Feedback

# re: とても似ているメソッドの共通化 2008/09/19 16:29 凪瀬

引数を何らかのクラスでラップして、ジェネリクスを適用してしまうんですよ。

public bool X(P param) {}

# re: とても似ているメソッドの共通化 2008/09/19 16:56 シャノン

ジェネリックは < > って書かないとダメっすよー?
HTML とジェネリックの相性の悪さは何とかなんないっすかね。

# re: とても似ているメソッドの共通化 2008/09/19 17:27 R・田中一郎

凪瀬 さん

>引数を何らかのクラスでラップして、ジェネリクスを適用してしまうんですよ

実は試してみたんですけど、引数のクラスを都度作るのが面倒くさくなって断念してしまいました。
匿名型をジェネリクスでうまく渡せれば最高なんだけどねー

・・・って、ここまで書いて何となくできそうな気がしてきた俺ガイルー
やってみよう。

--------------------------------
シャノン さん

>ジェネリックは < > って書かないとダメっすよー?

おー、ご指摘ありがとうございますー
書いて保存して、開いて一部修正すると消えちゃうんですよね。

結局、html で直書きするか、どこかにソースを書いてコピペしないとダメっぽい・・・

# 処理のクラス化 2008/09/23 23:09 C#と諸々

とても似ているメソッドの共通化ロギングのような別の関心事に関する共通処理ならばアスペクト指向という手もある。が、ここではそういうケ...

# Someone necessarily help to make severely posts I would state. This is the very first time I frequented your web page and to this point? I amazed with the analysis you made to make this particular put up amazing. Magnificent process! 2019/04/05 22:42 Someone necessarily help to make severely posts I

Someone necessarily help to make severely posts I would state.
This is the very first time I frequented your web page
and to this point? I amazed with the analysis you made to make this particular put up amazing.

Magnificent process!

# It's awesome to pay a visit this web site and reading the views of all friends regarding this article, while I am also keen of getting know-how. 2019/05/11 21:52 It's awesome to pay a visit this web site and read

It's awesome to pay a visit this web site and reading
the views of all friends regarding this article, while I am
also keen of getting know-how.

# If some one wants expert view on the topic of blogging afterward i recommend him/her to visit this blog, Keep up the fastidious job. 2019/06/12 10:35 If some one wants expert view on the topic of blog

If some one wants expert view on the topic of blogging afterward i recommend him/her to visit this blog, Keep up the fastidious job.

# I am regular reader, how are you everybody? This piece of writing posted at this website is truly pleasant. 2019/07/31 23:06 I am regular reader, how are you everybody? This p

I am regular reader, how are you everybody? This piece of writing posted at this website
is truly pleasant.

# I am regular reader, how are you everybody? This piece of writing posted at this website is truly pleasant. 2019/07/31 23:07 I am regular reader, how are you everybody? This p

I am regular reader, how are you everybody? This piece of writing posted at this website
is truly pleasant.

# I am regular reader, how are you everybody? This piece of writing posted at this website is truly pleasant. 2019/07/31 23:08 I am regular reader, how are you everybody? This p

I am regular reader, how are you everybody? This piece of writing posted at this website
is truly pleasant.

# I am regular reader, how are you everybody? This piece of writing posted at this website is truly pleasant. 2019/07/31 23:09 I am regular reader, how are you everybody? This p

I am regular reader, how are you everybody? This piece of writing posted at this website
is truly pleasant.

# Valuable information. Lucky me I discovered your web site accidentally, and I'm surprised why this twist of fate didn't happened earlier! I bookmarked it. 2019/08/15 5:39 Valuable information. Lucky me I discovered your w

Valuable information. Lucky me I discovered your web site accidentally, and I'm surprised why this twist of fate didn't happened earlier!
I bookmarked it.

# Valuable information. Lucky me I discovered your web site accidentally, and I'm surprised why this twist of fate didn't happened earlier! I bookmarked it. 2019/08/15 5:40 Valuable information. Lucky me I discovered your w

Valuable information. Lucky me I discovered your web site accidentally, and I'm surprised why this twist of fate didn't happened earlier!
I bookmarked it.

# Valuable information. Lucky me I discovered your web site accidentally, and I'm surprised why this twist of fate didn't happened earlier! I bookmarked it. 2019/08/15 5:41 Valuable information. Lucky me I discovered your w

Valuable information. Lucky me I discovered your web site accidentally, and I'm surprised why this twist of fate didn't happened earlier!
I bookmarked it.

# Valuable information. Lucky me I discovered your web site accidentally, and I'm surprised why this twist of fate didn't happened earlier! I bookmarked it. 2019/08/15 5:42 Valuable information. Lucky me I discovered your w

Valuable information. Lucky me I discovered your web site accidentally, and I'm surprised why this twist of fate didn't happened earlier!
I bookmarked it.

# DbHSYzQefnZHphRgA 2021/07/03 2:03 https://chttr.co/status/35d0056e-745c-4631-9d22-75

Im obliged for the article post. Fantastic.

# CwlKyYIHiOTKfAwqISJ 2021/07/03 3:33 https://amzn.to/365xyVY

Pink your weblog publish and beloved it. Have you ever thought about visitor publishing on other related weblogs similar to your website?

# help with essay writing m658xs 2022/09/03 14:59 Charlosmox


Really quite a lot of fantastic data! https://definitionessays.com/ thesis writing guidelines

# how to write biographical essay a236sy 2022/09/08 17:05 AaronBlact


Incredible all kinds of useful info. https://definitionessays.com/ write my assignment for me

# expert essay writers o370wa 2022/09/09 4:26 Charlosmox


Really tons of terrific information. https://definitionessays.com/ how to write a persuasive essay

# i need someone to write my dissertation f26irz 2023/02/27 2:57 Robertsaids


With thanks! Good stuff.
professional dissertation writing https://dissertationwritingtops.com/ assignment dissertation help

# writing college essays d68zzv 2023/03/06 6:06 EugeneSib


Nicely put, Many thanks.
writing a college essay about yourself https://domycollegehomeworkforme.com short college essays https://researchpaperwriterservices.com

# phd research thesis a58ncg 2023/03/06 15:01 Gregorysaipt


You've made the point.
how to write an effective college essay https://essaytyperhelp.com college essay writing prompts https://writeadissertation.com

# phd diss y71wnf 2023/03/07 12:07 EugeneSib


Wonderful data. Many thanks!
college essay guidelines https://writinganessaycollegeservice.com a thesis is____. https://helptowriteanessay.com

# write essay online c540mb 2023/03/07 13:35 Gregorysaipt


You have made your point very well.!
ucf college essay https://writeadissertation.com editing dissertations https://custompaperwritersservices.com

# how to write dialogue in an essay v608pv 2023/03/08 11:54 Gregorysaipt


You made your point very clearly!.
buy essay paper https://essayservicehelp.com criminology dissertation https://essaywritingservicehelp.com

# essays writing help z92rhc 2023/03/09 9:58 Gregorysaipt


Wow tons of excellent facts.
compare and contrast essay writing https://domycollegehomeworkforme.com writing a thesis proposal https://writingresearchtermpaperservice.com

# buy cheap essay online m204fx 2023/03/09 12:02 EugeneSib


Thanks. I like this.
how to write introduction for essay https://writeadissertation.com buy law essay https://researchproposalforphd.com

# writing a college application essay h33pbf 2023/03/10 16:56 EugeneSib


You said it adequately.!
dissertations writing https://studentessaywriting.com instant essay writer https://domycollegehomeworkforme.com

# best thesis writing service x85cbj 2023/03/11 5:06 Gregorysaipt


Helpful write ups. Appreciate it!
how to write an scholarship essay https://custompaperwritingservices.com how to write essay introduction https://payforanessaysonline.com

# personal essay writers g86xam 2023/03/11 8:01 EugeneSib


Useful advice. Appreciate it!
best resume writing services online https://service-essay.com how to write a dissertation https://englishessayhelp.com

# web copywriting services i593fg 2023/03/12 13:38 EugeneSib


Fantastic information, With thanks!
write essay https://writeadissertation.com how to write a portfolio essay https://helpwritingdissertation.com

# phd thesis writers e392az 2023/03/13 0:27 Gregorysaipt


Nicely put. Many thanks!
graduation thesis defense https://essaywritingservicehelp.com how to write an essay introduction paragraph https://writingresearchtermpaperservice.com

# affordable ghostwriters p64hzc 2023/03/13 22:14 Gregorysaipt


Superb facts. Many thanks!
write your essay for you https://essaywritinghelperonline.com academic dissertation https://customthesiswritingservice.com

# The plugins developed for WordPress 2023/05/09 23:16 Justas

The plugins developed for WordPress serve to enhance the features and functions of a WordPress website, allowing you to build your awesome and functional site https://t.me/wpigaming/648 Customise WordPress with powerful, professional and intuitive fields.

タイトル
名前
Url
コメント