Reputation: 57
I wrote this code earlier today. The purpose of the code is to implement ICommand such that it exclusively runs only one method at a time, and returns silently for others.
I wrote it for use with a multi-platform user interface so that multiple key presses aren't registered.
It has a ton of constructors, and I don't understand overloading of this sort very well. I am taking design patterns next semester and Object Oriented Programming the semester after that, so hopefully my code will be cleaner after that!
Is there a way for me to shorten the code (reduce the number of constructors needed)?
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
}
Upvotes: 3
Views: 106
Reputation: 5415
You can use optional parameters like so:
public AsyncCommand (Func<object, Task> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
This will allow your to remove your AsyncCommand (Func<object, Task> execute)
constructor.
You could also just have your Func<object, Task>
overload and drop the Func<Task>
overload and require clients to write the _ => stuff
lambda. But that may or may not be acceptable depending on your requirements.
Upvotes: 4