白话面向对象的六大原则

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/michael_yt/article/details/80801641

概念介绍

  1. 单一职责原则 (Single Responsibility Principle) : 一个类中应该是一组相关性很高的函数,数据的封装.即不能把两个完全不同的功能放在一个类中,但是这个类或者函数职责的划分就因人而异了.
  2. 开闭原则 (Open Close Principle) :软件中的对象(类,模块,函数等)应该对于扩展开放,对于修改封闭.即提倡我们应该通过 继承 或者 实现 接口的方式去扩展原有类的功能,尽量不直接去修改原有类的内容.
  3. 里氏替换原则 (Liskov Substitution Principle) : 所有引用了基类的地方都可以使用其子类.这个原则很好的体现的面向对象的继承和多态两大特点, 里氏和开闭原则一般都是同时出现.
  4. 依赖倒置原则 (Dependence Inversion Principle) :模块间的依赖通过抽象发生,实现类之间不发生直接的依赖关系,即依赖关系通过接口或者抽象类产生.
  5. 接口隔离原则 (Interface Segregation Principles) : 让客户端依赖的接口尽可能的小.
  6. 迪米特原则 (Law of Demeter) :最小知道原则,一个对象应该对其它对象有最少的了解.即只需要知道和它有直接关系的对象,其它的不用关心,这样耦合度就不大

类图关系

例子类图关系

代码实例

MainActivity

public class MainActivity extends Activity {

    private ImageView mImageView;
    private String mImageUrl = "http://img.my.csdn.net/uploads/201407/26/1406383291_8239.jpg";
    ImageLoader mImageLoader = new ImageLoader();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mImageView = (ImageView)findViewById(R.id.show_imageview);
        mImageLoader.setImageCache(new DoubleCache());
        mImageLoader.displayImage(mImageUrl, mImageView);
    }
}

ImageLoader

public class ImageLoader {
    ImageCache mImageCache = new MemoryCache();
    private static final int MSG_UPDATE_IMAGE = 2018;
    private ImageView mImageView;
    ExecutorService mExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime()
            .availableProcessors());


    public Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UPDATE_IMAGE:
                    if (mImageView != null) {
                        mImageView.setImageBitmap((Bitmap) msg.obj);
                    }
            }
        }
    };

    public void setImageCache(ImageCache cache) {
        mImageCache = cache;
    }

    public void displayImage(String imageUrl, ImageView imageView) {
        Bitmap bitmap = mImageCache.get(imageUrl);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
            return;
        }
        submitLoadRequest(imageUrl, imageView);
    }

    public void submitLoadRequest(final String imageUrl, final ImageView imageView) {
        imageView.setTag(imageUrl);
        mImageView = imageView;
        mExecutorService.submit(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = downloadImage(imageUrl);
                if (bitmap == null) {
                    return;
                }
                if (imageView.getTag().equals(imageUrl)) {
                    mHandler.removeMessages(MSG_UPDATE_IMAGE);
                    Message message = new Message();
                    message.what = MSG_UPDATE_IMAGE;
                    message.obj = bitmap;
                    mHandler.sendMessage(message);
                }
                mImageCache.put(imageUrl, bitmap);
            }
        });
    }

    public Bitmap downloadImage(String imageUrl) {
        Bitmap bitmap = null;
        try {
            URL url = new URL(imageUrl);
            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            bitmap = BitmapFactory.decodeStream(conn.getInputStream());
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }
}

ImageCache

public interface ImageCache {
    public Bitmap get(String url);
    public void put(String url, Bitmap bmp);
}

DoubleCache

public class DoubleCache implements ImageCache {

    ImageCache mMemoryCache = new MemoryCache();
    DiskCache mDiskCache = new DiskCache();

    @Override
    public Bitmap get(String url) {
        Bitmap bitmap = mMemoryCache.get(url);
        if (bitmap == null) {
            bitmap = mDiskCache.get(url);
        }
        return bitmap;
    }

    @Override
    public void put(String url, Bitmap bmp) {
        mMemoryCache.put(url, bmp);
        mDiskCache.put(url, bmp);
    }
}

DiskCache

public class DiskCache implements ImageCache {

    static String cacheDir = "sdcard/cache/";
    public String mImageName;

    @Override
    public Bitmap get(String url) {
        mImageName = getImageName(url);
        if (fileIsExists(cacheDir + mImageName)) {
            return BitmapFactory.decodeFile(cacheDir + mImageName);
        }
        return null;
    }

    @Override
    public void put(String url, Bitmap bmp) {
        FileOutputStream fileOutputStream = null;
        try {
            File file = new File(cacheDir + mImageName);
            fileOutputStream = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public boolean fileIsExists(String strFile) {
        try {
            File f=new File(strFile);
            if(!f.exists())
            {
                return false;
            }

        } catch (Exception e) {
            return false;
        }

        return true;
    }

    public String getImageName(String url) {
        Pattern p = Pattern.compile("[^/]*$");
        String [] str = p.split(url);
        Log.d("yigit","str[0] = "+str[0]);//前面部分
        // System.out.println(url.substring(str[0].length()));//后面部分
        String imageName = url.substring(str[0].length());//后面部分
        Log.d("yigit","imageName = "+imageName);
        return imageName;
    }
}

MemoryCache

public class MemoryCache implements ImageCache {

    private LruCache<String, Bitmap> mMemeryCache;

    public MemoryCache() {
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;
        mMemeryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight() / 1024; //Since API Level 1
//                return value.getByteCount() / 1024;  //Since API Level 12
            }
        };
    }

    @Override
    public Bitmap get(String url) {
        return mMemeryCache.get(url);
    }

    @Override
    public void put(String url, Bitmap bmp) {
        mMemeryCache.put(url, bmp);
    }
}

参考 Android源码设计模式解析与实践 – 何红辉 关爱名

猜你喜欢

转载自blog.csdn.net/michael_yt/article/details/80801641