小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

可以減少此代碼中使用的構(gòu)造函數(shù)的數(shù)量嗎?構(gòu)造函數(shù)重載和多個(gè)類的轉(zhuǎn)發(fā)

 印度阿三17 2019-10-28

我今天早些時(shí)候編寫了這段代碼.該代碼的目的是實(shí)現(xiàn)ICommand,使其僅一次僅運(yùn)行一個(gè)方法,而對(duì)其他方法靜默返回.

我編寫它的目的是為了與多平臺(tái)用戶界面一起使用,以便不記錄多次按鍵.

它有大量的構(gòu)造函數(shù),而且我不太了解這種重載.我將在下個(gè)學(xué)期學(xué)習(xí)設(shè)計(jì)模式,然后在本學(xué)期學(xué)習(xí)面向?qū)ο蟮木幊?因此希望在此之后我的代碼會(huì)更干凈!

我有辦法縮短代碼(減少所需的構(gòu)造函數(shù)的數(shù)量)嗎?

using System;
using Xamarin.Forms;
using System.Windows.Input;
using System.Threading.Tasks;

namespace dobjenkins
{
    public interface IAsyncCommand : ICommand
    {
        Task ExecuteAsync (object parameter);
    }

    public class AsyncCommand : IAsyncCommand
    {
        private readonly Func<object, Task> execute;
        private readonly Func<object, bool> canExecute;

        ///
        /// Constructors and initializors
        ///
        protected AsyncCommand ()
        {
        }

        public AsyncCommand (Func<object, Task> execute, Func<object, bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public AsyncCommand (Func<object, Task> execute)
        {
            this.execute = execute;
        }

        public AsyncCommand (Func<Task> execute, Func<bool> canExecute)
        {
            this.execute = _ => execute ();
            this.canExecute = _ => canExecute ();
        }

        public AsyncCommand (Func<Task> execute)
        {
            this.execute = _ => execute ();
        }

        ///
        ///  Execute Methods
        ///


        public async Task ExecuteAsync (object parameter)
        {
            await execute (parameter);
        }

        public async void Execute (object parameter)
        {
            await ExecuteAsync (parameter);
        }


        /// 
        /// CanExecute methods/Event
        ///

        public event EventHandler CanExecuteChanged;

        public void ChangeCanExecute ()
        {
            var ev = CanExecuteChanged;
            if (ev != null) {
                ev (this, EventArgs.Empty);
            }
        }

        public bool CanExecute (object parameter)
        {
            return canExecute == null || canExecute (parameter);
        }
    }

    public sealed class AsyncCommand<T> : AsyncCommand
    {
        private readonly Func<T, Task> execute;
        private readonly Func<T, bool> canExecute;

        public AsyncCommand (Func<T, Task> execute)
        {
            this.execute = execute;
        }

        public AsyncCommand (Func<T, Task> execute, Func<T, bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        ///
        ///  Execute Methods
        ///

        public async Task ExecuteAsync (T parameter)
        {
            await execute (parameter);
        }

        public async void Execute (T parameter)
        {
            await ExecuteAsync (parameter);
        }

        public bool CanExecute (T parameter)
        {
            return canExecute == null || canExecute (parameter);
        }
    }

    public class ExclusiveCommand : ICommand
    {
        protected ICommand Backing;
        protected static bool IsBusy = false;


        // 
        //  Constructors
        //
        #region Constructors
        public ExclusiveCommand()
        {
        }

        //
        // SYNC (normal) CONSTRUCTORS
        //

        public ExclusiveCommand (Action<object> execute, Func<object, bool> canExecute)
        {
            Backing = new Command (execute, canExecute);
        }

        public ExclusiveCommand (Action<object> execute)
        {
            Backing = new Command (execute);
        }

        public ExclusiveCommand (Action execute, Func<bool> canExecute)
        {
            Backing = new Command (execute, canExecute);
        }

        public ExclusiveCommand (Action execute)
        {
            Backing = new Command (execute);
        }

        //
        // ASYNC CONSTRUCTORS
        //


        public ExclusiveCommand (Func<object, Task> execute, Func<object, bool> canExecute)
        {
            Backing = new AsyncCommand (execute, canExecute);
        }

        public ExclusiveCommand (Func<object, Task> execute)
        {
            Backing = new AsyncCommand (execute);
        }

        public ExclusiveCommand (Func<Task> execute, Func<bool> canExecute)
        {
            Backing = new AsyncCommand (execute, canExecute);
        }

        public ExclusiveCommand (Func<Task> a)
        {
            Backing = new AsyncCommand (a);
        }

        // 
        //  End Constructors
        //
        #endregion Constructors

        // Execute

        public async void Execute (object parameter)
        {
            if (IsBusy) {
                return;
            }
            IsBusy = true;

            var async = Backing as AsyncCommand;
            if (async != null) {
                await async.ExecuteAsync (parameter);
            } else {
                Backing.Execute (parameter);
            }

            IsBusy = false;
        }

        //
        /// Can execute
        //

        public event EventHandler CanExecuteChanged;

        public void ChangeCanExecute ()
        {
            var ev = CanExecuteChanged;
            if (ev != null) {
                ev (this, EventArgs.Empty);
            }
        }

        public bool CanExecute (object parameter)
        {
            return Backing.CanExecute (parameter);
        }
    }
}

public sealed class ExclusiveCommand<T> : ExclusiveCommand
{
    /// 
    ///  Constructors
    ///
    #region Constructors
    public ExclusiveCommand()
    {
    }

    //
    // SYNC (normal) CONSTRUCTORS
    //

    public ExclusiveCommand (Action<T> execute, Func<T, bool> canExecute)
    {
        Backing = new Command<T> (execute, canExecute);
    }

    public ExclusiveCommand (Action<T> execute)
    {
        Backing = new Command<T> (execute);
    }

    //
    // ASYNC CONSTRUCTORS
    //


    public ExclusiveCommand (Func<T, Task> execute, Func<T, bool> canExecute)
    {
        Backing = new AsyncCommand<T> (execute, canExecute);
    }

    public ExclusiveCommand (Func<T, Task> execute)
    {
        Backing = new AsyncCommand<T> (execute);
    }

    //
    //  End Constructors
    //
    #endregion Constructors
}

解決方法:

您可以像這樣使用可選參數(shù):

public AsyncCommand (Func<object, Task> execute, Func<object, bool> canExecute = null)
{
    this.execute = execute;
    this.canExecute = canExecute;
}

這將允許您刪除AsyncCommand(Func< object,Task> execute)構(gòu)造函數(shù).

您也可以只將Func< object,Task>重載并刪除Func< Task>重載,并要求客戶編寫_ =>東西lambda.但這可能會(huì)接受,也可能無法接受,具體取決于您的要求.

來源:https://www./content-4-529251.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多