"Programming Ideas" "Design patterns" "structural patterns structural" MVC

Source: Internet
Author: User

Python version

https://github.com/faif/python-patterns/blob/master/structural/mvc.py

#!/usr/bin/env python#-*-coding:utf-8-*-"""*tl;dr80separates data in GUIs from the ways it is presented, and accepted."""classModel (object):def __iter__(self):RaiseNotimplementederrordefGet (Self, item):"""Returns a object with a. Items (), call method, iterates over key,value pairs of its information."""        RaiseNotimplementederror @propertydefItem_type (self):RaiseNotimplementederrorclassProductModel (Model):classPrice (float):"""a polymorphic-pass a float with a particular __str__ functionality."""        def __str__(self): First_digits_str= STR (round (Self, 2))            Try: Dot_location= First_digits_str.index ('.')            exceptValueError:return(First_digits_str +'. XX')            Else:                return(First_digits_str +'0'* (3 + dot_location-Len (FIRST_DIGITS_STR))) Products= {        'Milk': {' Price': Price (1.50),'Quantity': 10},        'eggs': {' Price': Price (0.20),'Quantity': 100},        'Cheese': {' Price': Price (2.00),'Quantity': 10}} Item_type='Product'    def __iter__(self): forIteminchself.products:yieldItemdefGet (self, product):Try:            returnSelf.products[product]exceptKeyerror as E:RaiseKeyerror ((str (e) +"Not in the model ' s item list."))classView (object):defshow_item_list (self, Item_type, item_list):RaiseNotimplementederrordefshow_item_information (self, item_type, Item_name, item_info):"""Would look for item information by iterating through key,value pairs yielded by item_info.items ()"""        RaiseNotimplementederrordefItem_not_found (self, Item_type, item_name):RaiseNotimplementederrorclassConsoleview (View):defshow_item_list (self, Item_type, item_list):Print(Item_type.upper () +'LIST:')         forIteminchitem_list:Print(item)Print("') @staticmethoddefCapitalizer (String):returnString[0].upper () + string[1:].lower ()defshow_item_information (self, item_type, Item_name, item_info):Print(Item_type.upper () +'Information:') PrintOut='Name:%s'%Item_name forKey, ValueinchItem_info.items (): PrintOut+= (', '+ Self.capitalizer (str (key)) +': '+str (value)) PrintOut+='\ n'        Print(printout)defItem_not_found (self, Item_type, item_name):Print('That %s '%s ' does not exist in the records'%(Item_type, item_name))classController (object):def __init__(self, Model, view): Self.model=Model Self.view=ViewdefShow_items (self): items=list (Self.model) Item_type=Self.model.item_type self.view.show_item_list (item_type, items)defshow_item_information (Self, item_name):Try: Item_info=self.model.get (item_name)except: Item_type=Self.model.item_type self.view.item_not_found (Item_type, Item_name)Else: Item_type=Self.model.item_type self.view.show_item_information (Item_type, Item_name, Item_info)if __name__=='__main__': Model=ProductModel () View=Consoleview () controller=Controller (model, view) Controller.show_items () controller.show_item_information ('Cheese') controller.show_item_information ('eggs') controller.show_item_information ('Milk') controller.show_item_information ('Arepas')## # OUTPUT # # ##PRODUCT LIST:#Cheese#eggs#Milk##PRODUCT Information:#Name:cheese, price:2.00, Quantity:10##PRODUCT Information:#Name:eggs, price:0.20, quantity:100##PRODUCT Information:#Name:milk, price:1.50, Quantity:10##That product "Arepas" does not exist in the records
Python reproduced version

"Programming Ideas" "Design patterns" "structural patterns structural" MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.