2011年9月21日

FLEX嵌入HTML時,HTML元素無法輸入中文問題

  • 解決方法:
  • 在指定為Application的mxml頁面,加入監聽MouseEvent.ROLL_OUT事件,相關程式碼如下:
    
    vc.addEventListener(MouseEvent.ROLL_OUT, setSRF); //vc請自行修改成合適的ID名稱
    
    private function setSRF(event:MouseEvent):void{ 
         if(Capabilities.hasIME){ 
              IME.enabled=true; 
              IME.conversionMode="CHINESE"; 
         } 
    }
    

參考來源:http://blog.csdn.net/vipliyaohua/article/details/6586457

2011年3月23日

Factory Method Pattern(工廠模式)

  • Definition
  • The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
    
  • Class Diagram
  • Product Role
  • public abstract class Product {
        public void method1() {
        //doSomething
        }
    
    public abstract void method2() ;
    
    }
    
  • ConcreteProduct Role
  • public ConcreteProduct1 extend Product {
        public void method2() {
            //doSomething
        }
    }
    
    public ConcreteProduct2 extend Product {
        public void method2() {
            //doSomething
        }
    }
    
  • Creator Role
  • public abstract class Creator {
        public abstract <T extends Product> T createProduct(Class<T> c);
    }
    
  • ConcreteCreator Role
  • public class ConcreteCreator extends Creator {
        public <T extends Product> T createProduct(Class<T> c) {
            Product product=null;
            try {
                product = (Product)Class.forName(c.getName()).newInstance();
            } catch (Exception e) {
                //error process
            }
            Return (T)product;
        }
    }
    
  • Client Role
  • public class Client {
        public static void main(String[] args) {
            Creator creator = new ConcreteCreator();
            Product product = creator.creatorProduct(ConcreteProduct1.class);
            //doSomething
        }
    }
    

2011年3月2日

Decorator Pattern(裝飾模式)

  • Definition
  • The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
    
  • Class Diagram
  • Component Role
  • public abstract class Component {
        public abstract void operate();
    }
    
  • ConcreteComponent Role
  • public class ConcreteComponent extends Component {
        @Override
        public void operate() {
            System.out.println("do Something");
        }
    }
    
  • Decorator Role
  • 
    public abstract class Decorator extends Component {
        private Component _component;
        
        public Decorator(Component component) {
            this._component = component;
        }
    
        @Override
        public void operate() {
            this._component.operate();
        }
    }
    
  • ConcreteDecorator Role
  • public class ConcreteDecorator1 extends Decorator {
    
        public ConcreteDecorator1(Component component)
            super(component);
        }
    
        private void method1() {
            System.out.println("method1 Decorator");
        }
    
        public void operate() {    
            this.method1();
            super.operate();
        }
    }
    
    public class ConcreteDecorator2 extends Decorator {
    
        public ConcreteDecorator2(Component component)
            super(component);
        }
    
        private void method2() {
            System.out.println("method2 Decorator");
        }
    
        public void operate() {    
            this.method2();
            super.operate();
        }
    }
    
  • Client Role
  • public class Client {
        public static void main(String[] args) {
            Component component = new ConcreteComponent();
            component = new ConcreteDecorator1(component);
             component = new ConcreteDecorator2(component);
            component.operate();
        }
    }
    

2011年2月28日

Observer Pattern(觀察者模式)

  • Definition
  • The Observer Pattern defines a one-to-many dependency between 
    objects so that when one object changes state, all of its 
    dependents are notified and updated automatically.
    
  • Class Diagram
  • Subject Role
  • public abstract class Subject {
        private Vector<Observer> obsVector = new Vector<Observer>();
        
        public void addObserver(Observer o) {
            this.obsVector.add(o);
        }
        
        public void delObserverr(Observer o) {
            this.obsVector.remove(o);
        }
    
        public void notifyObservers() {
            for (Observer o:this.obsVector) {
                o.update();
            }
        }
    }
    
  • ConcreteSubject Role
  • public class ConcreteSubject extends Subject {
        public void doSomething() {
             //do something
            notifyObserver();
        }
    }
    
  • Observer Role
  • public interface Observer {
        public void update();
    }
    
  • ConcreteObserver Role
  • public class ConcreteObserver implements Observer {
        pulic void update() {
            system.put.println("received notification, do somthing");
        }
    }
    
  • Client Role
  • public class Client {
        public static void main(String[] args) {
            ConcreteSubject subject = new ConcreteSubject();
            Observer obs = new ConcreteObserver();
            subject.addObserver(obs);
            subject.doSomething(); 
        }
    }
    

2011年2月27日

Strategy Pattern(策略模式)

  • Definition
  • The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
    
  • Class Diagram
  • Strategy Role
  • public interface Strategy {
        public void doSomething();
    }
    
  • ConcreteStrategy Role
  • public class ConcreteStrategy1 implements Strategy {
        public void doSomething(){
            System.out.println(“ConcreteStrategy1’s algorithm”);
        }
    }
    public class ConcreteStrategy2 implements Strategy {
        public void doSomething(){
            System.out.println(“ConcreteStrategy2’s algorithm”);
        }
    }
    
  • Context Role
  • public class Context {
        private Strategy _strategy;
        
        public Context(Strategy strategy){
            this._strategy= strategy;
        }
    
        public performDoSomething(){
            this._strategy.doSomething();
        }
    }
    
  • Client Role
  • public class Client {
        Strategy strategy = new ConcreteStrategy1();
        Context context = new Context(strategy);
        context.performDoSomething();
    }
    

2011年2月22日

[Python] Handle many levels of nested lists

list於Python中,是有序的物件集合,list內的元素若是list,則形成巢狀結構,若要對巢狀list內的每項元素作相同的處理或運算,則可以遞迴方式作回應。
  • Ex:顯示每項元素於螢幕上(程式碼)
  • def print_item(the_list):
        for each_item in the_list:
            if isinstance(each_item, list):
                print_item(each_item)
            else:
                print(each_item)
    
  • 執行結果顯示

2011年1月17日

Ubuntu 10.04 wubi無法啟動問題

在近一次的自動更新後,於開機選單就無法選擇Ubuntu啟動,錯誤訊息如下:
Try (hd0,0): NTFS5: No wubildr

error: unknown command 'loadfont'
error
求助Google大神得到的解決方式如下:
  • 以Ubuntu Live CD或Ubuntu Live USB開機。
  • 於Terminal輸入以下指令: 
    • 注意:sda1是指windows作業系統所在磁區。
sudo mkdir /media/win 
sudo mount /dev/sda1 /media/win
sudo mount -o loop /media/win/ubuntu/disks/root.disk /mnt
sudo cp /mnt/boot/grub/grub.cfg /mnt/boot/grub/grub.cfg.copy
sudo chmod +w /mnt/boot/grub/grub.cfg
gksu gedit /mnt/boot/grub/grub.cfg
  • 修改grub.cfg檔 
    • 筆者是將以下程式碼刪除,再重新開機,即可登入Ubuntu作業系統。
menuentry "Ubuntu, Linux 2.6.32-27-generic" {
    insmod ntfs
    set root='(hd0,2)'
    search --no-floppy --fs-uuid --set 74a62494a62458be
    loopback loop0 /ubuntu/disks/root.disk
    set root=(loop0)
    linux /boot/vmlinuz-2.6.32-27-generic root=/dev/sda2 loop=/ubuntu/disks/root.disk ro   quiet splash
    initrd /boot/initrd.img-2.6.32-27-generic
}
menuentry "Ubuntu, Linux 2.6.32-27-generic (recovery mode)" {
    insmod ntfs
    set root='(hd0,2)'
    search --no-floppy --fs-uuid --set 74a62494a62458be
    loopback loop0 /ubuntu/disks/root.disk
    set root=(loop0)
    linux /boot/vmlinuz-2.6.32-27-generic root=/dev/sda2 loop=/ubuntu/disks/root.disk ro single 
    initrd /boot/initrd.img-2.6.32-27-generic
}
menuentry "Ubuntu, Linux 2.6.32-26-generic" {
    insmod ntfs
    set root='(hd0,2)'
    search --no-floppy --fs-uuid --set 74a62494a62458be
    loopback loop0 /ubuntu/disks/root.disk
    set root=(loop0)
    linux /boot/vmlinuz-2.6.32-26-generic root=/dev/sda2 loop=/ubuntu/disks/root.disk ro   quiet splash
    initrd /boot/initrd.img-2.6.32-26-generic
}
menuentry "Ubuntu, Linux 2.6.32-26-generic (recovery mode)" {
    insmod ntfs
    set root='(hd0,2)'
    search --no-floppy --fs-uuid --set 74a62494a62458be
    loopback loop0 /ubuntu/disks/root.disk
    set root=(loop0)
    linux /boot/vmlinuz-2.6.32-26-generic root=/dev/sda2 loop=/ubuntu/disks/root.disk ro single 
    initrd /boot/initrd.img-2.6.32-26-generic
}

解決方案參考網址