Beginner's conceptual question about OOP and persistence - oop

This is a very basic question about OOP (I'm using python but really it's a conceptual question so not really language specific). I've looked around but no tutorials or books cover this specific question. If I am unclear I apologise and would be happy to clarify anything I've written.
Let's say I'm creating a simple address book that I want to write to disk using pickle. I have a class called Contact where __init__ takes in some args (firstName and lastName) and I have a menu where you can create contacts, edit them etc. For all examples of OOP I have seen they would do something like...
bob = Contact('Bob', 'Smith')
jane = Contact('Jane', 'Smith')
...to create new instances of Contact. But these are all defined before runtime. What happens when I want all these instances created on the fly by user input? Do I create new instances for each person? How would I do this from user input? Then just write all the instances to a list and pickle it? Or do you do something like...
firstName, lastName = raw_input("Enter first name: "), raw_input("Enter last name: ")
contact = Contact(firstName, lastName)
...then just append contact to the list and get new values for the contact instance every time I want to add a user? This is a key concept that I'm not really getting (because I haven't seen it explained really anywhere). All examples I've seen don't do the above but instead create new instances for each thing/person but all of them are pre-defined and not created on the fly. I would be really grateful for someone to explain this concept to me.

Your example is exactly how it works.

Yes, that's generally how you do it - make arrays of your objects. Or some other kind of collections, depending on your language and/or framework. When creating a new object, you first create it in a temporary variable, and then insert it into your collection.
Sometimes, when you have a LOT of objects, you don't load them all at once from your persisted storage (like a DB or a file). You just load the one (or few) that you need to work with. If you load just one, it might get a special variable. Several will get a collection again.

Related

Efficient way to translate an application

So i have developed an application in vb.net but recently i came across the requisite of allowing multiple languages for it. I dont know if there is any 'common' way of doing this kind of things, but my approach to accomplish that is the following:
I'll need to search in the code for components, error messages and everything that is displayed in the GUI of the application to be translated.
Secondly i will create a class in which i'll store in memory a dictionary of everything that will be translated
after, i'll replace the stuff to be translated withing an entry of the dictionary
then when the application start i'll load the dictionary
later on, i'll replace the static dictionary and will load it in memory from the database
So for example, my dictionary class:
Dim dictionary As New Dictionary(Of String, String)
dictionary.Add("00011", "hello there!")
Somewhere in my code i'll replace:
mylabel.text = "hello there!"
With:
mylabel.text = dictionary.item("00011")
Later on i will, instead of having a static dictionary, create that dictionary getting the information from a database like this (and load it at the start of the application:
_______________________________________
word_code ### word_EN ### word_FR
_______________________________________
00011 ### hello there ### bonjour il
I will load the dictionary considering which language is selected.
I'm not very confortable with this approach and i have no idea if this is the right thing to do, but if so i have a couple of questions:
is a dictionary the best data-structure to do so?
will this be memory-heavy considering i'll have 1000 entries, 1m entries or 10m entries?
is there any logic and faster way of accomplish the same?
Thank you so much in advanced,
J
It's a common way of doing it - having a system name along side a language code being used to look up a translated value. However, generally speaking I'd only advice you to do this for something like system texts and smaller text segments.
The reason is that in for example CMS/ecommerce systems, pages with lots of text likely will need to be translated in a data model to support it to begin with; and then you already have the language division.
So in that situation, you're better off making a page structure with a translated data model where the detail will be language specific per language for your current website.
For example, you'll have a product -> product_detail where detail keeps the translated values for said product. Similar for article -> article_detail and so on.
But for general translations and system texts which needs to be displayed, it's a common way to do it.
And as you suggest yourself, structures like like dictionary would be a good structures to to make fast look ups and can be cached in the system so you do not need to retrieve them all the time.
Some ways you can expand on it, is by sub dividing your translations into sub groups; say you have an order page and a product page. Then you can have translations assigned to "product" and to "order" with a "common" group as well.
It will also make it easier to build smaller cache objects, extract less data from your data storage etc, so a page which only revolves around orders don't need to worry about product translations.
It will require memory, but unless you put entire CMS systems into the translations, it should be "minor".
I would however question a need of 10 million entities of translations and wonder whether or not your system actually requires that many and if it does, then maybe consider an alternate approach and whether it might be better to make multiple versions of the "page" to eliminate the need for translations.
I would also advice you to not use "00011" as a system code to begin, and go for a more "readable" version (like "hello") to ease the readability and maintainability of your code. Then if you want a 'system value' which is like "00011", it's easy to do a search/replace.

Is there a standard for customer data?

It seems like every time a new project gets started, a bunch of customer-related code gets copied and pasted. First name, last name, gender, email, etc.
Is there a current standard for customer contact / id data? It seems to me that it would be MUCH easier to have a defined standard for working with this information rather than getting stuck on whether or not to use "first_name" or "firstName" when creating this structure.
What I'm looking for is a universal format for a customer object.
Anyone seen anything like this?
In java every variable name and also member variable names should be set in lower camel case.
http://en.wikipedia.org/wiki/CamelCase
Here are the naming conventions from oracle:
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367
This is some kind of "standard" for such things.
A bit old, but have you looked at the Party Model as a standard way of dealing with things like customers?
Take a look at the data model diagrams from the Apache Open for Business Project - you'll find plenty of useful information on how to construct the customer object by looking at how they deal with the Party object first:
https://cwiki.apache.org/confluence/display/OFBTECH/Data+Model+Diagrams

Simulation for large number of objects associated with other objects ("have a")

I am trying to really get a good idea how to think in OOP terms, so I have a semi-hypothetical scenario in my mind and I was looking for some thoughts.
If I wanted to design a simulation for different types of people interacting with each other, each of whom could acquire different proficiency levels in different "skills", what would be an optimal way to do this?
It's really the "skills" thing that I was a bit caught up on. My requirements are as follows:
-Each person either "has" a skill or does not
-If people have skills, they also have a "proficiency level" associated with the skill
-I need a way to find and pick out every person that has certain skills at all, or at a certain level
-The design needs to be extendible (ie, I need to be able to add more "skills" later)
I considered the following options:
have a giant enum for every single skill I include, and have the person class contain an
"int Skills[TOTAL_NUM_SKILLS]" member. The array would have zeros for "unacquired" skills, and 1 to (max) for proficiency levels of "acquired skills".
have the same giant enumeration, and have the person class contain a map of skills (from the enum) and numbers associated with the skills so that you can just add only the acquired skills to the map and associate a number this way.
Create a concrete class for every single skill, and have each inherit from an abstract base class (ISkill, say), and have the person class have a map of ISkill's
Really, option 1 seems like the straightforward no-nonsense way to do it. Please criticize; is there some reason this is not acceptable? Is there a more object oriented way to do this?
I know that option 3 doesn't make much sense right now, but if I decided to extend this later to have skills be more than just things with proficiency associated with them (ie, actually associate new actions with the skills (ISkill::DoAction, etc), does this make sense as an option?
Sorry for the broad question, I just want to see if this line of thought makes sense, or if I'm barking up the wrong tree altogether.
The problem with option 1 is future compatibility. Say you were shipping this framework to customers. Now, the customer has built this array of Skill values, which is length TOTAL_NUM_SKILLS, for each person. But this fails as soon as you try to add another skill, and especially as you try to reorder skills.
What if the customer is using an RPC framework in which a client and server pass Person objects over the wire? Now, unless the customer upgrades the client and server at the exact same time, the RPC calls break, since now the client and server expect arrays of different lengths. This can be particularly tricky because the customer may own only the client, or only the server, and be unable to upgrade both at once.
But it gets worse. Say the client has written out a Person object to disk in some file. If they decided to serialize a person as a simple list of numbers, then a new skill will cause the deserialization code to fail. Worse, if you reorder skills in your enum, the deserialization code may work just fine but give a wrong answer.
I like option 3 exactly for the reason you named: later you can add more functionality, and do so safely (well, except for the fact that every public change is a breaking change if your customers exercised certain edge cases in the language).
If you want to add skills often without changing the overall program structure, I'd consider some kind of external data file that you can change without recompiling your code. Think about how you'd want to do it in a really large project. The person who chooses the skills might be a designer with no programming ability. He could edit the skills in an XML file, but not in C++ code.
If you defined the skills in XML, it would naturally extend to store more data with each skill. Your players could be serialized as XML files too.
When you set up a player's skills at runtime, you could build a hash table keyed on the skill name from the XML file. If it's more common to enumerate a player's skills than to query whether a player has a certain skill, you could just use a vector of strings.
Of course, this solution will use more memory and run slower than your enum solution. But it will probably be good enough unless you're dealing with millions of players in your program.

Entity, Value Object or what is it and where it should be?

I have class called 'Tool'. Tool has properties like: Name, Description and some specific others. Tool is special because Name and others are read only but description can be modified by users.
Count of tools is constant and known at development time. It is not Value Object because I need to query them and show to users where they can update Description. So, it's kind of Entity but users can not create new Tools.
I'm looking for possibility to take Tool like this:
Tool.SomeGreatTool
where SomeGreatTool is Tool with name "Some great tool" and description should be the same like this specified by user.
Jimmy Bogard has solution almost perfect but NHibernate know anything about SomeGreatTool and Description will be null.
How to modify Jimmy's solution or how to do it different way? How to instantiate SomeGreatTool from database?
We still treat these kinds of semi-constant data as a sort of well-known entities. We create value objects/enumeration classes for the tool types, but separate tool types from tools. You still need to go to the persistent store to do something like ToolRepository.Find(ToolType.Screwdriver). Even though there will be only one tool per tool type, you'd still separate these two concepts.

What to do with queries which don´t have a representation in a domain model?

This is not specific to any language, it´s just about best practices. I am using JPA/Hibernate (but it could be any other ORM solution) and I would like to know how do you guys deal with this situation:
Let´s suppose that you have a query returning something that is not represented by any of your domain classes.
Do you create a specific class to represent that specific query?
Do you return the query in some other kind of object (array, map...)
Some other solutions?
I would like to know about your experiences and best practices.
P.S.
Actually I am creating specific objetcs for specific queries.
We have a situation that sounds similar to yours.
We use separate objects for reporting data that spans several domain objects. Our convention is that these will be backed by a view in the database, so we have come to call them view objects. We generally use them for summarising complex data into a flat format.
I typically write a function that performs a query using SQL and then puts the results into either a list or dictionary (in Java, I'd use either an ArrayList or a HashMap).
If I found myself doing this a lot, I'd probably create a new file to hold all of these queries. Otherwise I'd just make them functions in whatever file they were needed/used.
Since we're talking Java specifically, I would certainly not create a new class in a separate file. However, for queries needed in only one class, you could create a private static inner class with only the function(s) needed to generate the query(s) needed by that class.
The idea of wrapping that up the functionality in some sort of manager is always nice. It allows for better testing, and management therefore of schema changes.
Also allows for easier reuse in the application. NEVER just put the sql in directly!!!. For Hibernate I have found HQL great for just this. In particular , if you can use Named queries. Also be careful of adding an filter values etc use "string append", use parameters (can we say SQL injection ?). Even if the SQL is dynamic in terms of the join or where criteria, have a function in some sort of manager is always best.
#DrPizza
I will be more specific. We have three tables in a database
USER
PROJECT
TASK
USER to TASK 1:n
PROJECT to TASK 1:n
I have a query that returns a list of all projects but showing also some grouped information (all tasks, open tasks, closed tasks). When returned, the query looks like this
PROJECTID: 1
NAME: New Web Site
ALLTASK: 10
OPENTASK: 7
CLOSEDTASK: 3
I don´t have any domain class that could represent this information and I don´t want to create specific methods in Project class (like getAllTasks, getOpenTasks) because each of these methods would trigger a new query.
So the question is:
I create a new class (somenthing like ProjectTasksQuery) just to hold that information?
I return information within array or map?
Something else?
You might feel better after reading about Data Transfer Objects. Some people plain don't like them, but if it feels like a good fit to you, it probably is.