工厂模式

工厂模式个人理解:工厂这里就比喻为一个大果园,只要是水果就实现这个大果园,然后在具体实现这个大果园的函数时正式分类是什么水果,然后售卖具体的水果

//果园里产水果
public interface Orchard 
{
    string DrinkFunction();
}

单利模式链接点击跳转

//Singleton使用的是单利
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;
    public static T Instance
    {
        get
        {
            return instance;
        }
    }
    protected virtual void Awake()
    {
        instance = this as T;
    }
}
//香蕉
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

class Banana : Singleton<Banana>, Orchard 
{
    protected override void Awake()
    {
        base.Awake();
    }
    public string DrinkFunction()
    {
        return "Banana";
    }
}
//苹果
//再添加新的水果品类也是如此
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

class Apple : Singleton<Apple>, Orchard 
{
    protected override void Awake()
    {
        base.Awake();
    }
    public string DrinkFunction()
    {
        return "Apple";
    }
}

//购买水果
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FruitStore : MonoBehaviour
{
    void Start()
    {
        print(Apple.Instance.DrinkFunction());
        print(Banana.Instance.DrinkFunction());
    }
}
上一篇:java 之 Collections集合工具类排序


下一篇:Comparable和Comporator