Python设计模式
2016-10-22 15:00
232 查看
一.简单工厂模式
首先来看下工厂模式的原理图:[b]
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Jason Wang class IUser: def GetUser(self): pass def InsertUser(self): pass class IDepartment: def GetDepartment(self): pass def InsertDepartment(self): pass class CAccessUser(IUser): def GetUser(self): print("Access GetUser") def InsertUser(self): print("Access InsertUser") class CAccessDepartment(IDepartment): def GetDepartment(self): print("Access GetDepartment") def InsertDepartment(self): print("Access InsertDepartment") class CSqlUser(IUser): def GetUser(self): print("Sql GetUser") def InsertUser(self): print("Sql InsertUser") class CSqlDepartment(IDepartment): def GetDepartment(self): print("Sql GetDepartment") def InsertDepartment(self): print("Sql InsertDepartment") class IFactory: def CreateUser(self): pass def CreateDepartment(self): pass class AccessFactory(IFactory): def CreateUser(self): temp=CAccessUser() return temp def CreateDepartment(self): temp = CAccessDepartment() return temp class SqlFactory(IFactory): def CreateUser(self): temp = CSqlUser() return temp def CreateDepartment(self): temp = CSqlDepartment() return temp if __name__ == "__main__": factory = SqlFactory() user=factory.CreateUser() depart=factory.CreateDepartment() user.GetUser() depart.GetDepartment() ## """ Sql GetUser Sql GetDepartment """
abstract
相关文章推荐
- Python设计模式系列之一: 用模式改善软件设计
- 设计模式之python版(六)--原型模式
- 设计模式之python版(四)代理模式
- 设计模式之python版(三)--装饰模式
- 【设计模式】观察者模式(observer)c#/python实现
- <读书笔记> Thinking in python (Python 设计模式) 3. Proxy and State模式
- Head First 设计模式—Python实现
- Head First 设计模式——装饰器(Decorator Pattern)——Python实现
- 设计模式之python版(二)--策略模式
- Head First 设计模式——策略模式(Strategy Pattern)——Python实现
- python与设计模式之二 装饰器模式(以dota为例)
- Python设计模式系列之一: 用模式改善软件设计(转载)
- Python设计模式系列之一: 用模式改善软件设计
- Head First 设计模式——观察者模式(Observer Pattern)——Python实现(1)
- <读书笔记> Thinking in python (Python 设计模式) 2. Templated Method
- <读书笔记> Thinking in python (Python 设计模式) 1. Singlton的c++与python的实现
- Python语言与设计模式
- Python 与设计模式 1
- 设计模式之python版(一)--工厂模式
- Python设计模式系列之一: 用模式改善软件设计