@ranger-01
2018-05-14T10:06:32.000000Z
字数 4151
阅读 994
精通
# mother can only read book, 依赖的具体Bookclass Book{public String getContent(){return "很久很久以前有一个阿拉伯的故事……";}}class Mother{public void narrate(Book book){System.out.println("妈妈开始讲故事");System.out.println(book.getContent());}}public class Client{public static void main(String[] args){Mother mother = new Mother();mother.narrate(new Book());}}# 依赖接口,方便扩展interface IReader{public String getContent();}class Newspaper implements IReader {public String getContent(){return "林书豪17+9助尼克斯击败老鹰……";}}class Book implements IReader{public String getContent(){return "很久很久以前有一个阿拉伯的故事……";}}class Mother{# 依赖IReader接口,子类(Book,Newspaper可以替换IReader)public void narrate(IReader reader){System.out.println("妈妈开始讲故事");System.out.println(reader.getContent());}}public class Client{public static void main(String[] args){Mother mother = new Mother();mother.narrate(new Book());mother.narrate(new Newspaper());}}

import abcclass Leifeng:__metaclass__ = abc.ABCMeta@abc.abstractmethoddef wash(self):""""wash"""@abc.abstractmethoddef sweep(self):"""sweep"""@abc.abstractmethoddef buy_rice(self):"""buy rice"""class Undergraduate(Leifeng):def wash(self):print "undergraduate wash"def sweep(self):print "undergraduate sweep"def buy_rice(self):print "undergraduate buy rice"class Volunteer(Leifeng):def wash(self):print "volunteer wash"def sweep(self):print "volunteer sweep"def buy_rice(self):print "volunteer buy rice"class IFactory:__metaclass__ = abc.ABCMeta@abc.abstractmethoddef CreateLeifeng(self):"""create class leifeng"""class UndergraduateFactory(IFactory):def CreateLeifeng(self):return Undergraduate()class VolunteerFactory(IFactory):def CreateLeifeng(self):return Volunteer()if __name__ == "__main__":# create undergraduate to sweepi_factory = UndergraduateFactory()leifeng = i_factory.CreateLeifeng()leifeng.sweep()# create volunteer to washi_factory = VolunteerFactory() # just replace UndergraduateFactory with VolunteerFactoryleifeng = i_factory.CreateLeifeng()leifeng.wash()

import threadingclass Singleton(object):instance = Nonelock = threading.RLock()@classmethoddef __new__(cls):if cls.instance is None:cls.lock.acquire()if cls.instance is None:cls.instance = super(Singleton, cls).__new__(cls)cls.lock.release()return cls.instanceif __name__ == "__main__":instance1 = Singleton()instance2 = Singleton()print id(instance1) == id(instance2)

import abcclass Person:def show(self):print "person"class Finery(Person):def __init__(self, c):self.component = cdef show(self):"""finery show"""class Tie(Finery):def show(self):print "tie ",self.component.show()class Suit(Finery):def show(self):print "suit ",self.component.show()class Shoes(Finery):def show(self):print "shoes ",self.component.show()if __name__ == "__main__":person = Person()tie = Tie(person)suit = Suit(tie)shoes = Shoes(suit)shoes.show()

import abcclass Observer:__metaclass__ = abc.ABCMetadef __init__(self, n, nr):self.name = nself.notifier = nr@abc.abstractmethoddef update(self):"""updated by notifier"""class NbaObserver(Observer):def update(self):print self.name + ", " + self.notifier.state + ", close NBA"class StockObserver(Observer):def update(self):print self.name + ", " + self.notifier.state + ", close stock"class Notifier:def __init__(self):self.observers = []self.state = ""def attach(self, o):self.observers.append(o)def detach(self, o):self.observers.remove(o)def notify(self):for observer in self.observers:observer.update()class Boss(Notifier):passif __name__ == "__main__":boss = Boss()nba_observer = NbaObserver("Bob", boss)boss.attach(nba_observer)stock_observer = StockObserver("Alice", boss)boss.attach(stock_observer)boss.state = "boss's back himself"boss.notify()