实例demo理解面向接口思想

来源:互联网 发布:淘宝网休闲女装套装 编辑:程序博客网 时间:2024/06/03 23:02

浅显的理解面向接口编程

Android开发的语言是java,至少目前是,所以理解面向接口的思想是有必要的。下面通过一个简单的例子来理解。具体的概括我也不知道怎么说。

例子: 现在我们要开发一个应用,模拟移动存储设备的读写,即计算机与U盘、MP3、移动硬盘等设备进行数据交换。已知要实现U盘、MP3播放器、移动硬盘三种移动存储设备,要求计算机能同这三种设备进行数据交换,并且以后可能会有新的第三方的移动存储设备,所以计算机必须有扩展性,能与目前未知而以后可能会出现的存储设备进行数据交换。各个存储设备间读、写的实现方法不同,U盘和移动硬盘只有这两个方法,MP3Player还有一个PlayMusic方法。

PS:这个例子我是用Android的Demo来演示的。


思路:
1.电脑类要和外部设备交互,可以让外部设备都去实现一个接口,电脑类只需要调接口中的方法,接口中的方法被具体的外部设备类实现然后被调用。
2. 电脑类需要持有接口的引用,而且电脑类可以设置接口引用,因为可能会随时更换外部设备。

Computer类代码

import com.test.inter.IMobileStorage;public class Computer {    //持有一个接口引用    private IMobileStorage mIMobileStorage;    /*public Computer(IMobileStorage mIMobileStorage){        //给电脑制作一个外部接口,可以插MP3,usb,移动硬盘等等,只要是符合IMobileStorage规则的外部设备        this.mIMobileStorage = mIMobileStorage;    }*/    //设置电脑的外部接口,可以插MP3,usb,移动硬盘等等,只要是符合IMobileStorage规则的外部设备    public void setIMobileStorage(IMobileStorage mIMobileStorage){        this.mIMobileStorage = mIMobileStorage;    }    //电脑读的方法,其实就是调接口中读的方法    public void ComputerRead(){        mIMobileStorage.Read();    }    //电脑写的方法,其实就是调用接口中的写方法    public void ComputerWrite(){        mIMobileStorage.Write();    }}

IMobileStorage接口代码

/**所有移动设备的接口,如usb,移动硬盘,实现了此接口才能读写 * @author xh * */public interface IMobileStorage {    void Read();    void Write();}

Mp3Imp实现类代码

package com.test.Imp;import android.widget.Toast;import com.test.application.MyApplication;import com.test.inter.IMobileStorage;public class Mp3Imp implements IMobileStorage{    @Override    public void Read() {        Toast.makeText(MyApplication.getContext(), "MP3 read", 0).show();    }    @Override    public void Write() {        Toast.makeText(MyApplication.getContext(), "MP3 write", 0).show();    }}

Usb实现类代码

package com.test.Imp;import android.widget.Toast;import com.test.application.MyApplication;import com.test.inter.IMobileStorage;public class Mp3Imp implements IMobileStorage{    @Override    public void Read() {        Toast.makeText(MyApplication.getContext(), "MP3 read", 0).show();    }    @Override    public void Write() {        Toast.makeText(MyApplication.getContext(), "MP3 write", 0).show();    }}

SuperStorage类代码

package com.test.activity;import com.test.application.MyApplication;import android.widget.Toast;/**超级存储器,没有实现IMobileStorage接口,但如果电脑要用肿么办? * 用一个转换器 * @author xh * */public class SuperStorage {    public void read(){        Toast.makeText(MyApplication.getContext(), "超级读", 0).show();    }    public void write(){        Toast.makeText(MyApplication.getContext(), "超级写", 0).show();    }}

SuperStorageAdapter类代码

package com.test.adapter;import com.test.activity.SuperStorage;import com.test.inter.IMobileStorage;/**这个是转换器,实现了IMobileStorage接口 * @author xh * 作用:把没有实现IMobileStorage接口规则的外部设备转换,从而到达实现了IMobileStorage接口的目的 *  * 原理: * 1.电脑只能调用ComputerRead方法和ComputerWrite方法 * 2.ComputerRead方法和ComputerWrite方法是调用的是IMobileStorage接口中的方法 *  * 需求:现在的SuperStorage没有实现规定的接口,那么以前的原理就无法实现功能。 *  * 思路: *    1.转换器实现了接口,所以在设置外部设备的时候设置SuperStorageAdapter对象 *    2. 把SuperStorage对象设置进转换器,在转换器实现的接口方法中调用SuperStorageAdapter自己独特的方法。 *  */public class SuperStorageAdapter implements IMobileStorage {    //持有一个SuperStorage引用    private SuperStorage mSuperStorage;    //设置SuperStorage对象    public void setSuperStorage(SuperStorage mSuperStorage){        this.mSuperStorage = mSuperStorage;    }    /**Read()方法是实现了接口的方法     * read()方法是超级存储器自己独特的方法     *      */    @Override    public void Read() {        mSuperStorage.read();    }    @Override    public void Write() {        mSuperStorage.write();    }}

MainActivity中代码

package com.test.activity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.example.test2.R;import com.test.Imp.Mp3Imp;import com.test.Imp.UsbImp;import com.test.adapter.SuperStorageAdapter;public class MainActivity extends Activity{    private Button btnMp3Read;    private Button btnMp3Write;    private Button btnUsbRead;    private Button btnUsbWrite;    private Button btnSuperRead;    private Button btnSuperWrite;    private Computer mComputer;    private Mp3Imp mp3;    private UsbImp usb;    private SuperStorage superStorage;    private SuperStorageAdapter superStorageAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mp3 = new Mp3Imp();        usb = new UsbImp();        mComputer = new Computer();        //超级存储器        superStorage = new SuperStorage();        //超级存储器的转换器        superStorageAdapter = new SuperStorageAdapter();        //通过转换器转换超级存储器        superStorageAdapter.setSuperStorage(superStorage);        initView();    }    private void initView() {        btnMp3Read = (Button) this.findViewById(R.id.btn_mp3_read);        btnMp3Write = (Button) this.findViewById(R.id.btn_mp3_write);        btnUsbRead = (Button) this.findViewById(R.id.btn_usb_read);        btnUsbWrite = (Button) this.findViewById(R.id.btn_usb_write);        btnSuperRead = (Button) this.findViewById(R.id.btn_super_read);        btnSuperWrite = (Button) this.findViewById(R.id.btn_super_write);        btnMp3Read.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                //设置接口的对象                mComputer.setIMobileStorage(mp3);                mComputer.ComputerRead();            }        });        btnMp3Write.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                mComputer.setIMobileStorage(mp3);                mComputer.ComputerWrite();            }        });        btnUsbRead.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                mComputer.setIMobileStorage(usb);                mComputer.ComputerRead();            }        });        btnUsbWrite.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                mComputer.setIMobileStorage(usb);                mComputer.ComputerWrite();            }        });        btnSuperRead.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                mComputer.setIMobileStorage(superStorageAdapter);                mComputer.ComputerRead();            }        });        btnSuperWrite.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                mComputer.setIMobileStorage(superStorageAdapter);                mComputer.ComputerWrite();            }        });    }}

完整demo下载地址: http://download.csdn.net/detail/u013467495/8492331

0 0
原创粉丝点击