|
查看: 1087|回复: 4
|
Factory Pattern
[复制链接]
|
|
|
发表于 15-11-2008 09:23 PM
|
显示全部楼层
请大家使用中文发表文章,没输入法者可使用本站提供的网上输入法。
目前论坛已內置中文输入法,大家可轻松输入中文。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-11-2008 11:06 PM
|
显示全部楼层
回复 2# cheng1986的分身 的帖子
抱歉,我不知如何用华文解释 ,如果真的是要中文的话,请你帮我翻译可以吗?先谢了.. |
|
|
|
|
|
|
|
|
|
|
The factory method pattern deals with the problem of creating objects (products) without specifying the exact class of object that will be created. This pattern is the most common use in any programming language. Let said, my application (UI), need to exchange files using few transport protocols (http,smtp,ftp,sftp......). I need decouple the UI with my transport layer (because it easy for me to add/edit the protocol without modifies my UI layer). Here is what i do.
1. Create a Transport abstract class (it may be interface in other language like C#/Java)
2. Implement the transport concrete class. ( the actual protocol coding is here....)
3. Create a Manager class. (UI only deal with this manager)
#abstract class
class Transport(object):
def __init__(self):
self.desc = "transport"
def connect(self):
print "connect..."
def disconnect(self):
print "disconnect..."
def logon(self):
print "logon..."
def send(self):
print "send file..."
def receive(self):
print "receive file..."
#concrete class
class Http(Transport):
def __init__(self):
self.desc = "http"
def connect(self):
print "http connect..."
def disconnect(self):
print "http disconnect..."
def logon(self):
print "http logon..."
def send(self):
print "http send file..."
def receive(self):
print "http receive file..."
#concrete class
class Ftp(Transport):
def __init__(self):
self.desc = "ftp"
def connect(self):
print "ftp connect..."
def disconnect(self):
print "ftp disconnect..."
def logon(self):
print "ftp logon..."
def send(self):
print "ftp send file..."
def receive(self):
print "ftp receive file..."
#concrete class
class smtp(Transport):
def __init__(self):
self.desc = "smtp"
def connect(self):
print "smtp connect..."
def disconnect(self):
print "smtp disconnect..."
def logon(self):
print "smtp logon..."
def send(self):
print "smtp send file..."
def receive(self):
print "smtp receive file..."
#manager class
class TransportFactory():
def __init__(self):
self.factory = {"http":Http,"ftp":Ftp,"smtp":smtp}
def getTransport(self,transporttype):
return self.factory [transporttype]() if transporttype in self.factory else Http()
#UI layer. ---> UI only deal with the abstract/interface only
trp = TransportFactory().getTransport("smtp")
trp.connect()
trp.logon()
trp.send()
trp.receive()
trp.disconnect()
trp2= TransportFactory().getTransport("ftp")
trp2.connect()
trp2.logon()
trp2.send()
trp2.receive()
trp2.disconnect()
Happy coding..... |
|
|
|
|
|
|
|
|
|
|
发表于 16-11-2008 03:35 PM
|
显示全部楼层
基本上Design Patters 分三个部分
- Creational
- Behavioral
- Structural
而楼主所说的Factory Patter 是属于 Creational
小弟对Design Patters 还是很模糊  |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 16-11-2008 11:51 PM
|
显示全部楼层
回复 4# sfkwan 的帖子
|
慢慢来,其实在我们日常的CODING中已经用到PATTERN了。只是我们不发觉而已。只要理解PATTERN 的用途在稍微REFACTORING你的CODE,就慢慢上手了。最重要的是,你的CODE容易MAINTAIN和EXTEND就好了。当然也要别人明白你的CODE才算OK。:) |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|