Created
July 19, 2016 21:03
-
-
Save hholst80/17cf00176b3e90ca34733ba2611f9a18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Basic dumb abstraction. | |
class Base(object): | |
def name(self): | |
return self._name | |
# .... | |
# MegaBase: an abstraction that we can work with. | |
# Classic OO and derived from Base. | |
class MegaBase(Base): | |
def supervalue(self): | |
return 3.14 * self._beta | |
# ... | |
def name_length_times_supervalue(self): | |
# use abstract methods | |
return len(self.name()) * self.supervalue() | |
# Proxy base is an useful abstraction, | |
# based on the useful MegaBase abstraction. | |
class ProxyBase(MegaBase): | |
def __init__(self, base): | |
self._base = base | |
# Base methods | |
def name(self): | |
return self._base.name() | |
# ... | |
# MegaBase methods | |
def name_length_times_supervalue(self): | |
return self._base._name_length_times_supervalue() | |
# ... | |
# Now we can create epic machines on instances of different abstractions, | |
# and those constructs are still MegaBases. | |
class DeathStar(ProxyBase): | |
def __init__(self, base): | |
self._base = base # or super(self, Proxybase).__init__(base)?? | |
# ... | |
def fire_pulsar_beam(self): | |
if self.name() == 'DeathStar' and self.name_length_times_supervalue() == 1234.55: | |
self.fire() | |
else: | |
raise 'NOPE' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment