外觀模式,中介者模式

在這裏插入圖片描述
外觀模式:單向

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FacadeTest : MonoBehaviour {

	// Use this for initialization
	void Start () {
        HeadMaster headMaster = new HeadMaster();
        headMaster.OrderTeacherDoSummary();
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
/// <summary>
/// 校長類
/// </summary>
public class HeadMaster
{
    private Teacher teacher;

    public HeadMaster()
    {
        teacher = new Teacher();
    }
    public void OrderTeacherDoSummary()
    {
        teacher.SubmitStudentInfomation();
    }
}
/// <summary>
/// 老師類
/// </summary>
public class Teacher
{
    private Monitor monitor;
    private LeagueSecretary leagueSecretary;

    public Teacher()
    {
        monitor = new Monitor();
        leagueSecretary = new LeagueSecretary();
    }

    public void SubmitStudentInfomation()
    {
        monitor.CollectStudentRuleInfomation();
        leagueSecretary.CollectStudentStudyInfomation();
    }

}
/// <summary>
/// 班長類
/// </summary>
public class Monitor
{
    public void CollectStudentRuleInfomation()
    {
        Debug.Log("班長統計班裏紀律信息:整理材料");
    }
}
/// <summary>
/// 團支書類
/// </summary>
public class LeagueSecretary
{
    public void CollectStudentStudyInfomation()
    {
        Debug.Log("團支書統計班裏學習信息:整理材料");
    }
}

中介者模式:雙向

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MediatorTest : MonoBehaviour {

	// Use this for initialization
	void Start () {
        Matcher liGouDan = new Man(52,10000000,20000,0);
        Matcher cuiHua = new Women(20,3000,3000000,0);

        WomenMatchMaker womenMatchMaker = new WomenMatchMaker(liGouDan ,cuiHua);

        womenMatchMaker.OfferManInformation();
        womenMatchMaker.OfferWoManInformation();
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

public abstract class Matcher
{
    public int mAge;//年齡
    public int mMoney;//財富值
    public int mFamilyBG;//家庭背景

    public int mFavor;//好感度

    public Matcher(int age,int money,int familuBG,int favor)
    {
        mAge = age;
        mMoney = money;
        mFamilyBG = familuBG;
        mFavor = favor;
    }
}
/// <summary>
/// 男性
/// </summary>
public class Man : Matcher
{
    public Man(int age, int money, int familuBG, int favor) : base(age,money,familuBG,favor)
    {

    }
}
/// <summary>
/// 女性
/// </summary>
public class Women : Matcher
{
    public Women(int age, int money, int familuBG, int favor) : base(age, money, familuBG, favor)
    {

    }
}
/// <summary>
/// 媒婆
/// </summary>
public class WomenMatchMaker
{
    private Matcher mMan;
    private Matcher mWomen;

    public WomenMatchMaker(Matcher man,Matcher women)
    {
        mMan = man;
        mWomen = women;
    }
    /// <summary>
    /// 男方信息
    /// </summary>
    public void OfferManInformation()
    {
        mWomen.mFavor = -mWomen.mAge * 200 + mWomen.mMoney + mWomen.mFamilyBG;
        Debug.Log("把男生信息提供給女孩之後,女孩的好感度:" + mWomen.mFavor);
    }
    /// <summary>
    /// 男方信息
    /// </summary>
    public void OfferWoManInformation()
    {
        mMan.mFavor = mMan.mAge + mMan.mMoney + mMan.mFamilyBG;
        Debug.Log("把女孩信息提供給男生之後,男生的好感度:" + mMan.mFavor);

    }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章