@ranger-01
2018-05-14T18:06:32.000000Z
字数 4151
阅读 884
精通
# mother can only read book, 依赖的具体Book
class 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 abc
class Leifeng:
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def wash(self):
""""wash"""
@abc.abstractmethod
def sweep(self):
"""sweep"""
@abc.abstractmethod
def 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.abstractmethod
def 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 sweep
i_factory = UndergraduateFactory()
leifeng = i_factory.CreateLeifeng()
leifeng.sweep()
# create volunteer to wash
i_factory = VolunteerFactory() # just replace UndergraduateFactory with VolunteerFactory
leifeng = i_factory.CreateLeifeng()
leifeng.wash()
import threading
class Singleton(object):
instance = None
lock = threading.RLock()
@classmethod
def __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.instance
if __name__ == "__main__":
instance1 = Singleton()
instance2 = Singleton()
print id(instance1) == id(instance2)
import abc
class Person:
def show(self):
print "person"
class Finery(Person):
def __init__(self, c):
self.component = c
def 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 abc
class Observer:
__metaclass__ = abc.ABCMeta
def __init__(self, n, nr):
self.name = n
self.notifier = nr
@abc.abstractmethod
def 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):
pass
if __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()