I have 4 classes of the following hierachy:
MyTop
|
-------------------
| | |
BlockA BlockB BlockC
BlockA, BlockB, and BlockC are the composed objects of MyTop. BlockA provides a function LookUpTable(), and BlockB and BlockC now need to access the look-up table owned by BlockA. What is a good (or at least typical) practice for BlockB and BlockC to access the look-up table?
I thought about implementing the look-up table as a class itself, but the content of the loop-up table is slightly coupled with the initialization of BlockA, and it may not be trivial to decouple the table from BlockA (it's still doable, but not preferred). Right now my solution is to pass the request from BlockB and BlockC to MyTop, and let MyTop to manage the communication between the blocks. However, it doesn't seem a good way to handle the problem when there are more blocks that need access to the look-up table in BlockA. I wonder if this is a well-known problem and already has a "best practice" solution to it. Thanks!
I thought about implementing the
look-up table as a class itself, but
the content of the loop-up table is
slightly coupled with the
initialization of BlockA, and it may
not be trivial to decouple the table
from BlockA (it's still doable, but
not preferred).
Can't you simply make BlocA intialize that look-up table owner object? It would be a singleton (if it's correct in your case) with BlocA as friend class to be sure it's the only one to access initialization (construction?) functions.
I don't know if there's a general solution but there definitely is a general advice: tell, don't ask.
From what I can see you are trying to access the data of BlockA that is stored in a lookup table and then perform some operation on them. This is violation of the encapsulation principle where only the object holding the data should access and modify it.
A better way would be to extract the lookup table functionality into a class and add common methods for all three blocks. Then create three subclasses of the lookup table class and add methods that will be used only by specific blocks (if there are any such methods). In BlockA, BlockB and BlockC use these classes. This allows a more loose coupling than accessing a lookup table stored directly in BlockA.
With the current mechanism, BlockB and BlockC consumes the facility provided in BlockA. My understanding is that it makes it different from BlockA. They are not on par.
BlockA, BlockB, BlockC inherits the methods of MyTop.
So LookUpTable() a method of MyTop.
It should access a object attribute which is initialized only when you instantiate BlockA.
Related
I have always used structs for packaging and receiving packets, will i gain anything by converting them to classes inherited from main packet class ? is there another "c++ish" way for packaging and any performance gain by this ?
It is very general and various solutions may be available. This is related to Serialization topic and what you say is a simple model of serialization where packets contains structs which they can be loaded directly into memory and vice versa. I think C and C++ are great in this case because they allow you to write something like struct directly to stream and read it back easily. In other languages you can implement your byte alignment or you should serialize objects to be able to write them to streams.
In some cases you need to read a string stream like XML, SOAP, etc. In some application you should use structs. In some cases you need to serialize your objects into stream. It depends. But I think using structs and pointers is more forward than using object serialization.
In your case, you have 2 structures for each entity I think. A struct which moved along wire or file and a class which holds the entity instance inside memory. If you use binary serialization for your object, you can use just a class for sending, receiving and keeping the instance.
Data modelling
Generally, your C++ classes should factor the redundancy in the data they model. So, if the packets share some common layout, then you can create a class that models that data and the operations on it. You may find it convenient to derive classes that add other data members reflecting the hierarchy of possible packet data layouts, but other times it may be equally convenient to have unrelated classes reflecting the different layouts of parts of the packet (especially if the length or order of parts of the message can vary).
To give a clearer example of the simplest case fitting in with your ideas - if you have a standard packet header containing say a record id, record size in bytes and sequence id, you might reasonably put those fields into a class, and publicly derive a class for each distinct record id. The base class might have member functions to read those values while converting from network byte order to the local byte order, check sequence ids are incrementing as needed etc. - all accessible to derived classes and their users.
Runtime polymorphism
You should be wary of virtual members though - in almost all implementations they will introduce virtual dispatch pointers in your objects that will likely prevent them mirroring the data layout in the network packets. If there's a reason to want run-time polymorphism (and there can easily be, especially when reading packets), you may find it useful to have a polymorphic hierarchy of classes having 1:1 correspondences with the hierarchy of non-polymorphic data-layout classes, and just containing a pointer to the location of the data in memory.
Performance
Using a class or struct with layout deliberately mirroring your network packets potentially lets you operate on that memory in-place and very conveniently, trusting the compiler to create efficient code to do so. Compilers are normally pretty good at that.
The efficiency (speed) of that access should be totally unaffected by the hierarchy of classes you use to model the data. The data offsets involved and calls to non-virtual functions will all be resolved at compile-time.
You may see performance degredation if you introduce virtual functions as they can prevent inlining and require an extra pointer indirection, but you should put that in context by considering how else and how often you'd have switched between the layout-specific operations you need to support (for example, using switch (record_id) all over the place, if (record_id == X), or explicit function pointers).
I have to write a bunch of DTOs (Data Transfer Objects) - their sole purpose is to transfer data between client app(s) and the server app, so they have a bunch of properties, a serialize function and a deserialize function.
When I've seen DTOs they often have getters and setters, but is their any point for these types of class? I did wonder if I'd ever put validation or do calculations in the methods, but I'm thinking probably not as that seems to go beyond the scope of their purpose.
At the server end, the business layer deals with logic, and in the client the DTOs will just be used in view models (and to send data to the server).
Assuming I'm going about all of this correctly, what do people think?
Thanks!
EDIT: AND if so, would their be any issue with putting the get / set implementation in the class definition? Saves repeating everything in the cpp file...
If you have a class whose explicit purpose is just to store it's member variables in one place, you may as well just make them all public.
The object would likely not require destructor (you only need a destructor if you need to cleanup resources, e.g. pointers, but if you're serializing a pointer, you're just asking for trouble). It's probably nice to have some syntax sugars constructors, but nothing really necessary.
If the data is just a Plain Old Data (POD) object for carrying data, then it's a candidate for being a struct (fully public class).
However, depending on your design, you might want to consider adding some behavior, e.g. an .action() method, that knows how to integrate the data it is carrying to your actual Model object; as opposed to having the actual Model integrating those changes itself. In effect, the DTO can be considered part of the Controller (input) instead of part of Model (data).
In any case, in any language, a getter/setter is a sign of poor encapsulation. It is not OOP to have a getter/setter for each instance fields. Objects should be Rich, not Anemic. If you really want an Anemic Object, then skip the getter/setter and go directly to POD full-public struct; there is almost no benefit of using getter/setter over fully public struct, except that it complicates code so it might give you a higher rating if your workplace uses lines of code as a productivity metric.
I have previously asked about the proper way of accessing member variables present in the project. In the project, I have CWinapp-derived class, CMainFrm class, a list of different view classes. However, currently, I have instances of different user-defined classes instantiated in the CWinApp-derived class, while the rest of the classes use a pointer obtained from AfxGetApp() function, and then access the different user-defined classes. I was told by some community members on the MFC newsgroup that this is a very bad design (i.e. the parent should not know anything about an app-class, view class, or document class). However, I'm not sure how otherwise I can access various user-defined classes without using this design. It would be great to hear some suggestions as I'm not familiar enough with MFC to come up with proper search terms.
"(i.e. the parent should not know anything about an app-class, view class, or document class)"
I'm not sure I understand this sentence, what do you mean with 'parent' here?
Anyway, in my opinion, the design you describe isn't really a problem. It's a trade off: do you either pass these classes to all functions that need them, complicating their use and API, or do you store them as a sort of global variables like you're doing? It depends on the data that is accessed, and how often. Data that is needed in many places can just as well be 'global'.
There are multiple ways of making data 'global': make it a member of CWinApp (that is, your CWinApp-derived class), or of CMainFrame, or do you make an actual 'global variable', or do you make a singleton, ...
The problem with global variables is that it becomes hard to figure out who accesses it when and from where. If you data as a member of CWinApp, you can access it through an accessor function and trace access from there (through log messages, break point, ...) This, in my opinion, mitigates most of the problems associated with global variables. What I usually do nowadays is use a Loki singleton.
The reason that is stated in your post for not making data a member of CWinApp, as a decoupling issue, is (in the context that you've presented it) a bit strange imo. If certain classes need access, they'll need to know of those data structures anyway, and their storage location is irrelevant. Maybe it's just because I don't know about the specifics of your design.
I have a Message class which parses text messages using lookup tables. I receive a lot of messages and create and destroy a lot of objects so I thought I declare those lookup tables as static members to prevent initializing the same tables with the same values again and again.
Is it the correct approach or there's more appropriate C++ way?
Thanks.
If your message classes share lookup information that it may be worth abstracting your static lookups into a class of their own so that they really are only initialised once.
But regardless, static class members are definitely the way to go.
They could be class variables, i.e. static at the class level. That way they are available to subclasses, if any, and perhaps a bit more visible than if "hidden" as static local variables in individual methods.
This sounds like the right way to do it, although I'd expect the compiler to optimize this. Have you benchmarked your application and does declaring the tables as static speed it up?
Also note that if you have many large lookup tables, performance will increase, but the tables will be hold in memory all the time.
Yes, all ok. Sometimes it is good solution: function static which will created only once. Also you could use singleton object, but it has more wide accessibility.
Be careful if you have multi-threaded application and if your lookup-tables contain pointer to another data which could be destroyed early than this table.
Using a singleton pattern will also work for this.
I am writing a perl script to parse a mysql database schema and create C++ classes when necessary. My question is a pretty easy one, but us something I haven't really done before and don't know common practice. Any object of any of classes created will need to have "get" methods to populate this information. So my questions are twofold:
Does it make sense to call all of the get methods in the constructor so that the object has data right away? Some classes will have a lot of them, so as needed might make sense too. I have two constrcutors now. One that populates the data and one that does not.
Should I also have a another "get" method that retrieves the object's copy of the data rather that the db copy.
I could go both ways on #1 and am leaning towards yes on #2. Any advice, pointers would be much appreciated.
Ususally, the most costly part of an application is round trips to the database, so it would me much more efficient to populate all your data members from a single query than to do them one at a time, either on an as needed basis or from your constructor. Once you've paid for the round trip, you may as well get your money's worth.
Also, in general, your get* methods should be declared as const, meaning they don't change the underlying object, so having them go out to the database to populate the object would break that (which you could allow by making the member variables mutable, but that would basically defeat the purpose of const).
To break things down into concrete steps, I would recommend:
Have your constructor call a separate init() method that queries the database and populates your object's data members.
Declare your get* methods as const, and just have them return the data members.
First realize that you're re-inventing the wheel here. There are a number of decent object-relational mapping libraries for database access in just about every language. For C/C++ you might look at:
http://trac.butterfat.net/public/StactiveRecord
http://debea.net/trac
Ok, with that out of the way, you probably want to create a static method in your class called find or search which is a factory for constructing objects and selecting them from the database:
Artist MJ = Artist::Find("Michael Jackson");
MJ->set("relevant", "no");
MJ->save();
Note the save method which then takes the modified object and stores it back into the database. If you actually want to create a new record, then you'd use the new method which would instantiate an empty object:
Artist StackOverflow = Artist->new();
StackOverflow->set("relevant", "yes");
StackOverflow->save();
Note the set and get methods here just set and get the values from the object, not the database. To actually store elements in the database you'd need to use the static Find method or the object's save method.
there are existing tools that reverse db's into java (and probably other languages). consider using one of them and converting that to c++.
I would not recommend having your get methods go to the database at all, unless absolutely necessary for your particular problem. It makes for a lot more places something could go wrong, and probably a lot of unnecessary reads on your DB, and could inadvertently tie your objects to db-specific features, losing a lot of the benefits of a tiered architecture. As far as your domain model is concerned, the database does not exist.
edit - this is for #2 (obviously). For #1 I would say no, for many of the same reasons.
Another alternative would be to not automate creating the classes, and instead create separate classes that only contain the data members that individual executables are interested in, so that those classes only pull the necessary data.
Don't know how many tables we're talking about, though, so that may explode the scope of your project.