AlphaWang.com

Alpha's Programming Notes | 一个程序员的日常

How to Implement a HashMap

作为一个Java开发民工,对HashMap可以说是经常使用的,不过要是想更好地使用它,还是需要了解其内部实现原理的。当了解了实现原理后,自己实现一个HashMap也就不难了。

How to Pass POJO Array to Spring MVC Controller

问题

最近遇到个需求,需要将一个对象数组从前端用ajax传递给后端的Spring MVC Controller,例如传递如下数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  products: [
    {
      category: 100,
      name: 'iPad',
      attributes: {
        volume: '32G',
        color: 'white'
      }
    },
    {
      category: 200,
      name: 'xx t-shirt',
      attributes: {
        size: 'M',
        color: 'red'
      }
    }
    
  ],
  locale: 'zh_CN'
}  

Integrate Octopress With Flickr

Octopress Blog有一个很酷的插件,可以将Flickr中的某个图片插入到你的博文中。像这样:
{\% flickr_image 15313566521 o \%}

还可以把Flickr某个相册中的所有图片都插入进来,像这样

Utility Classes Are Evil

This post is a summary of this artical and this one.

What’s Utility Classes

A utility class is a class filled with static methods. It is usually used to isolate a “useful” algorithm.

StringUtils, IOUtils, FileUtils from Apache Commons; Iterables and Iterators from Guava, and Files from JDK7 are perfect examples of utility classes.

Why Utility Classes

If you have two classes A and B, and have a method f() that both must use, then the most naive approach is to repeat the function as a method in both classes. However, this violates the Don’t repeat yourself (DRY) approach to programming.

The most natural solution is inheritance, but it’s not always beneficial for A and B to be subclasses of some parent class. In my case, A was already a subclass of another class, while B was not. There was no way to make A and B subclasses of a parent class without breaking that other relationship.

The alternative is to define the utility class: a public static class that sits in the global namespace, awaiting anyone to “borrow” them.

They are not bad in themselves, but they do imply relationships between your data that are not explicitly defined. Also, if your static class has any static variables, then A and B never really know what they’re getting into when they use it.

Make Mac Terminal Console / iTerm Colorful

Neither Terminal or iTerm on Mac is white/black color by default, which looks tedious. What if different entries shown in different colors?
Fortunately, we can do this just by writing a configuration file, then the terminal console or iTerm can look like this:

iterm icon

Calendar Tools on Mac

Mac菜单栏原生的日期时间显示比较简单,例如不能直接查看月历,比较不爽。
找到了几个比较优秀的Mac日期工具,可以替代原生功能:

Day-O

Day-O的界面风格与原生系统一样:

 dayo post title bug

[六大设计原则] 6. Open Closed Principle

定义

Open Closed Principle:

  • Software entities like classes, modules and functions should be open for extension but closed for modifications.

    对扩展开放,对修改关闭。
    一个软件实体应该通过扩展来实现变化,而不是通过修改已有的代码来实现变化。——but,并不意味着不做任何修改;底层模块的扩展,必然要有高层模块进行耦合

“变化”可分为三种类型:

  • 逻辑变化——不涉及其他模块,可修改原有类中的方法;
  • 子模块变化——会对其他模块产生影响,通过扩展来完成变化;
  • 可见视图变化——界面变化,有时需要通过扩展来完成变化。

问题由来

在软件的生命周期内,因为变化、升级和维护等原因需要对软件原有代码进行修改时,可能会给旧代码中引入错误,也可能会使我们不得不对整个功能进行重构,并且需要原有代码经过重新测试。

[六大设计原则] 5. Least Knowledge Principle

定义

最少知识原则(Least Knowledge Principle),又称迪米特法则(LoD,Law of Demeter),是指一个对象应该对其他对象有最少的了解。 一个类对自己依赖的类知道的越少越好。
也就是说,对于被依赖的类来说,无论逻辑多么复杂,都尽量地的将逻辑封装在类的内部,对外除了提供的public方法,不对外泄漏任何信息。

问题由来

类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大。