What is the right way to do "loose coupling" in python? -
hello wrote code data using pyserial below.
class depend on serial class doesn't meet "loose coupling" rule.
should use interface decouple class?
lot instruction.
import serial class arduinoconnect: def __init__(self): pass def serial_connect(self, serial_port, serial_baudrate): self._serial_port = serial_port try: self.ser = serial.serial( port=self._serial_port, baudrate=serial_baudrate, parity=serial.parity_none, stopbits=serial.stopbits_one, bytesize=serial.eightbits, ) except serial.serialutil.serialexception, e: print str(e) def serial_disconnect(self): self.ser.close() def get_quaternion(self, number_of_data=50): buff = [] self.ser.write('q') self.ser.write(chr(number_of_data)) j in range(number_of_data): in_string = self.ser.readline() buff_line = in_string.split(",") buff_line.pop() buff_line = self.hex_to_quaternion(buff_line) buff.append(list(buff_line)) return buff def hex_to_quaternion(self, list_of_hex=none): #...... pass arduino = arduinoconnect() arduino.serial_connect(serial_port="com5", serial_baudrate=115200) print arduino.get_quaternion() arduino.serial_disconnect()
i adjusted code recommended.
di separate serial process,and factory method encapsulate di process.
there else meet "loose coupling" rule?
help.
import serial class arduinoconnect: def __init__(self, serial_to_arduino): self._serial_to_arduino = serial_to_arduino def get_quaternion(self, number_of_data=50): buff = [] self._serial_to_arduino.write('q') self._serial_to_arduino.write(chr(number_of_data)) j in range(number_of_data): in_string = self._serial_to_arduino.readline() buff_line = in_string.split(",") buff_line.pop() buff_line = self.hex_to_quaternion(buff_line) buff.append(list(buff_line)) return buff def hex_to_quaternion(self, list_of_hex): ...... def __getattr__(self, attr): return getattr(self._serial_to_arduino, attr) class serialtoarduino: def __init__(self): pass def serial_connect(self, serial_port="com5", serial_baudrate=115200): self._serial_port = serial_port try: self.ser = serial.serial( port=self._serial_port, baudrate=serial_baudrate, parity=serial.parity_none, stopbits=serial.stopbits_one, bytesize=serial.eightbits, ) except serial.serialutil.serialexception, e: print str(e) def serial_disconnect(self): self.ser.close() def readline(self): return self.ser.readline() def write(self, data): self.ser.write(data=data) def get_arduinoconnect(): 'factory method' return arduinoconnect(serial_to_arduino=serialtoarduino()) arduino = get_arduinoconnect() arduino.serial_connect(serial_port="com5", serial_baudrate=115200) print arduino.get_quaternion() arduino.serial_disconnect()
i can think of 2 possible solutions
- implement "adapter" expose methods main class , hide implementation of serial. main class can avoid having dependency on concrete class serial
do dependency injection, likes this
def serial_connect(self, engine, serial_port, serial_baudrate)
update 1: referred http://en.wikipedia.org/wiki/adapter_pattern commonly used when want separate concrete implementation abstraction. think of travel plug adapter.
it particularly useful language java strict interface , everything. in case, because in python don't have "interface", can simulate using abstract class
class abstractadapter(): def serial_connect(self, serial_port="com5", serial_baudrate=115200): raise("needs implementation") # same thing rest of methods
then in arduinoconnect
, can check type
def __init__(self, serial_to_arduino): if not isinstance(serial_to_arduino, abstractadapter): raise("wrong type")
this forces serial_to_arduino
extend abstractadapter
enforces implementation of abstract methods hence adapter.
this might not "pythonic" way things, oop point of view, can way have highest level of loose coupling (in opinion)
p/s: actually, think correct pattern in case should strategy, both of them pretty similar in term of implementation meant different purposes. can read more patterns strategy, proxy, command, mediator used achieve loose coupling
Comments
Post a Comment