我是第一次尝试使用python在OOP中编写。它仍然没有感觉通过主控制器传递接口类型。我试图遵循SOLID原则。让我展示代码。
TicTacToeOnConsol.py:
import os
from Controller import StartController
os.system('cls')
StartController().StartGame('consolInterface')
Controller.py:
from UserInterface import ExecuteActionOnInterface
class StartController:
def StartGame(self, strInterfaceType):
dispWelcomeContinue = ExecuteActionOnInterface().DisplayWelcome(strInterfaceType)
if dispWelcomeContinue == False:
return False
#Do more stuff
return True
UserInterface.py:
from ConsolInterface import ConsolInputMessages
from ConsolInterface import ConsolOutputMessages
class ExecuteActionOnInterface:
def __init__(self):
self.selectDisplayWelcome = {
'consolInterface' : ConsolInterface().DisplayWelcomeConsol,
'windowInterface' : WindowInterface().DisplayWelcomeWindow,
'webInterface' : WebInterface().DisplayWelcomeWeb,
}
def DisplayWelcome(self, strInterfaceType):
self.selectDisplayWelcome[strInterfaceType]()
return True
class ConsolInterface:
def DisplayWelcomeConsol(self):
return ConsolInputMessages().DisplayWelcomeWithInput()
class WindowInterface:
def DisplayWelcomeWindow(self):
raise NotImplementedError('This interface has not yet been implemented')
class WebInterface:
def DisplayWelcomeWeb(self):
raise NotImplementedError('This interface has not yet been implemented')
ConsolInterface.py:
import os
import msvcrt as inputKey
class ConsolInputMessages:
def ContinueOrQuit(self):
print "Any Key = Continue"
print "Q = Quit"
print "Please press Any Key to continue, or Q to quit:"
continueOrQuit = inputKey.getche().upper()
if continueOrQuit == 'Q':
print '\n'
print '\nExiting Game...'
return 'quit'
os.system('cls')
return 'continue'
def ContinueRestartOrQuit(self):
print "Any Key = Continue"
print "Q = Quit"
print "R = Restart"
print "Please press Any Key to continue, R to restart, or Q to quit:"
continueRestartOrQuit = inputKey.getche().upper()
if continueRestartOrQuit == 'Q':
print '\n'
print '\nExiting Game...'
return 'quit'
elif continueRestartOrQuit == 'R':
print '\n'
print '\nRestating Game...'
return 'restart'
os.system('cls')
return 'continue'
def DisplayWelcomeWithInput(self):
ConsolOutputMessages().DisplayWelcome()
if self.ContinueOrQuit() == 'quit':
return False
return True
class ConsolOutputMessages:
def DisplayWelcome(self):
print """\
******************************************************
**************** Welcome to TicTacToe ****************
******************************************************
Copyright (c) 2014 fiddlefreak
All Rights Reserved
This product is protected by copyright and distributed under
licenses restricting copying, distribution and decompilation.
"""
任何python专家都能给我一些例子,说明UserInterface如何将Consol接口实例化为自己的对象,所以当控制器在接口上调用一个函数时,它不必通过通过类型,UserInterface已经知道使用哪一个?
答案 0 :(得分:1)
不传递接口类型的字符串值,传递类的实例(或传递字符串,并让控制器实例化接口)。您可以在创建控制器时执行此操作。保存对接口的引用,然后直接调用该接口上的方法:
interface = ConsoleInterface()
StartController(interface).StartGame()
...
class StartController:
def __init__(self, interface):
self.interface = interface
def StartGame(self, interface):
self.interface.DisplayWelcome()
return True