C#—高级|设计模式(1)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#region old
public class Animal : MonoBehaviour {
public string name;
public Animal(string name) {
this.name = name;
}
public void Shout() {
if (name == "鸟") {
Debug.Log("吱吱吱");
}
else if (name=="猫") {
Debug.Log("喵喵喵");
}
}
}
#endregion
#region New
public class NewAnimal {
public string name;
public NewAnimal(string name) {
this.name = name;
}
public virtual void Shout() {}
}public class Cat : NewAnimal {
public Cat(string name) : base(name) { }
public override void Shout()
{
base.Shout();
Debug.Log("喵喵喵");
}
}public class Bird : NewAnimal
{
public Bird(string name) : base(name) { }
public override void Shout()
{
base.Shout();
Debug.Log("吱吱吱");
}
}public class Dog : NewAnimal
{
public Dog(string name) : base(name) { }
public override void Shout()
{
base.Shout();
Debug.Log("汪汪汪");
}
}#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SRPConsole : MonoBehaviour {
#region Old
// Use this for initialization
void OldStart () {
Animal bird=new Animal("鸟");
bird.Shout();//吱吱吱
Animal cat = new Animal("猫");
cat.Shout();
}
#endregion#region New
void Start()
{
Cat cat = new Cat("mimi");
cat.Shout();
Dog dog = new Dog("旺财");
dog.Shout();}
#endregion}
子类必须有自己的行为和特征
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#region Old
public class GameRole : MonoBehaviour {
public string name;
public GameRole(string name) {
this.name = name;
}
public void Talk(GameRole otherGameRole) {
Debug.Log(name+"正在和"+otherGameRole+"进行谈话。。。");
}
public virtual void Attack(GameRole otherGameRole) {
Debug.Log(name+"正在攻击"+otherGameRole);
}
}
public class Hero : GameRole
{
public Hero(string name) : base(name)
{
}
}public class NPC : GameRole
{
public NPC(string name) : base(name)
{
}
public override void Attack(GameRole otherGameRole) {
Debug.Log("当前角色不能攻击。。");
throw new System.Exception();
}
}
#endregion
#region New
public class NewGameRole {
public string name;
public NewGameRole(string name)
{
this.name = name;
}
public void Talk(NewGameRole otherGameRole)
{
Debug.Log(name + "正在和" + otherGameRole + "进行谈话。。。");
}
}
public class CanAttackGameRole : NewGameRole
{
public CanAttackGameRole(string name) : base(name)
{
}
public void Attack(NewGameRole otherGameRole)
{
Debug.Log(name + "正在攻击" + otherGameRole);
}
public NewGameRole otherGameRole;
public void BuyEquip() { }}
public class NewNPC : NewGameRole
{
public NewNPC(string name) : base(name)
{
}
}#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class LSPConsole : MonoBehaviour {
#region old
private void Awake()
{
Hero angel = new Hero("Angle");
NPC shopNPC = new NPC("ShopNPC");
//shopNPC.Attack(angel);
GameRoleAttack(shopNPC); //问此时会不会抛出异常?
//向上转型
//GameRole gameRole = shopNPC;
}
public void GameRoleAttack(GameRole gameRole) {
//调用的是父类
gameRole.Attack(new GameRole("机器人"));
}
#endregion
#region New
//private void Start()
//{
// NewNPC shopNPC = new NewNPC("ShopNPC");
// CanAttackGameRole hero = new CanAttackGameRole("德玛西亚");
// shopNPC.Talk(hero);
// hero.otherGameRole = shopNPC;
// hero.Attack(hero);
//}
#endregion
}
一个类只与尽量少的类有联系
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#region Old
//School依赖Class和Student
public class School{
List<Class> classes;
public string name;
public School(string name, List<Class> classes) {
this.name = name;
this.classes=classes;
}
public void Show() {
Debug.Log("我们学校是:"+name);
Debug.Log("我们的班级有:");
for (int i=0;i<classes.Count;i++) {
//获取当前班级
Class @class = classes[i];
Debug.Log(@class.name);
Debug.Log("该班学生有:");
for (int j = 0; j < @class.students.Count; j++) {
//获取当前学术
Student @student = classes[i].students[j];
Debug.Log(@student.studentName);
}
}
}
}public class Class {
public List<Student> students;
public string name;
public Class(string name, List<Student> students)
{
this.name = name;
this.students = new List<Student>();
}
public void Show()
{
//Debug.Log("我们班级是:" + name);
}
}public class Student {
public string studentName;
public Student(string name)
{
this.studentName = name;
}
public void Show()
{
//Debug.Log("我们是:" + studentName);
}
}
#endregion#region New
//NewSchool只依赖与Class
public class NewSchool {
List<NewClass> newClasses;
public string name;
public NewSchool(string name, List<NewClass> newClasses) {
this.name = name;
this.newClasses = newClasses;
}
public void Show() {
Debug.Log("我们学校是:" + name);
for (int i = 0; i < newClasses.Count; i++) {
//班级介绍
newClasses[i].Show();
}
}
}public class NewClass {
public string name;
List<NewStudent> newStudents;
public NewClass(string name, List<NewStudent> newStudents)
{
this.name = name;
this.newStudents=newStudents;
}
public void Show() {
Debug.Log("我们班级是:" + name);
for (int i=0;i<newStudents.Count;i++) {
newStudents[i].Show();
}
}
}
public class NewStudent {
public string name;
public string hobby;
public NewStudent(string name,string hobby)
{
this.name = name;
this.hobby = hobby;
}
public void Show() {
Debug.Log("我们是:" + name);
Debug.Log("我的爱好是:" + hobby);
}
}#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class LKPConsole : MonoBehaviour {
private void Start()
{
#region OldSchool school = new School("一高",
new System.Collections.Generic.List<Class>() {
new Class("三年一班",
new System.Collections.Generic.List<Student>() {
new Student("一班小王"), new Student("一班小李") }),
new Class("三年二班",
new System.Collections.Generic.List<Student>() {
new Student("二班小吴"), new Student("一班小可")})
}
);
school.Show();
}#endregion
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// 遵循依赖倒置原则,全部都依赖于抽象
/// </summary>
namespace DIP {
public abstract class PhoneUser {
public string name;
public PhoneUser(string name) {
this.name = name;
}
/// <summary>
/// 抽象方法
/// </summary>
public abstract void PlayPhone(Phone phone);
}public abstract class Phone {
public string name;
public string owner;
public Phone(string name,string owner)
{
this.name = name;
this.owner = owner;
}
public abstract void Call(Phone other);
public abstract void Message(Phone other);
public abstract void PlayGame();
public abstract void EnjoyMusic();
}public class Student : PhoneUser
{
public Student(string name) : base(name)
{
}public override void PlayPhone(Phone phone)
{
Debug.Log("学生"+name+"正在玩手机-->"+phone.name);
phone.PlayGame();
}
}public class Programmer : PhoneUser
{
public Programmer(string name) : base(name)
{
}public override void PlayPhone(Phone phone)
{
Debug.Log("程序员" + name + "正在玩手机-->" + phone.name);
phone.PlayGame();
}
}public class Iphone : Phone
{
public Iphone(string name, string owner) : base(name, owner)
{
}public override void Call(Phone other)
{
Debug.Log("正在使用iPhone打电话给"+other.owner);
}public override void EnjoyMusic()
{
Debug.Log("正在使用iPhone听音乐");
}public override void Message(Phone other)
{
Debug.Log("正在使用iPhone发短信给" + other.owner);
}public override void PlayGame()
{
Debug.Log("正在使用iPhone玩游戏");
}
}public class HuaWeiPhone : Phone
{
public HuaWeiPhone(string name, string owner) : base(name, owner)
{
}public override void Call(Phone other)
{
Debug.Log("正在使用HuaWeiPhone打电话给" + other.owner);
}public override void EnjoyMusic()
{
Debug.Log("正在使用HuaWeiPhone听音乐");
}public override void Message(Phone other)
{
Debug.Log("正在使用HuaWeiPhone发短信给" + other.owner);
}public override void PlayGame()
{
Debug.Log("正在使用HuaWeiPhone玩游戏");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DIP;
public class DIPConsole : MonoBehaviour {#region use DIP
private void Start()
{//手机用户
//PhoneUser phoneUser;
//Phone iphone;
//Phone miPhone;
//Phone huaweiPhone;
Phone myiPhone =new Iphone("xiaohong的iPhone","xiaohong");
Phone myHuaWeiPhone = new HuaWeiPhone("xiaomei的HuaWeiPhone", "xiaomei");
PhoneUser phoneUser = new DIP.Student("xiaoming");
UserPlayPhone(phoneUser,myiPhone);
}
#endregion#region DIP Extend Method
/// <summary>
/// 用户使用手机
/// </summary>
/// <param name="phoneUser"></param>
/// <param name="phone"></param>
public void UserPlayPhone(PhoneUser phoneUser,Phone phone) {
phoneUser.PlayPhone(phone);
}
#endregion
}
维护性好
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ISP {public interface IBaseMobilePhone {
void Call();
void Message();
}
public interface IMP3 {
void Music();
}
public interface IMP4{
void Movie();
}
public interface IGameMachine {
void Game();
}
public interface ICamera {
void Camera();
}
public interface INetwork {
void Support4G();
}public interface IMobileSmartPhone:IBaseMobilePhone,IMP3,IMP4,IGameMachine,ICamera,INetwork
{
}public class IMobileIPhone : IBaseMobilePhone, IMP3, IMP4, IGameMachine, ICamera
{
public void Call()
{
Debug.Log("Call");
}public void Camera()
{
Debug.Log("Camera");
}public void Game()
{
Debug.Log("Game");
}public void Message()
{
Debug.Log("Message");
}public void Movie()
{
Debug.Log("Movie");
}public void Music()
{
Debug.Log("Music");
}
}
public class MobileOldManPhone : IBaseMobilePhone
{
public void Call()
{
Debug.Log("Call");
}public void Message()
{
Debug.Log("Message");
}
}
public class HongMiPhone : IBaseMobilePhone, ICamera, IMP3
{
public void Call()
{
Debug.Log("Call");
}public void Camera()
{
Debug.Log("Camera");
}public void Message()
{
Debug.Log("Message");
}public void Music()
{
Debug.Log("Music");
}
}
public class MobileIPhone3G : IMobileIPhone {
public MobileIPhone3G() {
Game();
Music();
Camera();
}
}
public class MobileIPhoneX : IMobileIPhone, INetwork
{
public void Support4G()
{
throw new System.NotImplementedException();
}
public void SendMessageByWeChat(INetwork terminal) {}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ISP;public class ISPConsole : MonoBehaviour {
private void Start()
{
IMobileIPhone MobileIPhone = new MobileIPhoneX();
//传入实现接口的所有子类对象
ShowDatas(new List<string>());
ShowDatas(new string[] { "a", "b" });
}
public void ShowDatas(IList<string> datas) {}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class OCPConsole : MonoBehaviour {
public Button button;
private void Start()
{
//第一天
button.onClick.AddListener(ShowMe);
//第二天
button.onClick.RemoveAllListeners();}
public void ShowMe() {
Debug.Log("我是小明");
}
}