Category Archives: Development

All posts related to software development.

Applying Command pattern in a demo of undoable actions.

In programming, we are usually need to queue requests, or decouple implementation of requests from its caller. And this is reason Command pattern was given birth. The Command pattern, basically, allows us encapsulate commands into objects. Thereby, it provides some useful options, such as:

  • Queue or dequeue requests
  • Parameterize client with different requests
  • Support redo/undo functionality

The implementation of Command pattern is quite simple with four major terms: Receiver, Invoker, Command, ConcreteCommand and Client. Let take a look at bellow diagram: Continue reading

Adapter pattern

Sometimes existing classes already provide functionalities required by client, but its interface is not what the client expects. So that we need a bridge to convert existing interface to what the client expects. And the bridge is called an Adapter. In real world, an adapter is very popular such as adapters for supplies, adapters for memory cards, so on. In software programming, the features of adapter are provided by Adapter pattern. We are going into its details in this article.

Adapter pattern helps us with two main jobs:

  • Convert the interface of a class into another interface what client expects.
  • Adapter lets classes work together, that couldn’t be able because of incompatible interfaces.

Continue reading

Decorator pattern

Sometimes we need to extend an object functionality, a normal way we can do it statically by inheritance mechanism. This is not wrong, but in some situations it makes source code more complex as well. That is reason Decorator pattern, a way of extending functionality dynamically, was born.

According to GoF, Decorator pattern is one of the widely used structure patterns. This pattern dynamically changes the functionality of an existed object at runtime without impacting on the existing functionality of the other objects. Continue reading

Install Python3, virtual environment on Mac OS X

This article will explain how to install Python3 and virtual environment with virtualenv on Mac OS X. Normally, your Mac installed Python 2.7 already, you could remove it or keep both of them.

Install Xcode

Before starting, make sure your Mac installed Xcode already. If not you can get it from Apple Store.

Next, we install Command line of Xcode through bellow command:

xcode-select –install

Continue reading

Understand IOC principle through Dependency Injection and Service Locator

Before to be continue, I suggest that you should know about OOP Design Principles. If you do not any more, please take a look at this link before keep on reading this articles.

One of the design principles is “Dependency Inversion Principle”, it says that:

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions.

Continue reading

URLConnection with Cookies

Updated on 25/11/2015: You should take a note about cookie with redirect-request. By default HttpURLConnection will take care about it automatically. You set your wide-system CookieManager, you pass a cookie to request, everything is OK.

But in case you have no default CookieManager and you do request without Cookie in header, you will get the problem. There’s no cookie was returned with response. This is because of HttpURLConnection set/get cookie for each request/response through the wide-system CookieManager instance. Let see bellow flow:

  1. Client sends a request without Cookie in header.
  2. Server returns a response which has code 301, new Localtion and Cookie inside header.
  3. HttpURLConnection looks at the header of response. It takes the Cookie then checks the global CookieManager instance. The instance if NULL then no cookie was saved.
  4. HttpURLConnection automatically make other request due to the Location. But because of no instance of CookieManager is available, then this request has no Cookie header too.
  5. Server receives the redirect request and sees no Cookie in header. It understands that no need to use Cookie mechanism. So it returns a request which has no Cookie.

To solve this problem, we can disable automatically process redirection by method setInstanceFollowRedirects(boolean) and do it by yourself. Notice that at step 3, you will extract Cookie header and set it to step 4 manually by setRequestProperty(String, String) and getHeaderField(String key).

As we know that Android 6.0 releases doesn’t support Apache HTTP client anymore. Instead of this, we will use the HttpURLConnection class. This API is more efficient, and in fact there are many interesting things to do with it. However today I only start with managing cookies. In that I will just focus on customize your own CookieStore. Continue reading