设计模式之中介者模式

中介者模式 双向的
中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。中介者模式属于行为型模式。

中介者模式在这里用媒婆表示

代码展示

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 中介者模式测试
/// </summary>
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);

    }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值