I have a question regarding OOP. I'm not new to Delphi, but I learned it by myself, mostly from the internet and didn't learned it the "correct" way. I just started to take a look at OOP some weeks ago. So this is my problem:
I have an own Class called "Session". This class is connected with Edits through Visual LiveBindings. I want to fill my class with data from a TClientDataset (there is a reason why I don't bind the dataset with the edits directly). Now I have 2 ideas:
1. I build another class which implements the Dataset and handles the opening of files and Navigation.
2. I Drag and drop the ClientDataset one my Form and connect it to my class with Livebindings, like this: DataSet <---> Class <---> Edit
I hope you understand my description. So: Which idea is better? I have the feeling that LiveBindings "destroy" the Concept of OOP. On the other hand I don't need to write Code and typecast, because the live bindings do that for me.
So which way would you choose and why? Or has somebody another idea?
I hope you understand what I mean and can help me :)
With LiveBindings it is perfectly possible to bind to objects instead of datasets.
See: http://www.malcolmgroves.com/blog/?p=1084
In summary: take look at TAdapterBindSource component. It is designed for creating LiveBindings between existing controls and custom objects. In it's onCreateAdapter event you can return a TObjectBindSourceAdapter as aBindSourceAdapter in case you want to bind one specific object, or return a TListBindSourceAdapter for binding list of objects.
LiveBindings and OOP does not bite each other!
Related
I have experience with StarUML and Jumli. At my current company we have a Professional license for EA 9.2.
When I design a program, I start with creating Use Cases. Then from every Use Case, I create a Sequence Diagram. And from the Sequence Diagrams, almost every Lifeline-object will become a class (and all messages will become a method). With StarUML and Jumli this wasn't a problem.
But I cannot find a way in EA to convert the Lifeline objects into classes.
I tried to read the help from Sparx, but to me it is not really helpful. Their 'tutorial' is not a tutorial, but actually just another help file with lots of blah blah.
Can anyone tell me how to do this? Or are there any really helpful links?
It IS possible, but only in one a bit hidden way. First I'll tell you about the alternative ways:
you have a conceptual error here. Timelines in Sequence diagram are not classes, but objects. So, they can't become classes.
Of course, a tool could have a function to create a class according to an object, as VP UML has. Obviously, EA has not such feature.
On the contrary, it is not a problem to drop a class on the sequence diagram to become a timeline. But EA has some conceptual problems with it, too. You have to make it an object here. Look also this.
On the other hand, there should be no problems to drop a timeline to the object diagram, but in EA it IS a problem - the object-timeline remains the timeline even on the Object diagram. That is obviously non-convenient and ununderstandable, but it is so. Obviously, it is one of these bugs that are called "features".
The same is behaviour of the timeline if we drop it on the Class Diagram. We even can't connect this object to a class by generalization - EA considers such connection as a rules breaking one.
And finally, how you CAN connect the timeline/object to a class. Right-click the timeline on the sequence diagram or its copy object on the class diagram. Go to Advanced. Choose Instance Classifier - the first menu item. There you can choose an existent class or even create a new one. And the object/timeline will belong to this very class.
You can't reach this feature from the Project Browser. It is obviously, another "feature" of the EA.
In a spec-adherent UML tool, one can assign classes (among other things) to a lifeline. I think that is why you are having difficulty converting lifelines into classes: they aren't the same kind of thing! Try creating classes first and assigning them to lifelines.
To transform sequence model into class model right-click on the package choose "Transform Current Package" => Check DDL => Select Transform
I hope this helps.
Regards,
Nabil
This question already has answers here:
Prefer composition over inheritance?
(35 answers)
Closed 9 years ago.
I am making a program for studying chess openings, traps and maybe other related things. It has a class MoveSequence, which basically is an ordered list of objects from a class ChessPosition. I also have a class ChessOpening which has a sequence of moves and a name, an ECO-code (chess opening classification system) and probably some more stuff.
Should I implement ChessOpening as a subclass of MoveSequence, or should it just contain a MoveSequence object? The same question would apply for a class ChessTrap.
Don't think it matter so much what I choose in this simple problem. But I want to learn this stuff, so I was wondering if there is some principles, or rules of thumb, one should consider when making decision like this.
My 0.02$ worth:
If a possible subclass does not share basically all of the properties and methods of its superclass, I start to wonder.
Overriding methods to change behavior, or adding new properties and methods is normal. But when you hit that awkwardness when some of the superclass properties/methods are not applicable to the subclass, it gets creepy. (What do you do then, just ignore the invalid/inappropriate parts? Hope no one ever calls them? Stub them out and raise an error if they are used?)
If xxx is not really a yyy, maybe xxx and yyy are both zzz's. (Or if they just share common behaviors, maybe create a module to include, which is a standard Ruby/Rails practice.)
A real Computer Scientist may have a more concise and definitive answer...
I started writing some code for a 2D game, created a class "objets" trying to keep it as generic as possible. I have a few methods and attributes that are common to every kind of element (buldings, ppl, interface buttons, etc) like (w, h, x, y ...you know) but most of them only make sense when applied to and specific type of item.
So I would have to inherit a new class for every type of actor in the game?
Just wondering if this is a common practice, or maybe i should manage it in a different way.
Thanks in advance.
If you're introducing behaviour then subclass, however if the difference is attribute based then don't e.g.
Animal (has .colour and .makeSound) -> Dog (has .eatOwnPoop) -> RedDog (no, too specific, covered by colour)
Notice how I had ".makeSound" in Animal. I could have put .bark in dog, but then I'd have to put .meow in cat etc. The subclass can simply override and provide a concrete sound.
However, you can use interfaces to better cross-cut your code, but that's quite a lengthy topic and probably overkill for your needs (although it could help any unit testing you do).
It sounds like you are over-using inheritance. It is certainly a red flag when you simultaneously say "common attributes like ..." and "...only make sense when applied to a specific type." Also, it is a red flag that domain objects such as building share a common base class with an interface object like button. Finally, it is quite unusual to define your own objet (object?) class from which every class in your system derives. It's not inconceivable, but in combination with your other comments, it sounds like you've started down an unproductive path.
You might want to refer to a good tutorial on object-oriented design and analysis such as "Head First OOA&D"
You do not HAVE to do anything. Generally, it is useful to use derived classes if they exhibit some kind of commonality but become more specialised in nature requiring specific functionality at each level of inheritance. It is also good to use if you want to have polymorphic behaviour. You have asked a very open ended question but basically do not feel that you HAVE to use inheritance as not every problem requires it and indeed some people overuse inheritance, introducing it in places where it really is not needed. All in all, I would really recommend that if you haven't already that you read a good book on object oriented design as this will then get you to think about your code from a different perspective and greatly improve the way you view software and design it. It may sound like a cop out but this kind of question is very hard to answer without knowing all details of what you are doing.
I'm writing some documentation and I just can't find the right word. Let say my class is called Writer and some people will be using it. How should I name objects that use the class (or instances of) I'm documenting?
Users of Writer class? - Program is not "a user".
Consumers of Writer class? - Sounds like somebody will eat it.
Callers or Writer class? - Sounds good for methods only.
There must be a correct word for this and I should feel stupid for asking but please, help.
Edit: just to clarify, I'm thinking about the code (not programmer) that is calling and using the class or instance of it (well, maybe I'm thing in code to much...)
I will prefer user anyway, even it is not a end-user. When you write documentation for your code the target audience is a programmer that will use your code. That programmer and their programs are users of your code.
How about "Client"?
First of all, know that there are no stupid questions, just stupid answers.
An End-User (the developer in this case) would be the user of the class.
Consuming is a verb related to aquiring resources.
Indeed calling is for functions.
Well, in the classic book Thinking in Java 2nd edition, there are two ways of using a class: by composition or by inheritance.(yes, this bit is also important)
I don't remember the exact words, but the summary would be sort of like this:
composition - you create a new object from which the target class instance would be referred.
inheritance - you create a new class that inherits (extends) the target class, provided that class is able to be inherited from.
For the exact terminology, I would say it really doesn't matter that much.
But if you want just describe the case where a new instance of such a class is created, you could say some actor (user) instantiates a new object of this class.
Hope this helps.
Edited : the terminology really depends if you think it is the user or the user's code that makes use of the target class.
I have an extremely hard time figurering out how classes needs to communicate with eachother. In a current project I am doing, many classes have become so deeprooted that I have begun to make Singletons and static fields to get around(from what I get this is a bad idea).
Its hard to express my problem and its like other programmers dont have this problem.
Here is a image of a part of the program:
Class diagram
ex1. When I create a Destination object it needs information from Infopanel. How to do that without making a static getter in InfoPanel?
ex2. DestinationRouting is used in everybranch. Do I really have to make it in starter and then pass it down in all the branches?
Not sure if this makes sense to anybody :)
Its a problem that is reacurring in every project.
After looking at your class diagram, I think you are applying a procedural mind set to an OO problem. Your singletons appear to contain all of the behavior which operate on the records in your domain model and the records have very little behavior.
In order to get a better understanding of your object model, I'd try and categorize the relationships (lines) in your class diagram as one of "is-a", "has-a", etc. so that you can better see what you have.
Destination needs some information from InfoPanel, but not likely all information. Is it possible to pass only the needed information to Destination instead of InfoPanel?
What state is being captured in the DestinationRouting class that forces it to be a singleton? Does that information belong elsewhere?
There's just too little information here. For example, I am not even sure if MapPanel and InfoPanel should be the way they are. I'd be tempted to give the decorator pattern a try for what it's worth. I don't know why a Listener is a child of a Panel either. We need to know what these objects are and what system this is.