Looking for a self-explanatory name for RAII-object lifetime management classes - naming

Lets say, I have a class named Texture that realizes the RAII-idiom. Furthermore I have a corresponding TextureManager class, that instantiates and deletes Texture objects. Its a 1-to-1 relationship: Each TextureManager instance creates one particular Texture object when it is time to do so and it deletes it when it is not required any more. The TextureManager decides this.
The point is that I'm not happy with the name of the TextureManager class. I think it is too vacuous, for two reasons:
The name TextureManager does not reflect the 1-to-1 relationship of a TextureManager object and the Texture object it creates/deletes.
The name is misleading. It could be understood as a class that loads textures from hard drive.
I've thought of a few alternatives, that I'm not happy with either:
TextureControl does reflect the 1-to-1 relationship in my opinion, but it sounds more like the name of class that actually controls existing instances.
TextureLifetimeControl would be self-explanatory, but it is long and inelegant in my opinion.
TextureHolder does reflect the "possessing" nature of the the 1-to-1 relashionship, but it sounds more like an object that takes an existing instance and keeps it to me.
Any suggestions?

Related

How to access variables from another class using getter

I am a beginner programmer and I am having a hard time grasping getters and setters. I just do not see the point.
I am trying to access the variable in Class A and use that value in class B to do some function. I thought I could use getter to access that value but that returns null since I understand that I am creating a new object with new values now. Then what is the point of a getter then?
I passed the variables over using the method parameters but that seems counter intuitive to my beginner's mind. I just don't understand that entire concept. Or am I wrong. I can use getters to access the value of another class's variable without making it static?
If I'm understanding you correctly, what you're asking is "Why do I need instance variables, with getter and setter methods to read/modify those variables, when I can just pass data into an object using method arguments?" Does that sound about right?
The answer gets to the heart of what OOP (object-oriented programming) is all about. The central concept of OOP is that you create distinct objects to represent discrete pieces of data. For example, you might want to track names and ages for some group of people; in that case, you would use different objects to represent (and by extension, store and manage data about) each individual person.
Person 1 ("Bill", 52)
Person 2 ("Mary", 13)
Person 3 ("Lana", 29)
The purpose of the class in this model is simply to define the specifications of these objects (e.g. a "Person" consists of a name and an age).
Why is this useful? First, this is a pretty intuitive system, since you can think of the objects you're creating as being actual real-life objects. Second, it makes it easy to work with data that are related. If, for example, we wanted to concatenate (join together in a string) a person's name with their age, having an object representing each person makes that easy! Just look at each object, one by one, and use getters to access the values for each instance.
To do this in a non-OOP way, we would need some other way to store the information -- perhaps as a list of names and a separate list of ages.
List of names: ["Bill", "Mary", "Lana"]
List of ages: [52, 13, 29]
In that kind of setup, it's not as easy to see which name relates to which age -- the only thing they have connecting them is their position within the list. And if the lists were sorted, those positions could change!
So, in short: object instances are a great way to handle many similar discrete collections of data.
As far as why we generally use getter methods and setter methods when working with those instances, instead of just exposing properties directly, a great explanation can be found here. But it bears mentioning that different languages handle this differently. In JavaScript, for example, all properties are accessible directly. In Ruby, none of them are, and you must use setters and getters to see/modify object instance variables.
I hope this provides some clarity!

To Have An ID or Not To Have An ID - Regarding Value Object

Let's say two domain objects: Product and ProductVariety (with data such as color, size etc). The relationship between these two is one-to-many. Conceptually saying in the domain driven design, the ProductVariaty should be a value object, which is not the same object once its data is changed. From the implementation point of view, however, it is better to have some sort identification for the ProductVariaty so that we know which ProductVariety is selected and so on. Is an only solution to convert it to an entity class?
The following is a code segment to illustrate this situation.
#Embeddable
class ProductVariety {...}
#Entity
class Product {
#ElementCollection
private Set<ProductVariety> varities;
...
}
Conceptually saying in the domain driven design, the ProductVariaty should be a value object, which is not the same object once its data is changed
That's not quite the right spelling. In almost all cases (many nines), Value Object should be immutable; its data never changes.
Is an only solution to convert it to an entity class?
"It depends".
There's nothing conceptually wrong with having an identifier be part of the immutable state of the object. For example, PANTONE 5395 C is an Identifier (value type) that is unique to a particular Color (value type).
However, for an identifier like PANTONE 5395 C to have value, it needs to be semantically stable. Changing the mapping of the identifier to the actual color spectrum elements destroys the meaning of previous messages about color. If the identifier is "wrong", then the proper thing to do is deprecate the identifier and nominate a replacement.
Put simply, you can't repaint the house by taking the label off the old paint can and putting it on a new one.
In that case, there's no great advantage to using the identifier vs the entire value object. But its not wrong to do so, either.
On the other hand, if you are really modeling a mapping, and you want to follow changes that happen over time -- that's pretty much the definition of an entity right there.
What it really depends on is "cost to the business". What are the trade offs involved, within the context of the problem you are trying to solve?
Note: if you really do find yourself in circumstances where you are considering something like this, be sure to document your cost benefit analysis, so that the next developer that comes along has a trail of breadcrumbs to work from.

UML Circular reference with both aggregation and composition

A few days ago a friend pointed out to me that I had a wrong idea of composition in UML. She was completely right, so I decided to find out what more I could have been wrong about. Right now, there is one more thing that I have doubts about: I have a circular dependency in my codebase that I would like to present in UML form. But how.
In my case the following is true:
Both A and B have a list of C
C has a reference to both A and B to get information from.
C cannot exist if either A or B stops to exist
Both A and B remain to exist after C is deleted from A and/or B
To model this, I've come up with the following UML (I've ommited multiplicities for now, to not crowd the diagram.)
My question is, is this the right way to model such relations?
Problems
Some facts to keep in mind:
Default multiplicity makes your model invalid. A class may only be composed in one other class. When you don't specify multiplicity, you get [1..1]. That default is sad, but true.
The UML spec doesn't define what open-diamond aggregation means.
Your model has many duplicate properties. There is no need for any of the properties in the attribute compartments, as there are already unnamed properties at the ends of every association.
Corrections
Here is a reworking of your model to make it more correct:
Notice the following:
The exclusive-or constraint between the associations means only one of them can exist at a time.
Unfortunately, the multiplicities allow an instance of C to exist without being composed by A or B. (See the reworked model below.)
The property names at the ends of all associations explicitly name what were unnamed in your model. (I also attempted to indicate purpose in the property names.)
The navigability arrows prevent multiple unwanted properties without resorting to duplicative attributes.
Suggested Design
If I correctly understand what your model means, here is how I would probably reverse the implementation into design:
Notice the following:
Class D is abstract (the class name is in italics), meaning it can have no direct instances.
The generalization set says:
An instance cannot be multiply classified by A and B. (I.e., A and B are {disjoint}.)
An instance of D must be an instance of one of the subclasses. (I.e., A and B are {complete}, which is known as a covering axiom.)
The subclasses inherit the ownedC property from class D.
The composing class can now have a multiplicity of [1..1], which no longer allows an instance of C to exist without being composed by an A or a B.
Leave away the open diamonds and make them normal associations. These are no shared aggregations but simple associations. The composite aggregations are ok.
In general there is not much added value in showing aggregations at all. The semantic added value is very low. In the past this was a good hint to help the garbage collection dealing with unneeded objects. But nowadays almost all target languages have built-in efficient garbage collectors. Only in cases where you want an explicit deletion of the aggregated objects you should use the composite aggregation.

Shared Domain Logic?

Take for example:
CreateOrderTicket(ByVal items As List(Of OrderItems)) As String
Where would you put this sort of logic given:
CreateOrder should generate a simple list ( i.e. Item Name - Item Price )
PizzaOrderItem
SaladBarOrderItem
BarOrderItem
Would you recommend:
Refactoring common to an abstract class/interface with shared properties a method called CreateOrderTicket
Or,
Creating a common service that exposes a CreateOrderTicket
We obviously would not want three createOrderTicket methods, but adding methods, inheriting, overloading and using generics seem like a high cost just to abstract one behaviour..
Assume for the sake of a simple example that (currently) there is no OrderItem baseclass or interface..
Help!! :)
p.s. Is there a way to overload without forcing all inheriting objects to use the same name?
Abstract base class sounds like the best option in this situation. Of course it all depends on what kind of shared behaviour these items have. Without knowing more, I'd guess all of these order items have Name and Price for example - and in future you might add more common stuff.
Without a shared base class which contains the Name and Price properties, you'll probably have troubles implementing a CreateOrderTicket method which takes a list containing more than 1 kind of orders.
Also I don't think inheriting from an abstract base class would be that high cost as technically the objects already derive from the Object base class. (Though I don't think this is completely equal to a custom base class.)
VB.Net can implement methods from an interface using a different name than the one specified in the interface but don't think the same goes for overriding abstract functionality.

UML class diagram relation type question

I have a data class with the following methods:
ExecuteUDIQuery(string query)
ExecuteSelectQuery(string query)
ExecuteSP(string anme, string[,] params)
I have a lot classes which use the data class. Now i want to create a class diagram, but i don't know what kind of relation the classes have with the data class. Is it a composite? Is it 1:1 or .. ?
An example of a class which use the data class is the Staff class. This class has a method Load(), which will load a staff object with the Id of the staff member. This method contains a query which is passed to the ExecuteSelectQuery(string query) method of the Data class.
EDIT:
The data class isn't static. However, i have my doubts. I actually don't know what to. The point is, the only thing it does is executing queries and returning the results.
I would suggest its a usage dependency relationship.
See here for a brief description.
I would query the naming of your classes. a class name should normally be a singular noun. Examples;
Window
Person
Transaction
Data is a plural, and in any case I think it should be Database.
Similarly for Staff - once again a plural, I think it should be MemberOfStaff. Unless of course it is a list of members of staff, in which case I would call it something like Department, Project or Division - whatever your problem domain indicates.
You will find that coming up with good names for classes is suprising ly difficult, but it is well worth the effort.
The difference between aggregations, composites and 1 on 1 relations are a bit vague and somewhat arbitrary.
I use the aggregation (open diamond) if one class owns the other class (is responsible for the lifecycle.
I use 1 on 1 relationships in all other cases.
Is the class instantiated by the classes that use it or are the methods static?
If they are static I would represent this as an unqualified dependency (dotted arrow pointing from the classes that is using the data class to the data class)
If the classes that are using the data class create their own private instance of that class this would be a 1:1 composition (assuming that the data class instance's lifcycle is tied to the object that is using it)
I cannot refrain from commenting your overall design, I would try to move the Load method out of the Staff class, so that this class is not dependent on the Data class directly.
Within the scope of your existing design I would suggest the following:
If the staff class contains an instance variable of the data class, then it is an association. If the data class is instantiated just to retrieve the instance, it is just a dependency of a given type, like #toolkit says.
Not enough data.
Give us some class outlines or something. From what I can see, I wouldn't have actually called this a data class (it looks more like a data accessor) which sounds like it might be a singleton (many:1, aggregation or association), or if instanced will be a 1:1 component.
Now i want to create a class diagram, but i don't know what kind of relation the classes have with the data class.
Nor do we - you've only described the Data class, and not said how Staff gets the Data it uses.
If Staff holds on to one or more instances of the data class, then there is either an association between Staff and Data, or Staff has an attribute of type Data (if Data has value semantics).
If the Data instances are referenced by multiple Staff instances, and their lifecycles are dependent on being referenced by Staff instances then this may be shown as an aggregation relation. If the Data instances are not shared between Staff instances and their lifecycles are dependent on being referenced, then this may be shown as an composition relation.
If X doesn't keep hold of the Data instances it uses, then a usage relationship is appropriate.
Dependency and Usage are the two weakest kind of "connectors". You might consider stereotypes, keywords to refine the relationship. You might find that instantiate,call,create,send stereotypes work. Without more information though the correct answer seems to be usage.

Resources