如何在扩展不同基类的两个类中减少代码重复?

时间:2013-06-12 11:23:14

标签: c# oop xamarin.android

我有一个必须扩展Activity的类,例如

BaseActivity : Activity { }

和另一个必须扩展ListActivity的类,例如

BaseListActivity : ListActivity {} // ListActivity in turn extends Activity

这两个类都有一些相同的方法,例如

OnCreate(Bundle bundle), OnStart(), OnStop()

两者中的实现完全相同,例如

BaseActivity : Activity {

    public bool isBound = false;
    public MyServiceBinder binder;
    MyServiceConnection _myServiceConnection;
    MyReceiver _myReceiver;
    internal Intent _myServiceIntent;   

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        _myServiceIntent = new Intent(this, typeof(MyServiceCls));
        _myReceiver = new MyReceiver();

        _myServiceConnection = LastNonConfigurationInstance as MyServiceConnection;

        if (_myServiceConnection != null)
            binder = _myServiceConnection.Binder;
    }

    protected override void OnStart()
    {
        base.OnStart();

        var intentFilter = new IntentFilter(MyServiceCls.MyUpdatedAction) { Priority = (int)IntentFilterPriority.HighPriority };
        RegisterReceiver(_myReceiver, intentFilter);

        _myServiceConnection = new MyServiceConnection(this);
        Application.Context.BindService(_myServiceIntent, _myServiceConnection, Bind.AutoCreate);

        StartService(_myServiceIntent);
    }

    protected override void OnStop()
    {
        base.OnStop();

        if (isBound)
        {
            Application.Context.UnbindService(_myServiceConnection);

            isBound = false;
        }

        StopService(_myServiceIntent);

        UnregisterReceiver(_myReceiver);
    }
}

BaseListActivity : ListActivity {

    public bool isBound = false;
    public MyServiceBinder binder;
    MyServiceConnection _myServiceConnection;
    MyReceiver _myReceiver;
    internal Intent _myServiceIntent;   

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        _myServiceIntent = new Intent(this, typeof(MyServiceCls));
        _myReceiver = new MyReceiver();

        _myServiceConnection = LastNonConfigurationInstance as MyServiceConnection;

        if (_myServiceConnection != null)
            binder = _myServiceConnection.Binder;
    }

    protected override void OnStart()
    {
        base.OnStart();

        var intentFilter = new IntentFilter(MyServiceCls.MyUpdatedAction) { Priority = (int)IntentFilterPriority.HighPriority };
        RegisterReceiver(_myReceiver, intentFilter);

        _myServiceConnection = new MyServiceConnection(this);
        Application.Context.BindService(_myServiceIntent, _myServiceConnection, Bind.AutoCreate);

        StartService(_myServiceIntent);
    }

    protected override void OnStop()
    {
        base.OnStop();

        if (isBound)
        {
            Application.Context.UnbindService(_myServiceConnection);

            isBound = false;
        }

        StopService(_myServiceIntent);

        UnregisterReceiver(_myReceiver);
    }
}

不同之处在于两者的使用。例如

MyActivity : BaseActivity 
{ /* this overrides the methods and does things differently */}

MyListActivity : BaseListActivity 
{ /* this overrides the methods and does things differently */}

最终我的问题是我不希望在BaseActivityBaseListActivity类中复制相同的代码。解决这个问题的最佳方法是什么,我仍然可以获得执行所需功能所需的覆盖,而不需要重复代码?

注意:有些人可能认为这是一个Android项目 - 它是。它是用Xamarin(又名Monodroid)构建的,这就是它在C#中的原因。

2 个答案:

答案 0 :(得分:0)

将公共方法转换为辅助类的静态方法,并提供公共代码所依赖的所有变量作为参数。

答案 1 :(得分:0)

将公共代码解压缩到自己的类中,并在基类覆盖中调用类的方法。你将无法摆脱这些。