Last active
June 11, 2016 07:13
-
-
Save ShirakawaYoshimaru/a169bd51fcad4095d16dd65bcf255a75 to your computer and use it in GitHub Desktop.
実装クラスが増えると対応した機能クラスも増やす必要がある
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System; | |
//使い方 | |
public class TestScript : MonoBehaviour | |
{ | |
void Start () | |
{ | |
new Human (new Speak ()).Hellow (); | |
new God (new Speak ()).Hellow (); | |
new God (new Speak ()).SuperHellow (); | |
new HumanNatural (new NaturalSpeak ()).Hellow (); | |
new GodNatural (new NaturalSpeak ()).Hellow (); | |
} | |
} | |
//機能クラス | |
public class Human | |
{ | |
protected Speak speak; | |
public Human (Speak speak) | |
{ | |
this.speak = speak; | |
} | |
public void Hellow () | |
{ | |
this.speak.SayHellow (); | |
} | |
} | |
//機能クラス | |
public class God : Human | |
{ | |
public God (Speak speak) : base (speak) | |
{ | |
} | |
public void SuperHellow () | |
{ | |
for (int i = 0; i < 10; i++) { | |
this.speak.SayHellow (); | |
} | |
} | |
} | |
//機能クラス | |
public class HumanNatural | |
{ | |
protected NaturalSpeak speak; | |
public HumanNatural (NaturalSpeak speak) | |
{ | |
this.speak = speak; | |
} | |
public void Hellow () | |
{ | |
this.speak.SayHellow (); | |
} | |
} | |
//機能クラス | |
public class GodNatural : Human | |
{ | |
public GodNatural (NaturalSpeak speak) : base (speak) | |
{ | |
} | |
public void SuperHellow () | |
{ | |
for (int i = 0; i < 10; i++) { | |
this.speak.SayHellow (); | |
} | |
} | |
} | |
//実装interface | |
public interface ISpeak | |
{ | |
void SayHellow (); | |
void SayGoodBay (); | |
} | |
//実装クラス | |
public class NaturalSpeak : ISpeak | |
{ | |
public void SayHellow () | |
{ | |
Debug.Log ("やぅっほぉー!"); | |
} | |
public void SayGoodBay () | |
{ | |
Debug.Log ("じゃあね〜〜〜〜〜!"); | |
} | |
} | |
//実装クラス | |
public class Speak : ISpeak | |
{ | |
public void SayHellow () | |
{ | |
Debug.Log ("やっほー"); | |
} | |
public void SayGoodBay () | |
{ | |
Debug.Log ("じゃあね"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment