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)
    
  • 執行結果顯示