Android Design Pattern - Build a SOLID Image Loader

Develop a image loader guided by design patterns

Guowei Lv

6 minute read

SOLID Principles and Design Patterns play an important role in Android development. Lets take a look at how to design and implement an image loader step by step. This example comes from the book Android Source Code Design Patterns - Analysis and Practice. I also rewrote all the code from Java to Kotlin and made some bug fixes. Step 1: Make it work Requirement: Our first version of image loader will just use in-memory cache to cache images loaded from the Internet, we will ignore cache validation for now.

Practical C - Stack

Implement Stack in C

Guowei Lv

5 minute read

In this blog post, let’s implement a generic stack in C. We will do this by several iterations, from simple version to the full blown version step by step. An Integer Version Let’s start with implementing an integer stack. #include <assert.h>#include <stdio.h>#include <stdlib.h> typedef struct { // int array to store elements of the stack int *elems; // the actual length of the stack int logicalLen; // the allocated length of the array int allocLen; } stack; // Initialize a stack void StackNew(stack *s); // Dispose a stack void StackDispose(stack *s); // Push an element onto the stack void StackPush(stack *s, int value); // Pop an element from the stack int StackPop(stack *s); void StackNew(stack *s) { // allocate 4 elements for the array s->elems = (int *)malloc(4 * sizeof(int)); s->allocLen = 4; s->logicalLen = 0; } void StackDispose(stack *s) { free(s->elems); } void StackPush(stack *s, int value) { if (s->logicalLen == s->allocLen) { // double the size of the array if there is no free space in it s->elems = (int *)realloc(s->elems, 2 * s->allocLen * sizeof(int)); s->allocLen *= 2; } s->elems[s->logicalLen] = value; s->logicalLen++; } int StackPop(stack *s) { assert(s->logicalLen !

Practical C - Linear Search

Implement linear search in C

Guowei Lv

3 minute read

I start this blog series to show some of the trickier parts of C programming. In the first post, let’s implement the linear search in C. Integer Version Let’s imagine that we search on an integer array. Two things to notice here: The function needs the array size n as a separate parameter. Only the array’s address is passed in the function. Generic Basic Version Let’s write a more generic version that does not specify type.

OOP considered harmful ?

A dose of anti oop

Guowei Lv

1 minute read

I have no intention to start flame war here. Just over the years of exploring, I have collected some materials that are not supporting the mainstream OOP paradigm. I just list them here, in no particular order. Stop Writing Classes This is a great talk, I think people will agree most of what’s in this video. The Third Million Line Problem This is slightly off topic, but interesing to watch.

Clear Azure CosmosDB Documents

Clear Azure CosmosDB documents using NodeJS

1 minute read

It suprised me that one cannot clear all documents in a CosmosDB collection from the web portal. The only solution for now is to use its SDK, so I wrote a simple Node script. It now can list all the documents and delete them, it can be easily modified to suit your need. var docdb = require("documentdb"); var async = require("async"); var config = { host: "https://xxxx.documents.azure.com:443/", auth: { masterKey: "xxxx" } }; var client = new docdb.