3-tier(三层架构)
三层架构(3-tier architecture)是一个分层式的软件体系架构设计。通常意义上的三层架构就是将整个业务应用划分为:表现层(UI)、业务逻辑层(BLL)、数据访问层(DAL)。区分层次的目的即为了“高内聚,低耦合”。
1. 表现层(UI):
通俗讲就是展现给用户的界面,即用户在使用一个系统的时候他的所见所得。
2. 业务逻辑层(BLL):
针对具体问题的操作,也可以说是对数据层的操作,对数据业务逻辑处理。
3. 数据访问层(DAL):
该层所做事务直接操作数据库,针对数据的增添、删除、修改、更新、查找等。
4. 代码示例
#!/user/bin/env python #3-tier 3 layer architecture class Data(object): products = { "milk" : {'price':0.1 , 'quantity':10}, "cheese" : {'price':0.1 , 'quantity':10}, "bread" : {'price': 2.2 , 'quantity':20} } class BusinessLogic(object): """docstring for BussinessLogic""" def __init__(self, arg): self.data = Data() self.prompt = arg def productList( self ): return self.data.products.keys() def productInfo(self , product): return self.data.products.get( product , None ) def toString(self): print(self.prompt) class UI(object): """docstring for UI""" def __init__(self): self.BusinessLogic = BusinessLogic("Come in BussinessLogic layer") self.BusinessLogic.toString() def getProductList(self): print("this is a product list") for product in self.BussinessLogic.productList(): print(product) print("\n") def getProductInfo(self , product): productInfo = self.BussinessLogic.productInfo(product) if productInfo: print("The production information :") print('Name: {0} , Price:{1:.2f} ,Quantity: {2:}'.format( product.title(), productInfo.get('price' , 0), productInfo.get('quantity',0))) else: print("That product '{0}' does not exist in the records".format(product)) def main(): ui = UI() ui.getProductList( ) ui.getProductInfo('cheese') ui.getProductInfo('milk') ui.getProductInfo('bread') ui.getProductInfo('notExist') if __name__ == "__main__": main()
MVC :
MVC 是一种设计模式,包括模型(Model),视图(View),控制Controller)
1. Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。
通常模型对象负责在数据库中存取数据。
2. View(视图)是应用程序中处理数据显示的部分。
通常视图是通过控制器操作模型数据创建的。
3. Controller(控制器)作用于模型和视图之间,处理业务逻辑层的操作。
通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。
4. 代码示例
#!/user/bin/env python # design pattern : MVC #Model objects encapsulate the data specific to an application and define #the logic and computation that manipulate and process that data. class Model(object): """docstring for Model""" def __init__(self): self.prompt = "The Data Layer" def getData(self): products = { "milk" : {'price':0.1 , 'quantity':10}, "cheese" : {'price':0.1 , 'quantity':10}, "bread" : {'price': 2.2 , 'quantity':20} } return products def toString(self): print(self.prompt) #View acts on display layer that interact with users class View(object): """docstring for View""" def productList(self , productLists): for product in productLists: print(product) print ' ' #Controller acts on both Model and view. #It controls the data flow into model object and updates the #view whenever data changes. It keeps View and Model separate class Controller(object): """docstring for Controller""" def __init__(self): self.prompt = "The Business Logic Layer" self.Model = Model() self.View = View() def getproductList(self): data = self.Model.getData() self.View.productList(data) if __name__ == '__main__': controller = Controller() controller.getproductList()