设计模式模板2

门面模式:
将散的东西放在一起,单位之间没有固定联系。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

///


/// 门面模式
/// UI中用的较多,目的是把散的东西放在一起,形成接口使用
/// 这里but在添加监听时用的是new UnityAction,目的是不在栈上开辟空间
///

public class FacadeModle : MonoBehaviour {

public Transform redLight;
public Transform greenLight;

public Button but;
public bool isTrigger;

// Use this for initialization
void Start () {
    but = GetComponent

}


建造者模式:

模拟打造一个游戏:

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


/// 建造者模式
/// 基类建造子类,让子类去做事
///

public class Boss

{

ProductLeader[] leaders;

public void MakeMoney()
{

}

public void FindPerson()
{
    leaders = new ProductLeader[3];
}

public void MakeGame()
{
    MakeMoney();
    FindPerson();

    for (int i = 0; i < leaders.Length; i++)
    {
        leaders[i].MakeGame();
    }
}

}

public class ProductLeader
{
Worker[] workers;

public void FindPerson()
{
    workers = new Worker[3];
}

public void EqualRelation()
{

}

public void MakeGame()
{
    FindPerson();
    EqualRelation();
    for (int i = 0; i < workers.Length; i++)
    {
        workers[i].EnjoyWork();
    }

}

}

public class Worker
{

public void EnjoyWork()
{
    SolveTroubl();
    FindPerson();
    MakeGame();

}

public void FindPerson()
{

}

public void SolveTroubl()
{

}

public void MakeGame()
{

}

}

public class Bulder : MonoBehaviour {

void Start () {
    
}

void Update () {
    
}

}

中介者模式:

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

///


/// 中介者模式
/// 两个类之间避免相互引用,采用中介者模式
///

public class MiddleBase
{
public Transform ower;

public float blood = 0;

public virtual void ReducelBlood()
{

}

}

public class MiddlePlayer:MiddleBase
{

public override void ReducelBlood()
{
    //base.ReducelBlood();
    blood -= 10;
}

}

public class MiddleNPC : MiddleBase
{
public override void ReducelBlood()
{
//base.ReducelBlood();
blood -= 50;
}
}

public class MiddModle : MonoBehaviour {

public void CaculateAttack(MiddleBase attacker,MiddleBase attacked)
{
    if( Vector3.Distance(attacker.ower.position, attacked.ower.position) < 10)
    {
        attacked.ReducelBlood();
    }
}

MiddleBase player;

MiddleBase npc;


// Use this for initialization
void Start () {
    CaculateAttack(player, npc);

    CaculateAttack(npc, player);
}

// Update is called once per frame
void Update () {
    
}

}


组合模式:

你可能感兴趣的:(设计模式模板2)