Talk:Computer Science Design Patterns/Prototype
Add topicIf I have an implementation that may be good enough to add to the prototype implementation list. If so, how would this be added?
[edit source]I'm teaching myself some design patters and have what I think may be a workable python implementation of the prototype design pattern. I didn't see a way to edit the page, so figured I would drop it off here. There are probably some NOOB aspects to this, but I've only been programming professionally for 6 months, and I had to stop writing code altogether for 6 months after that due to some traveling so there are likely some obvious refactorings you can spot right off the bat, or maybe I have something that works, but not the way I intended because I'm not 100% sure what's going on behind the scenes...please be gentle...
I think the strength of this decorator approach for prototyping is how simple it is client-side. Whether I did it 100% right, the fact that all the client needs to do is throw "@Prototype" on top of the class definition and then they can call "Clone()" on any object created with that definition and assign the result to a variable...and that's it...
# Decorator class which allows an object to create an exact duplicate of itself
class Prototype:
def _clone_func(self):
# This function will be assigned to the decorated object and can
# be used to create an exact duplicate of that decorated object
clone = self.cls()
# Call _copy_func to ensure the attributes of the objects are identical
self._copy_func(self.instance, clone)
return clone
def _copy_func(self, fromObj, toObj):
# Dual purpose function which is assigned to the decorated object
# and used internally in the decorator to copy the original attributes
# to the clone to ensure it's identical to the object which made the clone
for attr in dir(fromObj):
setattr(toObj, attr, getattr(fromObj, attr))
def __init__(self, cls):
# This is only called once per decorated class type so self.cls
# should remember the class that called it so it can generate
# unique objects of that class later on (in __call__)
self.cls = cls
# NOTE: on a decorator "__call__" MUST be implemented
# this function will automatically be called each time a decorated
# object is cloned/created
def __call__(self):
# Create an instance of the class here so a unique object can be
# sent back to the constructor
self.instance = self.cls()
# Assign the private functions back to the unique class
# (which is the whole point of this decorator)
self.instance.Clone = self._clone_func
self.instance.Copy = self._copy_func
# Finally, send the unique object back to the object's constructor
return self.instance
@Prototype
class Concrete:
def __init__(self):
# Test value to see if objects are independently generated as
# well as if they properly copy from one another
self.somevar = 0
@Prototype
class another:
def __init__(self):
self.somevar = 50
if __name__ == '__main__':
print "Creating A"
a = Concrete()
print "Cloning A to B"
b = a.Clone()
print "A and B somevars"
print a.somevar
print b.somevar
print "Changing A somevar to 10..."
a.somevar = 10
print "A and B somevars"
print a.somevar
print b.somevar
print "Creating another kind of class as C"
c = another()
print "Cloning C to D"
d = c.Clone()
print "Changing C somevar to 100"
c.somevar = 100
print "C and D somevars"
print c.somevar
print d.somevar
Output:
Creating A
Cloning A to B
A and B somevars
0
0
Changing A somevar to 10...
A and B somevars
10
0
Creating another kind of class as C
Cloning C to D
Changing C somevar to 100
C and D somevars
100
50
Irishfarmer01 (discuss • contribs) 03:35, 7 February 2014 (UTC)
- It seems to be the implementation of the Prototype design pattern. I have read the code. I don't know all the functions in Python but the code is logical. I have added your code. You say that you don't manage to put this on the page. I don't know what has embarrassed you. The mechanism should be the same for the page and for this talk page. The only issue is that we can't edit a single section, we have to edit the whole page. This is due to the template of the page. Ftiercel (discuss • contribs) 20:14, 9 February 2014 (UTC)
the client source code use a tempotype should we read prototype ?
[edit source]after a quickly read of exemple source code (c++ and java):
- c++ source code :
the 'client source code' use a tempotype should we read prototype ?
- I renamed the variable temporaryPrototype. Every variable name should be explicit. This variable could have been named with anything because we are in the client code. It's not the design pattern implementation. I admit the term prototype in temporaryPrototype is confusing as this object is not the prototype of any other object. I have tested the code.
- java source code :
the 'new' instruction is never used ?
- Yes, it is used at line 6, PrototypeTest class. Ftiercel (discuss • contribs) 20:27, 27 February 2014 (UTC)