Created
June 10, 2016 17:25
-
-
Save ShirakawaYoshimaru/e7a707faa45c22568b1e22b163cee27c 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 System; | |
//使い方 | |
public class TestScript : MonoBehaviour | |
{ | |
void Start () | |
{ | |
var a = new GoodHellow1 ("いーちゃん", 20, "。戯言だけどね!"); | |
a.VeryGoodHellow (); | |
var b = new GoodHellow2 ("なると", 20, "ってばよ"); | |
b.VeryGoodHellow (); | |
} | |
} | |
//すでに用意されてある機能/ライブラリとか | |
public class Hellow | |
{ | |
private string name; | |
private int age; | |
public Hellow (string name, int age) | |
{ | |
this.name = name; | |
this.age = age; | |
} | |
public string SayHellow () | |
{ | |
return "こんにちは!" + GetName () + "です :)"; | |
} | |
public string GetName () | |
{ | |
return this.name; | |
} | |
public int GetAge () | |
{ | |
return age; | |
} | |
} | |
//欲しい機能のinterface | |
//めっちゃかっこよく挨拶したいよ・・・ | |
public interface HellowInterface | |
{ | |
void VeryGoodHellow (); | |
} | |
//Adapterパターン(継承) | |
public class GoodHellow1 : Hellow,HellowInterface | |
{ | |
private string keyword; | |
public GoodHellow1 (string name, int age, string keyword) : base (name, age) | |
{ | |
this.keyword = keyword; | |
} | |
public void VeryGoodHellow () | |
{ | |
Debug.Log ("やあ!諸君!僕の名前は" + GetName () + "です!年齢は" + GetAge () + GetKeyword ()); | |
} | |
public string GetKeyword () | |
{ | |
return this.keyword; | |
} | |
} | |
//Adapterパターン(移譲) | |
public class GoodHellow2 : HellowInterface | |
{ | |
private Hellow hellow; | |
private string keyword; | |
public GoodHellow2 (string name, int age, string keyword) | |
{ | |
hellow = new Hellow (name, age); | |
} | |
public void VeryGoodHellow () | |
{ | |
Debug.Log ("やあ!諸君!僕の名前は" + hellow.GetName () + "です!年齢は" + hellow.GetAge () + GetKeyword ()); | |
} | |
public string GetKeyword () | |
{ | |
return this.keyword; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment