Skip to content

Instantly share code, notes, and snippets.

@wgcv
Forked from pazdera/bridge.py
Created February 18, 2016 19:44
Show Gist options
  • Save wgcv/9f143a7c52aaacf328cb to your computer and use it in GitHub Desktop.
Save wgcv/9f143a7c52aaacf328cb to your computer and use it in GitHub Desktop.
Example of `bridge' design pattern in Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Example of `bridge' design pattern
# This code is part of http://wp.me/p1Fz60-8y
# Copyright (C) 2011 Radek Pazdera
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class InterfazAbastracta:
""" Interfaz Abastracta.
Es la interfaaz que el cliente usa
"""
def algunaFuncionalidad(self):
raise NotImplemented()
class Puente(InterfazAbastracta):
""" Clase Puente.
Es la clase puente entre la Interfaz Abstracta
y la implementacion.
"""
def __init__(self):
self.__implementation = None
class CasoDeUso1(Puente):
""" Variacion de la Interfaz Abstracta
Es la variacion de la interfaz abstracta que puede
hacer algo diferente y que tambien puede ser usada
por diferentes clases gracias a la clase puente
"""
def __init__(self, implementacion):
self.__implementation = implementacion
def algunaFuncionalidad(self):
print "Caso de uso 1: ",
self.__implementation.otraFuncionalidad()
class CasoDeUso2(Puente):
def __init__(self, implementacion):
self.__implementation = implementacion
def algunaFuncionalidad(self):
print "Caso de uso 2: ",
self.__implementation.otraFuncionalidad()
class InterfazDeImplementacion:
""" Interfaz para las clases de implementacion.
Esta define como el puente se comunica con
diferentes clases.
"""
def otraFuncionalidad(self):
raise NotImplemented
class Linux(InterfazDeImplementacion):
""" Implentacion de las clases finales
Implentacion de la clase linux
"""
def otraFuncionalidad(self):
print "Linux!"
class Windows(InterfazDeImplementacion):
def otraFuncionalidad(self):
print "Windows."
def main():
linux = Linux()
windows = Windows()
# Couple of variants under a couple
# of operating systems.
casoUso = CasoDeUso1(linux)
casoUso.algunaFuncionalidad()
casoUso = CasoDeUso1(windows)
casoUso.algunaFuncionalidad()
casoUso = CasoDeUso2(linux)
casoUso.algunaFuncionalidad()
casoUso = CasoDeUso2(windows)
casoUso.algunaFuncionalidad()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment