I have come across a problem where I need two classes that will have identical implementation and the only difference between them will be different name and base class. What is a reasonable way of doing this?
One obvious solution is to violate DRY and copy the implementations like this:
class FooA(BaseA):
def frobnicate(self):
print("frob")
class FooB(BaseB):
def frobnicate(self):
print("frob")
You can use multiple inheritance to implement interfaces and common functionality in a single mixin class. Given the clear desire to frobnicate in many classes, just implement a frobnicator. Python builds the class from right to left so mixins are left-most.
class Frobnicator(object):
def frobnicate(self):
print("frob")
class FooA(Frobnicator, BaseA):
pass
class FooB(Frobnicator, BaseB):
pass
Note that mixins usually do not implement their own __init__ - that's the job of the base class.
Related
I feel like this subject is touched in some other questions but it doesn't get into Python (3.7) specifically, which is the language I'm most familiar with.
I'm starting to get the hang of abstract classes and how to use them as blueprints for subclasses I'm creating.
What I don't understand though, is the purpose of concrete methods in abstract classes.
If I'm never going to instantiate my parent abstract class, why would a concrete method be needed at all, shouldn't I just stick with abstract methods to guide the creation of my subclasses and explicit the expected behavior?
Thanks.
This question is not Python specific, but general object oriented.
There may be cases in which all your sub-classes need a certain method with a common behavior. It would be tedious to implement the same method in all your sub-classes. If you instead implement the method in the parent class, all your sub-classes inherit this method automatically. Even callers may call the method on your sub-class, although it is implemented in the parent class. This is one of the basic mechanics of class inheritance.
At my OOP theory exam I was asked a question "What is a metaclass? What is a metaclass of metaclass?". I answered first one easily, but have no idea about the second one. Is there even something like "metaclasses of metaclasses" in any programming language, or even theoritically? I tried implementing something like that in Python 3, but it seems it's a bit too complicated for my (I only had simple Python course for 1 semester).
Yes.
I will use the Python language to explain how can that be - maybe it is the language where metaclasses are more palpable (at least it is the language with the majority of questions involving metaclasses here).
So, in an OOP language where "classes" are first class objects - that is, they are themselves objects, and all the rules that apply to other objects apply to classes as well - they just happen to be the "template" for other kind of objects, these classes themselves are instances of "something". This something is the "metaclass" - which is just a word to designate the "class of a class".
And it happens that this "metaclass", the class of a class is an object as well, just as other classes. And as such, it also is an instance of a class - this class of the metaclass is what can be named the "metametaclass".
Now, bringing it down to a concrete example in Python - by default, the metaclass for classes in Python is type. That is, type is the class which instances are classes.
The language does have ingenious mechanisms that make it practical in some points to inherit a class from type, and then work with custom meta-classes.
So, what is the class of type? That will be the "metametaclass" for most (or all) classes in Python.
And as it goes, the class of "type" is "type" itself. yes - it is a circular reference, without which the object hierarchy of the language could not be bootstraped. I don't know if other languages do that differently - but in Python it is quite patent:
>>> class A:
... pass
...
>>> type(A)
<class 'type'>
>>> type(type(A))
<class 'type'>
>>> type(type(A)) is type(A)
True
Now, besides working like that as a concept, it actually can be used as a metametaclass, and there are mechanisms of the language that can be tweaked by inheriting type for that purpose. (Normally it will be inherited to be customized as an "ordinary" metaclass).
By coincidence, I had an use case for the "metametaclass" exemplified this very week, in a question here - by customizing Python's type __call__ method, instead of __new__ or __init__, and using this custom class as the "metaclass for a metaclass" one can actually control how those methods are called and the parameters passed to them methods when an ordinary class is be created.
A few query and opinions to seek on the best type of relationship and representation to be used in a class diagram for modelling with uml
1) Third party library used by my class
-- I have modeled them as packages
2) Wrapper Class to wrap around modified code
-- I have modeled this class as an interface
3) My wrapper class actually use non-class member function that is written in another namespace
-- This puzzled me. How should I modeled them?
4) For classes in my own created library(dll), how do i differentiate the class that is exported and those that is not
Thanks
1) That's fine. However, it depends on the layer. I could think of a component to represent a library.
2) Not necessary. A wrapper inherits from a class. So use a generalization.
3) You can not really do that. You might use an artifact and a relation (association) to it.
4) I would use a component with interfaces (lollipops) to show the exported ones. The others are kept inside.
For all answers: YMMV
How do i model a call to a static method in a class diagram ? Can someone show me a link to a class diagram that has done that?
for example there's a class called Animal. and i have another class called Vertibrates, and it has a method which is static (and this is the only method in that class). My Animal class will be using this static method of the class Vertibrate. Therefore how can i model it in class diagram ?
You don't. Well, not in a class diagram at least. Instead, you should use sequence chart diagrams to express method calls (whether static or dynamic).
You can't model the call directly in a class diagram (as #Imontrieux says), but you can model the relationship (i.e., that Animal uses (calls) static methods in Vertibrate; BTW, the correct spelling is Vertebrate), which I suspect is actually what you meant.
You use a UML dependency for this, not an association (since the latter are for associations between instances of the classes at each end)--- see How to show usage of static methods UML Classdiagram.
Great question. One thing the GoF do in Design Patterns is used notes to demonstrate intended uses. For example, from the section on the Command Pattern:
Command Pattern
While #user1315906 is technically correct that you don't model such things in Class Diagrams, but rather in Use Case or Sequence Diagrams, if it makes sense to note how something is intended to be used in a Class Diagram, you can use a note :)
I am writing a system which generates code for a number of classes and I need to document it with a UML diagram. The classes will follow the same structure but they will have names set by the user. Is there a way to specify that CCodeGenerator generates the code for these classes?
Also, I currently have a relationship between my CDataDefinition class (which defines what should be included in each of the generated classes) and the CCodeGenerator, is there a way to denote that the multiplicity of the relationship between the generated classes and the generator is exactly equal to the number of CDataDefinition instances?
These classes will be used in another system which will also need UML class diagrams made for it. Is there a way to show that a class in this project (CEditior) uses them?
Example of operation:
I have 3 CDataDefinition objects which define classes X, Y, and Z. My CCodeGenerator instance will create 3 classes (C# code in .cs files) from these.
CEditor in a separate solution will then interface with these 3 classes.
If you read some of the introductory information on MOF, you will see that in the UML family an instance of a metaclass in one layer is a classifier in the next.
In your case, a class in the code generator describing the class in its output will be a metaclass (CDataGenerator), and the classes in the output represented by instances of the metaclass.
There is no way in plain UML for associations other than 'X is of type Y' to cross between the layers.
You may be able to model such a relationship using MOV QVT (query, view, transform - i.e. a language for mapping one model to another), but I don't know current state of tool support for that, and if you had a QVT tool you probably wouldn't need to be writing a code generator.
You need to build a template class (CDataDefinition) that will represent the structure of a class that can be created by CCodeGeneratorWhen you're creating actual class you do the binding so all you have to do is show that CCodeGenerator has an operation (let's say) classGenerator(name:String) and then you can show that this method creates a class as a proper binding on CDataDefinition.