Unity 工厂 模式

 泛型工厂:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
    public class Program: MonoBehaviour
    {
       void Start()
        {
            IAnimalFactory<Animal> factory = FactoryBuilder.Build("Dog");
            Animal a = factory.Create();

        }
    }
    public class Animal
    {
        public Animal()
        {
            Debug.Log("a");
        }
    }
    public class Dog : Animal
    {
        public Dog()
        {
            Debug.Log("Dog");
        }
        public void show()
        {
            Debug.Log("汪汪..");
        }
    }
    public class Cat : Animal
    {
        public Cat()
        {
            Debug.Log("Cat");
        }
    }
    public interface IAnimalFactory<TAnimal>
    {
        TAnimal Create();
    }

    public class AnimalIFactory<TAnimalBase, TAnimal> : IAnimalFactory<TAnimalBase> where TAnimal : TAnimalBase, new()
    {
        public TAnimalBase Create()
        {
            return new TAnimal();
        }
    }
    public class FactoryBuilder
    {
        public static IAnimalFactory<Animal> Build(string type)
        {
            if (type == "Dog")
            {
                return new AnimalIFactory<Animal, Dog>();
            }
            else if (type == "Cat")
            {
                return new AnimalIFactory<Animal, Cat>();
            }
            return null;
        }
    }

 

 

 

 

用于场景脚本的管理,可以通过public 把要管理的物品挂在在这里,然后通过 static  m_intstance.xxx 进行访问。

这个是最简单的单例,  但是在切换 场景的时候  , 对象 被摧毁 后 引用 报 空没有 对其 进行处理,这样的其实 坏处很大。

 

static 不好控制生命周期,用多容易出bug

https://www.cnblogs.com/yerongsc/p/4199666.html  :在最近unity3d的项目中,为了图方便使用了很多的静态变量,通常是单例类 (public static 类名 xxx )的使用,但是很少有在脚本使用结束后去将静态变量的值置为空,如果该脚本的成员变量中申请过内存空间 比如new byte[]或者存放了一些图片 Resources.load()等,那么即使该脚本对应的gameobject被删除了,这些成员变量中申请过的内存也不会被释放,另为Resource.load的图的内存还需要在调用Resources.UnloadUnusedAssets()后,才能释放。如果成员变量也为静态类型,仅仅将static的类名置空是无用的,还需要将对应的静态变量也置为空。

 

Unity3d中内存通常出现的ManagedStaticReferences就更静态变量的不规范使用有关系,以后得多注意了。

 

2 简单工厂

 

using UnityEngine;
using System;
using UnityEditor;


public class Food<T>
{
    public T type;

 

}
/// <summary>
/// 没有简单工厂之前
/// </summary>
public class Customer: MonoBehaviour
{
     
      /// <summary>
      /// 烧菜方法
      /// </summary>
      /// <param name="type"></param>
      /// <returns></returns>
    public static Food Cook(string type)
    {
        Food food = null;
        // 吃鸡蛋 自己烧

        if (type.Equals("炒鸡蛋"))
        {
            food = new ScrambledEggs();
        } 
         //又想吃土豆, 还是自己烧
        else if(type.Equals("炒土豆"))
        {
            food = new ScrambledTomato();
        }
        return food;

        //switch(type)
        //{
        //    case "炒鸡蛋":
        //        food = new ScrambledEggs();
        //        return food;
        //        break;
        //}    
    }

    void Awake()
    {
        //做鸡蛋
        Food food1 = Cook("炒鸡蛋");

        food1.Print();
        //做土豆
        Food food2 = Cook("炒土豆");
        food2.Print();
    }
    void Start()
    {
    }

     
     
        
    
}
/// <summary>
/// 菜抽象类
/// </summary>
public abstract class Food
{
    //输出点了什么菜
    public abstract void Print();
}

/// <summary>
/// 炒鸡蛋菜
/// </summary>
public class ScrambledEggs : Food
{
    public override void Print()
    {
        UnityEngine.Debug.Log("香喷喷的鸡蛋");
     }  
 }

/// <summary>
/// 炒土豆菜
/// </summary>
public class ScrambledTomato : Food
{
    public override void Print()
    {
        UnityEngine.Debug.Log("香喷喷的土豆");
    }
}

factory:

using UnityEngine;
using System;
using UnityEditor;


//public class Food<T>
//{
//    public T type;

 

//}
/// <summary>
/// 没有简单工厂之前
/// </summary>
#region  烧菜

//自己烧
//public class customer : MonoBehaviour
//{

//    /// <summary>
//    /// 烧菜方法 自己烧
//    /// </summary>
//    /// <param name="type"></param>
//    /// <returns></returns>
//    public static food cook(string type)
//    {
//        food food = null;
//        // 吃鸡蛋 自己烧

//        if (type.Equals("炒鸡蛋"))
//        {
//            food = new scrambledeggs();
//        }
//        //又想吃土豆, 还是自己烧
//        else if (type.Equals("炒土豆"))
//        {
//            food = new scrambledtomato();
//        }
//        return food;

//        //switch(type)
//        //{
//        //    case "炒鸡蛋":
//        //        food = new scrambledeggs();
//        //        return food;
//        //        break;
//        //}    
//    }

//    void awake()
//    {
//        //做鸡蛋
//        food food1 = cook("炒鸡蛋");

//        food1.print();
//        //做土豆
//        food food2 = cook("炒土豆");
//        food2.print();
//    }
//    void start()
//    {
//    }

//}

/// <summary>
/// 顾客类  向工厂点菜
/// </summary>
public class Custom : MonoBehaviour
{
    void Start()
    {
        food egg = FoodSimpleFactory.CreateFood("炒鸡蛋");
            egg.print();
        food tomoto = FoodSimpleFactory.CreateFood("炒土豆");
        tomoto.print();
    }
}
/// <summary>
/// 简单方法 负责烧菜,工厂烧
/// </summary>
public class FoodSimpleFactory
{
    /// <summary>
    /// 烧菜
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static food CreateFood(string type)
    {
        food food = null;
        if (type.Equals("炒鸡蛋"))
        {
            food = new scrambledeggs();
        }
        //又想吃土豆, 还是自己烧
        else if (type.Equals("炒土豆"))
        {
            food = new scrambledtomato();
        }
        return food;

    }
}

/// <summary>
/// 菜抽象类
/// </summary>
public abstract class food
{
    //输出点了什么菜
    public abstract void print();
}

/// <summary>
/// 炒鸡蛋菜
/// </summary>
public class scrambledeggs : food
{
    public override void print()
    {
        UnityEngine.Debug.Log("香喷喷的鸡蛋");
    }
}

/// <summary>
/// 炒土豆菜
/// </summary>
public class scrambledtomato : food
{
    public override void print()
    {
      UnityEngine.Debug.Log("香喷喷的土豆");
    }
}
/// <summary>
/// 简单工厂模式,顾客当客户端,负责点菜, 厨子(工厂) 负责烧菜 
/// </summary>
#endregion


//class Customer : MonoBehaviour
//{

//}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值