设计模式之-3-tier&MVC

本文介绍了3-tier(三层架构)的设计思想,它包括表现层UI、业务逻辑层BLL和数据访问层DAL,旨在实现高内聚、低耦合。同时,文章也解析了MVC设计模式,模型Model处理数据逻辑,视图View负责显示,控制器Controller协调模型与视图的交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
		


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值