小行星 jjing's
日历
网志分类
· 所有网志 (30)
· 面试题 (4)
· unix/linux (2)
· web services (4)
· c++ (2)
· Java (1)
· 位运算 (3)
· 有趣的算法 (10)
· 其他技术 (2)
· 未分类 (2)
最新的评论
站内搜索
友情链接
· 歪酷博客
· 我的歪酷 非非共享界
· kua
· angelboat
· jxtm.cublog.cn
· kaikai
· zzwu

订阅 RSS

0014146

歪酷博客

小行星 @ 2008-12-26 15:40

Constructor
Tips:
if super or this are called, they can only be called on the first line of the constructor
An Exception can be thrown if a parameter is invalid
You should ensure that the constructed object is in a valid state
Constructors should never call an overridable method (an overridable method is one which is neither private, static, nor final)
Constructors are never synchronized or static.
Constructors can be private, in order to restrict construction 

Private constructors
–classes containing only constants
–type safe enumerations
–singletons 
 
ŸStatic factory 
Static factory methods have names
Static factory methods can improve performance
Restrict the numbers of object creation
Interface-based return value

public class StaticFactory {
    private static final StaticFactory INSTANCE = new StaticFactory();
    private StaticFactory() {
    }
    public static StaticFactory getInstance(){
        return INSTANCE;
    }
}

Clone
Clone() is protected
Implement Cloneable interface
Overridden protected Object clone() throws CloneNotSupportedException
Invoke super.clone() in your clone() method
Catch possible exception

public class CloneExample implements Cloneable
{
   .....
   protected Object clone()
   {
       try{ 
        return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new Error(
                    "This should not occur since we implement Cloneable");
        }

   }

}

  
Use public clone() to override parent’s clone() 
No need to invoke constructor in clone method 
Inheritance hierarchy considerations 
Final variable doesn’t work for some clone()’s implementation 
Should not invoke any non-final method in clone() 
Final class

Copy Constructor



 
小行星 @ 2008-12-10 14:50

http://www.bitstrips.com/


 
小行星 @ 2008-08-26 21:41

Atom是一种表达有关作品发布的语义(如作者、摘要、类别)的XML词汇,atom发布协议(APP)则定义了一组能够反映发布过程(发布文章、编辑、设置类别、删除等)的资源。

APP一般用于Atom等在线发布系统,如博客、网页、内容管理系统等。有很多应用,如Bugzilla、Google Data APIs Protocol 等。
APP是符合REST风格的,在对资源的操作上,使用四种HTTP方法:
  • GET用于检索集合的表示或成员资源
  • POST用于创建新的成员资源
  • PUT用于更新成员资源
  • DELETE用于删除成员资源 
    Atom提供了一系列标签,提要(feed)是条目(entry)的列表。entry中包含了与资源相关的标签。客户端通过POST entry新建资源。       一个feed的例子:
    <feed xmlns="http://www.w3.org/2005/Atom"
          xml:lang="en"
          xml:base="http://www.example.org">
      <id>http://www.example.org/myfeed</id>
      <title>My Simple Feed</title>
      <updated>2005-07-15T12:00:00Z</updated>
      <link href="/blog" />
      <link rel="self" href="/myfeed" />
      <entry>
        <id>http://www.example.org/entries/1</id>
        <title>A simple blog entry</title>
        <link href="/blog/2005/07/1" />
        <updated>2005-07-15T12:00:00Z</updated>
        <summary>This is a simple blog entry</summary>
      </entry>
      <entry>
        <id>http://www.example.org/entries/2</id>
        <title />
        <link href="/blog/2005/07/2" />
        <updated>2005-07-15T12:00:00Z</updated>
        <summary>This is simple blog entry without a title</summary>
      </entry>
    </feed>

    服务文档响应请求,主要包含几种 元素:
  • Service - Workspace 的集合
  • Workspace - Collection 的集合 
  • Collection  -  一个Atom feed。
    一个例子:
    <?xml version="1.0" encoding="utf-8" ?>
    <service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom">
    - <workspace>
        <atom:title>Main Site</atom:title>
        - <collection href="http://localhost:8080/atompub/services/collections/main">
            <atom:title>My Main Page</atom:title>
            <accept>application/atom+xml;type=entry</accept>
            <categories fixed="yes" />
          </collection>
          <collection href="http://localhost:8080/atompub/services/collections/pics">
            <atom:title>My Pictures</atom:title>
            <accept>*/*</accept>
            <categories fixed="yes" />
          </collection>
      </workspace>
    - <workspace>
        <atom:title>Documents</atom:title>
        - <collection href="http://localhost:8080/atompub/services/collections/docs">
            <atom:title>My Documents</atom:title>
            <accept>application/atom+xml;type=entry</accept>
            <categories fixed="yes" />
          </collection>
      </workspace>
    </service>
    每个Atom Feed 是APP collection的表示,所谓表示,即资源当前情况的一个写照。


  •  
    小行星 @ 2008-08-19 14:24

    from:  http://www.elharo.com/blog/software-development/web-development/2005/12/08/post-vs-put/

    Musicians will understand this analogy. Have you ever tried to learn a piece that goes the wrong way? That is, you’re playing along and it’s so obvious where the next notes are going to go and instead the piece goes off in a completely different direction. Half the time you find yourself playing the notes you think the piece should use rather than the notes it does use.

    For me, understanding the difference between HTTP POST and PUT is very much like that. I’ve had a great deal of trouble understanding explicitly RESTful protocols like APP because they follow the actual definition of POST and PUT instead of what is to me clearly the right definition. However, I think I’m finally starting to get it.

    My mistake is in thinking that anything that creates a new page is a PUT and anything that changes an existing page is a POST. In SQL terms, POST is an UPDATE and PUT is an INSERT. However, that’s not the case. In fact, the mistake is in trying to model PUT and POST in terms of INSERT and UPDATE. They really aren’t even close.

    What actually happens is this. PUT puts a page at a specific URL. If there’s already a page there, it’s replaced in toto. If there’s no page there, a new one is created. This means it’s like a DELETE followed by an insert of a new record with the same primary key.

    POST, however, really has no equivalent in SQL. POST sends some data to a specified URL. The server on the other end of this URL can do whatever it wants with this data. It can store it somewhere private. (HTTP 204 NO CONTENT). It can store it in the page at the URL that was POSTed to (HTTP 205 RESET CONTENT). It can store it in a new page, in which case it returns the URL of that page in the Location field of the HTTP response header (HTTP 201 CREATED). It can use it as input for several different existing and new pages. It can throw the information away. It can insert, update, or delete records in a database (or all of the above). It can start brewing coffee (HTTP 202 ACCEPTED). It can start global thermonuclear war. POST is decidely non-side-effect free and non-idempotent.

    PUT is a much more limited operation that never does anything more than PUT one page at a specified URL. It is idempotent, which is a fancy way of saying that doing it twice is the same as doing it once. Both PUT and POST can be used to create new pages. However PUT should be used when the client specifies the location for the page. PUT is normally the right protocol for a web editor like DreamWeaver or BBEdit. POST is used when the client gives sends the page to the the server, and the server then tells the client where it put it. POST is normally the right protocol for a blog editor like TypePad or anything that inputs into a content management system. In SQL analogy, POST is an INSERT with an automatically generated primary key, and PUT is an INSERT that specifies the primary key in the INSERT statement.




     
    小行星 @ 2008-08-11 14:29

    环境rails 2.1.0 + ruby 1.8.6 + mysql
    需要的gem: acts_as_taggable, atom-tools, rest-open-uri
    ruby plugin: http_authentication
    rails 2.1中不能用ruby script/breakpointer 调试程序, gem安装ruby-debug,ruby在启动script/server时增加-u进入调试模式,需要断点的地方加入debugger.

    问题:
    1. 需要手工加入tags_bookmarks表:
        以下代码需要加入到001_initial_schema.rb
        create_table :tags_bookmarks, :force => true do |t|
          t.column :tag_id, :integer
          t.column :bookmark_id, :integer
        end

    2. ruby 1.8.6中Digest::MD5.new()和Digest::SHA1.new()是不带参数的,应该改成Digest::MD5.hexdigest和Digest::SHA1.hexdigest,可带参数

    3. 需要generate 一个Tag model

    4. bookmarks_controller中,find_by_user_id_and_uri_hash应为find_by_user_id_and_uri





     
    小行星 @ 2008-07-22 08:10

    用apt-get安装: apt-get install emacs
    问题:
     1.用apt-cache search emacs查找可用的emacs包
      2.用apt-get update更新解决无法找到resouces问题
      3.aptitude install libgtk2.0-dev 
          aptitude install emacs22 解决dependency问题
     4. 安装后出现"emacs: Cannot open termcap database file"
          在安装目录下cp ./etc/termcap.src  /etc/termcap即可。


     
    小行星 @ 2008-07-11 09:53

    来自:Linux联盟收集整理

    首先要弄清楚,在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),每个文件都有一个惟一的inode号。文件属性保存在索引结点里,在访问文件时,索引结点被复制到内存在,从而实现文件的快速访问。

    链接是一种在共享文件和访问它的用户的若干目录项之间建立联系的一种方法。Linux中包括两种链接:硬链接(Hard Link)和软链接(Soft Link),软链接又称为符号链接(Symbolic link)。

    一、硬链接

    硬链接说白了是一个指针,指向文件索引节点,系统并不为它重新分配inode。可以用:ln命令来建立硬链接。语法:

    ln [options] existingfile newfile
    ln[options] existingfile-list directory

    用 法: 第一种:为”existingfile”创建硬链接,文件名为”newfile”。第二种:在”directory”目录中,为” existingfile-list”中包含的所有文件创建一个同名的硬链接。常用可选[options] –f 无论”newfile”存在与否,都创建链接。-n 如果”newfile”已存在,就不创建链接。

    下面举一些例子:

    $ ls –il
    13058 -rwx - - - - - - 1 longcheng longcheng 48 8月 5 16:38 file1
    13059 -rwx - - - - - - 1 longcheng longcheng 57 8月 5 16:40 file2
    $ ln file2 file2hard
    $ ls –il
    13058 -rwx - - - - - - 1 longcheng longcheng 48 8月 5 16:38 file1
    13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2
    13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2hard

    注 意在创建链接前,file1 显示的链接数目为1,创建链接后(1)file1和file1hard的链接数目都变为2;(2) file1和file1hard在inode号是一样的(3) file1和file1hard显示的文件大小也是一样。可见进行了ln命令的操作结果:file1和file1hard是同一个文件的两个名字,它们具 有同样的索引节点号和文件属性,建立文件file1的硬链接,就是为file1的文件索引节点在当前目录上建立一个新指针。如下图,你可以删除其中任何一 个,如rm file2 ,每次只会删除一个指针,

    链接数同时减一,只有将所有指向文件内容的指针,也即链接数减为0时,内核才会把文件内容从磁盘上删除。当前目录逻辑结构:(不好意思图没有显示出来)。

    还可以在不同目录,但同一文件系统中建立文件的硬链接。设file1、file2在目录/home/longcheng/dir1中,下面的命令,在/home/longcheng中建立file2的硬链接。

    ln file2 /home/longcheng/file2hard

    下面的程序,是将dir1目录中所有文件,在目录dir2中建立硬链接

    $mkdir dir2
    $ln /home/longcheng/dir1/* /home/longcheng/dir2

    如果使用了 ln –f existingfile newfile,如果newfile已经存在,则无论原来newfile是什么文件,只用当前用户对它有写权限,newfile就成为exisitngfile的硬链接文件。

    尽 管硬链接节省空间,也是Linux系统整合文件系统的传统方式,但是存在一下不足之处:(1)不可以在不同文件系统的文件间建立链接(2)只有超级用户才 可以为目录创建硬链接。虽然很多树上说root用户可以创建,但是笔者在学习过程中发现即使是root用户也不能创建,我的系统是Redhat,内核 2.4、2.6都试过,在其他系统中不知道是不是可以。

    二、软链接(符号链接)

    软链接克服了硬链接的不足,没有任何文件系统的限制,任何用户可以创建指向目录的符号链接。因而现在更为广泛使用,它具有更大的灵活性,甚至可以跨越不同机器、不同网络对文件进行链接。

    建立软链接,只要在ln后面加上选项 –s,下面举个例子

    $ ls -il
    13058 -rwx - - - - - - 1 longcheng longcheng 48 8月 5 16:38 file1
    13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2
    13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2hard
    $ln –s file1 file1soft
    $ls -il
    13058 -rwx - - - - - - 1 longcheng longcheng 48 8月 5 16:38 file1
    13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2
    13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2hard
    13061 lrwxrwxrwx 1 longcheng longcheng 5 8月 5 16:58 file1soft->file1

    从 上面链接后的结果可以看出来软链接与硬链接,区别不仅仅是在概念上,在实现上也是不同的。区别:硬链接原文件&链接文件公用一个inode号,说明他们是 同一个文件,而软链接原文件&链接文件拥有不同的inode号,表明他们是两个不同的文件;在文件属性上软链接明确写出了是链接文件,而硬链接没有写出 来,因为在本质上硬链接文件和原文件是完全平等关系;链接数目是不一样的,软链接的链接数目不会增加;文件大小是不一样的,硬链接文件显示的大小是跟原文 件是一样的,这用强调,因为是等同的嘛,而这里软链接显示的大小与原文件就不同了,file1大小是48B,而file1soft是5B,这里面的5实际 上就是“file1”的大小。

    总之,建立软链接就是建立了一个新文件。当访问链接文件时,系统就会发现他是个链接文件,它读取链接文件找到真正要访问的文件。

    在不同系统之间建立软链接、对目录建立链接,这里就不举例了,读者可以自己去尝试,我也是在不断实践中学习的。

    当 然软链接也有硬链接没有的缺点,因为链接文件包含有原文件的路径信息,所以当原文件从一个目录下移到其他目录中,再访问链接文件,系统就找不到了,而硬链 接就没有这个缺陷,你想怎么移就怎么移;还有它要系统分配额外的空间用于建立新的索引节点和保存原文件的路径。补充一下:可以通过symlink来查看链 接文件,可以用 man symlink来学习。

    Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论


     
    小行星 @ 2008-06-13 13:54

    vector<string> split(const string& str)
    {
        typedef string::const_iterator iter;
        vector<string> ret;

        iter i = str.begin();
        while (i != str.end()) {

            // ignore leading blanks
            i = find_if(i, str.end(), not_space);

            // find end of next word
            iter j = find_if(i, str.end(), space);

            // copy the characters in [i, j)
            if (i != str.end())
            ret.push_back(string(i, j));
            i = j;
        }
        return ret;
    }


     
    小行星 @ 2007-10-19 09:43

    考官从办公室(面试现场)随意选取一个简单物品,假定是一个喝水的带广告图案的花纸杯,让应聘人对它设计出尽可能多的测试用例。

    测试项目:杯子

    需求测试:查看杯子使用说明书

    界面测试:查看杯子外观

    功能度:用水杯装水看漏不漏;水能不能被喝到

    安全性:杯子有没有毒或者细菌

    可靠性:杯子从不同高度落下的损坏程度

    可移植性:杯子在不同的地方、温度等环境下是否都可以正常使用

    兼容性:杯子是否能够容纳果汁、白水、酒精、汽油等

    易用性:杯子是否烫手、是否有防滑措施、是否方便饮用

    用户文档:使用手册是否对杯子的用法、限制、使用条件等有详细描述

    疲劳测试:将杯子盛上水(案例一)放24小时检查泄漏时间和情况;盛上汽油(案例二)放24小时检查泄漏时间和情况等

    压力测试:用根针并在上面不断加重量,看压强多大时会穿透

    跌落测试:杯子加包装(有填充物),在多高的情况下摔下不破损

    震动测试:杯子加包装(有填充物),六面震动,检查产品是否能应对恶劣的铁路\公路\航空运输

    测试数据:
    其中应用到:场景法、等价类划分法、因果图法、错误推测法、边界值法等方法

    期望输出:
    该期望输出需查阅国标、行标以及使用用户的需求

    说明书测试:检查说明书书写准确性

    另外,这个面试题目还可以推广到其它物品,比如手机、电饭煲、电梯等。


     
    小行星 @ 2007-06-12 22:47

    1.盲目搜索策略
        回溯
        图搜索
        深度优先
        宽度优先
        等代价搜索
    2.启发式搜索
        启发式搜索算法A
        爬山策略
        启发式搜索算法A*
    3.博弈树搜索
       Grundy博弈,完全取胜策略
       极小极大分析法
       a-b剪枝


     
    小行星 @ 2007-06-08 13:42

    通常n皇后问题使用回溯法,用固定排序搜索树解,搜索方向不具有启发性,回溯的次数比较多。
    而使用启发式方法,利用问题有关信息对规则进行动态排序,避免大量回溯。
    对于4皇后问题,可定义一对角线函数diag(i,j),用来计算经过棋盘上(i,j)单元的最长对角线长度,通过比较不同单元的diag函数值决定搜索的排序,每一行中diag(i,j)越小的单元排在前面。如四皇后可得这样的排序序列
    (12,13,11,14,21,24,22,23,31,34,32,33,42,43,41,44) ij表示第i行第j列
    搜索的过程如下。只需要回溯两次,而固定排序需要回溯22次
       12-->21(回)-->24-->31-->42(回)-->43            
                     


     
    小行星 @ 2007-06-06 12:56

    用算术或异或

    a = a + b;
    b = a - b;
    a = a - b;
    or
    a = a^b;//
    只能对int,char..
    b = a^b;
    a = a^b;
    or
    a ^= b ^= a;