473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

a new design pattern for Python Library?

Hi,

I tend to use this design pattern a lot in order to aid in
compartmentalizing interchangeable features in a central class that
depend on the central class's data. I know that explicit class
friendship designs syntactic sugar, but I am thinking this should be a
standard design pattern to make friendship dependencies a little more
self documenting.

Opinions?

The Eternal Squire
class Friend (object):
def __init__ (self, friend):
object.__init__ (self)
self.friend = friend
Example use:

class Bar (Friend):
TITLE = None
DEFAULT = 1
def __init__ (self, top):
Friend.__init__ (self, top)
self.data = self.default * self.default

def __str__ (self):
print "%s mode %d default = %d" % (self.__class__.__name__,

self.friend.mode,
self.data)
class ABar (Bar):
DEFAULT = 2
class Foo:
BAR = ABar

def __init__ (self):
self.bar = self.BAR (self)
self.mode = 1

def __str__ (self):
return str (self.bar)

Nov 23 '05 #1
6 1334
The Eternal Squire wrote:
I tend to use this design pattern a lot in order to aid in
compartmentalizing interchangeable features in a central class that
depend on the central class's data.


I'm afraid I've read this paragraph and the code 3 times and I still
have no idea what you're trying to convey. Perhaps it's just because
your example is too abstract to me. It does look like it obscures the
role of the classes involved however, which doesn't seem like a good
thing to me. What do you consider a 'friendship dependency'? Is this
just the Strategy pattern?

--
Ben Sizer

Nov 23 '05 #2
Ben Sizer wrote:
The Eternal Squire wrote:
I tend to use this design pattern a lot in order to aid in
compartmentalizing interchangeable features in a central class that
depend on the central class's data.

I'm afraid I've read this paragraph and the code 3 times and I still
have no idea what you're trying to convey.


<aol>

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 23 '05 #3
bruno at modulix wrote:
Ben Sizer wrote:
I'm afraid I've read this paragraph and the code 3 times and I still
have no idea what you're trying to convey.


<aol>


Got anything more constructive to add?

--
Ben Sizer

Nov 23 '05 #4
Ben Sizer wrote:
bruno at modulix wrote:
Ben Sizer wrote:
I'm afraid I've read this paragraph and the code 3 times and I still
have no idea what you're trying to convey.


<aol>

Got anything more constructive to add?


Why do you think I used this tag ?-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 23 '05 #5
Let me try again:

Suppose I have a central class with complex behavior that I want to
simply write as a bare skeleton upon which to hang the auxillary
classes that help provide the complex behavior.

The naive approach would be to write:

class Skeleton:
def __init__ (self):
self.core_data = 1

class Auxillary:
def __init__ (self):
pass

def string (self, skeleton):
return "%s %d" % (self.__class__.__name__, skeleton.core_data)
skeleton = Skeleton ()
auxillary = Auxillary ()
auxillary.behavior ()
What I will typically do is create the auxillary class as a friend so
that I can have tighter integration between the Skeleton instance and
an Auxillary member instance, where the Auxillary instance isolates
behavior that I might want to modify later through inheritance.

class Auxillary (Friend):
def __str__ (self):
return "%s %d" % (self.__class__.__name__, self.friend.core_data)

class Skeleton:
def __init__ (self):
self.auxillary = Auxillary (self)

skeleton = Skeleton ()

print skeleton.auxillary
See the difference? I know this is a bit contrived, but I wanted to
keep the effect simple. Effectively I now have a self-documenting
friend relationship between classes.

The Eternal Squire

Nov 24 '05 #6
The Eternal Squire wrote:
Suppose I have a central class with complex behavior that I want to
simply write as a bare skeleton upon which to hang the auxillary
classes that help provide the complex behavior.
So, it's akin to the GoF's Template Method or Strategy patterns, then.
What I will typically do is create the auxillary class as a friend so
that I can have tighter integration between the Skeleton instance and
an Auxillary member instance, where the Auxillary instance isolates
behavior that I might want to modify later through inheritance.

class Auxillary (Friend):
def __str__ (self):
return "%s %d" % (self.__class__.__name__, self.friend.core_data)

class Skeleton:
def __init__ (self):
self.auxillary = Auxillary (self)

skeleton = Skeleton ()

print skeleton.auxillary


This looks just like the Strategy pattern except that instead of
storing the skeleton as a value within Auxiliary, you use a 'Friend'
base class. I would argue that this obscures the code rather than makes
it clearer. I would do this sort of thing in C++ where you need that
base class to enforce the interface, but not in Python. I don't care
what type I assign to self.auxillary as long as it supports the
methods/properties I require, and it shouldn't care whether it's being
assigned to a Skeleton or something else, as long as that object
provides the data it requires.

--
Ben Sizer

Nov 24 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike | last post by:
Hey! Im starting up a new python based project, its a very simple file tracker for our network. Some people would most likely call this a kind of asset management system but its much simplier...
4
by: Neil Zanella | last post by:
Hello, I would be very interested in knowing how the following C++ multi-instance singleton (AKA Borg) design pattern based code snippet can be neatly coded in Python. While there may be...
2
by: Chris F Clark | last post by:
1) I would really like to ask this on comp.lang.c#, but no such group exists. However, perhaps someone in one of the above two groups will know the answer and help me out. In my product, I use a...
6
by: Mr.Tickle | last post by:
How can I impleement a singleton pattern in an abstract or interface for inheritance to an object so all the pattern rules are followed on all similar objects?
24
by: John Salerno | last post by:
Since Python does so many things different, especially compared to compiled and statically typed languages, do most of the basic design patterns still apply when writing Python code? If I were to...
6
by: Daniel Santa Cruz | last post by:
Hello all, I've been trying to go over my OO Patterns book, and I decided to try to implement them in Python this time around. I figured this would help me learn the language better. Well,...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
19
by: adriancico | last post by:
Hi I am working on a python app, an outliner(a window with a TreeCtrl on the left to select a document, and a RichTextBox at the right to edit the current doc). I am familiarized with OOP...
4
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.