创建一个自定义跟踪()类在AS3 [英] Creating a custom trace() class in AS3

查看:152
本文介绍了创建一个自定义跟踪()类在AS3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个想法扩大我的跟踪()的消息。

I got this idea of expanding my trace() messages.

为什么

跟踪()是在我的code,我想将它们打开/关闭的一个简单的命令,也许增加某种优先功能跟踪(),即

trace() is all over my code, I want to turn them on/off by a simple command and maybe add some sort of priority functionality to the trace(), i.e.

myTrace.TraceMsg("loosehere",debugme, 0);
myTrace.TraceMsg("winhere",debugme, 1);

和当我运行在这种情况下,只有一个具有较高优先级,1,示出了

And when I run, only the one with the higher priority, "1" in this case, shows.

还有很多更多的功能,我想补充,以及像日志消息到文件等。

There is a lot more functionality I would like to add as well, like logging messages to file and so on.

问题

如何跟踪()的工作? -is有可能超载跟踪()不知何故? - 如何将我实现自定义TraceMsg(什么code吗?)的方法?

How do trace() work? -Is it possible to overload trace() somehow? -How would I implement the custom TraceMsg(what code here?) method?

有一些严重的问题,找到信息在我们最喜欢的搜索引擎这个问题上,所以任何帮助,将AP preciated。

Having some serious problems finding info on this subject on our favourite search engine, so any help would be appreciated.

推荐答案

跟踪()本身是一个顶级的功能,而不是一个阶级,所以很遗憾,我们不能对它进行扩展。话虽这么说,我们可以利用它在一个简单的类来做到这它做什么通常,只有在这种情况下,跟踪是基于条件(即布尔值 - 真|假,等)。首先,我们创建Trace类,这是因为我们通过下面的类,示踪利用工厂设计模式,我们不会实例化自己。跟踪器是围绕Singleton设计模式,但利用工厂模式来实例化跟踪,当跟踪器的跟踪方法被调用的实例。

trace() itself is a top-level function, not a class, so unfortunately we cannot extend it. That being said, we can utilize it in a simple class to do just what it does normally, only in this case the trace is based on conditions (i.e. Boolean - true|false, etc). First we create the Trace class, which we wouldn't instantiate ourselves because we are utilizing a Factory design pattern through the class below, Tracer. Tracer is built around the singleton design pattern, yet utilizes the Factory pattern to instantiate instances of Trace, when the trace method of Tracer is called.

//This class is handled by Tracer, which is right below it.
//You WILL NOT instantiate these, nor hold references.
package
{
    public class Trace
    {
        private function _value:*;
        private function _trace:Boolean;

        public function Trace(pValue:*, pTrace:Boolean):void
        {
          _value = pValue;
          _trace = pTrace;
        }
        public function get value():*
        {
           return _value;
        }
        public function get trace():Boolean
        {
           return _trace;
        }
    }
}
//This is the important class and the only one you will work with.
package
{
    /**
     *Utilizes Singleton and Factory design patterns.
     */
    public class Tracer
    {
        private var _traceArray:Array;
        private static var _instance:Tracer;

        public function Tracer(pvt:PrivateClass = null):void
        {
            if(pvt == null)
            {
                throw(new Error("You cannot instantiate this class directly, please use the static getInstance method."));
            }

            _init();
        }
        public static function getInstance():Tracer
        {
            if(Tracer._instance == null)
            {
                Tracer._instance = new Tracer(new PrivateClass());
            }
            return Tracer._instance;
        }
        public function trace(pValue:*, pTrace:Boolean):void
        {
           var trace:Trace = new Trace(pValue, pTrace);
           if(trace.pTrace)
           {
               trace(pValue);
           }
        }
        //Since we have the option for individual traces to be disabled
        //I provide this to get access to any and all later.
        public function traceAll():void
        {
            traceStr:String = _traceArray.toString();
        }
        public function get traceables():Array
        {
            return _traceArray;
        }
        //Here we provide a method to trace all, even if set to false in their constructor.
        private function _init():void
        {
            _traceArray = new Array();
        }
    }
}
//Here we create a class that is OUTSIDE of the package.
//It can only be accessed from within this class file.  We use this
//to make sure this class isn't instantiated directly.
class PrivateClass
{
    function PrivateClass():void
    {
        trace('can only be accessed from within this class file');
    }
}

//Now for use in doc class
package
{
    import flash.display.Sprite;
    import flash.events.Event;

    //No need to import Tracer and Trace, they are also in the
    //unnamed package.

    public class DocumentClass extends Sprite
    {
        private var _tracer:Tracer;

        public function DocumentClass():void
        {
            if(stage) _init();
            else addEventListener(Event.ADDED_TO_STAGE, _init);
        }
        private function _init(e:Event = null):void
        {
            _tracer = Tracer.getInstance();
            _tracer.trace(10*20, false);
            _tracer.trace(10*20, 0); //SAME AS ABOVE
            _tracer.trace("I love AS3", true); //traces
            _tracer.traceAll(); //Would trace: 200, 200, I love AS3
        }
    }
}

请记住,这是关闭的髋关节和很可能有错误或两个,但这个想法是存在的;也就是说,这是没有经过测试,它只是给你一个如何实现这一点的想法。

Keep in mind this is off the hip and very well could have a bug or two, but the idea is there; That is to say that this is not tested, it is merely to give you an idea of how you might implement this.

我希望这有助于。

布赖恩·霍奇
blog.hodgedev.com

这篇关于创建一个自定义跟踪()类在AS3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆