Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
---------------------
| AbstractPersister |
---------------------
^
/ \
---
|
------------------
| OrderPersister |
------------------
^
/ \
---
|
--------- ---------
| Offer |<>---| Item |
--------- ---------
The above is ascii art for a UML class diagram. I have a domain class called "offer" which contains items. E.g. an offer for a trip which costs 500 bucks and contains a bus ticket, a nights stay at a motel and a show.
When the customer checks out of the store, the offer is persisted to the database as an order containing order items.
Would it make sense in a pure object oriented design to make the offer inherit from an OrderPersister? The OrderPersister can get a connection to the database using its super class, the AbstractPersister. I want to add a method to OrderPersister called Book, which will create the order and its items in the database.
If this is a bad design, what alternatives are there? I want this to be as object oriented as possible please.
Before extending a class, you should ask this question: is a?
Is an Offer an OrderPersister? I say it's not.
You should take a look at the Data Mapper pattern. It will let you use domain objects that know nothing about persistence, and still be able to persist them.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm creating a PostgreSQL DB where I'll store some users, so I need to know which is the exact size (MB) of each user.
This is my reasoning :
Profile picture : JPEG 15 Mb 75% = up to 1,8 MB
name + surname + work : 20 characters each so = 60 B
date of birth : timestamp = 8 B
bio : up to 500 characters = 500 B
For a total of (approximately) 2,5 MB.
So if I have 1 GB of available space on the DB I will store up to 400 users.
Is it right? Am I missing something?
I would not store the image binary data in the database, this is not a good idea. Store it in azure blob storage and just store the url to it. At least not in the database, make the database as small and fast as possible or you will get issues later down the line. e.g. indexing with large columns will make queries slow down in time.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the following problem in dynamic programming.
A person has time machine and he can move in time either 1 year or 2. At the beginning he is at year 0 and he wants to reach year 100. Every step he does (1 or 2 years) he is paying some fixed fees. There is an array with 100 integers represents the fee he needs to pay if he went threw the specific year.
I need to find the minimum amount the person can pay to go from year 0 to year 100 using dynamic programming.
From what i have done so far i think that there should be something like
minCost(i) = min{A[i-1], A[i-2]}
and the base cases are years 1 and 2 which costs A[1], A[2] respectively. But i think this approach has more of greedy algorithm rather than dynamic programming.
I saw the bin packing algorithm of dynamic programming and i understood it and the matrix that represents it.
How should the matrix of the shown problem above look like?
And how should i build the function and the pseudo code for this problem?
You are almost there.
Think about how will you reach the i th year from i-1 th year and i-2 th year. There is a fee which you are forgetting to take into consideration.
MinCostToReachYear(i) = min( MinCostToReachYear(i-1) + fee(i-1), MinCostToReachYear(i-2) + fee(i-2) )
You already know the base cases year 1 and year 2. Can you think of extrapolating with the use of a for loop or more easily with a recursive function which you already know as mentioned above? I leave it as an exercise for you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm taking a class in database design. The curriculum focuses heavily on nomalization. The steps and methods speak for themselves, however I find the intuition aspect frustrating. Is there a mathematical way to handle data normalization, where one can assign the properties and crunch a mathematically certain conclusion?
I'll give another real world example in a discussion regarding addresses.
You are architect in charge of redesigning a legacy app with an old database that contains address information.
The DB currently has 1 field for all address information.
Our address looks something like this:
Address
___________________________________
Bob Robertson 123 Broad Way Springfield IL 62701
Unacceptable! Let's normalize this a bit to remove some of that redundancy, so in our second pass, we break it up into a few fields:
Address1 Address2 City State Zip
________ ________ ____ _____ ____
Bob Robertson 123 Broad Way Springfield IL 62701
Well, you say, there are multiple addresses within a state, so we need to break that out into a lookup table, and we can save tons of space on that city name, and there is a one to many relationship between streetname and all of the numbers that exist on that street.
We also need to represent the 9 digit zip codes, and one 5 digit zip code has multiple 4 digit suffixes, so that needs it's own table too in order to fully normalize this relationship, boy am I clever!
Also, we might have multiple robertsons, and multiple Bobs that we ship things to, so we need a cross reference lookup table for that as well.
At this point, our table may look like this:
FirstNameID LastNameID StreetID StreetNum CityID StateProvinceID ZipID 4DigitSuffix
__________ _________ ________ ___________ ______ _______________ _____ ___________
3452 1257 45 234 990 32 123 1234
If you presented this to most rational developers, requiring 6 or more joins just to get address information, you might get run out of town on a rail.
In many cases, there is an optimal middle ground that satisfies business needs and also speed/ease of development.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm looking at writing a Django app to help document fairly small IT environments. I'm getting stuck at how best to model the data as the number of attributes per device can vary, even between devices of the same type. For example, a SAN will have 1 or more arrays, and 1 or more volumes. The arrays will then have an attribute of Name, RAID Level, Size, Number of disks, and the volumes will have attributes of Size and Name. Different SANs will have a different number of arrays and volumes.
Same goes for servers, each server could have a different number of disks/partitions, all of which will have attributes of Size, Used space, etc, and this will vary between servers.
Another device type may be a switch, which won't have arrays or volumes, but will have a number of network ports, some of which may be gigabit, others 10/100, others 10Gigabit, etc.
Further, I would like the ability to add device types in the future without changing the model. A new device type may be a phone system, which will have its own unique attributes which may vary between different phone systems.
I've looked into EAV database designs but it seems to get very complicated very quickly, and I'm unclear on whether it's the best way to go about this. I was thinking something along the lines of the model as shown in the picture.
http://i.stack.imgur.com/ZMnNl.jpg
A bonus would be the ability to create 'snapshots' of environments at a particular time, making it possible to view changes to the environment over time. Adding a date column to the attributes table may be a way to solve this.
For the record, this app won't need to scale very much (at most 1000 devices), so massive scalability isn't a big concern.
Since your attributes are per model instance and are different for each instance,
I would suggest going with completely free schema
class ITEntity(Model):
name = CharField()
class ITAttribute(Modle)
name = CharField()
value = CharField()
entity = ForeignKey(ITEntity, related_name="attrs")
This is very simple model and you can do the rest, like templates (i.e. switch template, router template, etc) in you app code - its much more straight-forward then using complicated model like EAV (I do like EAV, but this does not seem the usage case for this).
Adding history is also simple - just add timestamp to ITAttribute. When changing attribute - create new one instead. Then fetching attribute pick the one with the latest timestamp. That way you can always have point-in-time view of your environment.
If you are more comfortable with something along the lines of the image you posted, below is a slightly modified version (sorry I can't upload an image, don't have enough rep).
+-------------+
| Device Type |
|-------------|
| type |--------+
+-------------+ |
^
+---------------+ +--------------------+ +-----------+
| Device |----<| DeviceAttributeMap |>----| Attribute |
|---------------| |--------------------| |-----------|
| name | | Device | | name |
| DeviceType | | Attribute | +-----------+
| parent_device | | value |
| Site | +--------------------+
+---------------+
v
+-------------+ |
| Site | |
|-------------| |
| location |--------+
+-------------+
I added a linker table DeviceAttributeMap so you can have more control over an Attribute catalog, allowing queries for devices with the same Attribute but differing values. I also added a field in the Device model named parent_device intended as a self-referential foreign key to capture a relationship between a device's parent device. You'll likely want to make this field optional. To make the foreign key parent_device optional in Django set the field's null and blank attributes to True.
You could try a document based NoSQL database, like MongoDB. Each document can represent a device with as many different fields as you like.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am working on the parking lot example, and made few assumptions as I design.
I have couple of questions in assigning attributes to Items/Objects.
1) If parkingSpace is not assigned by a system, i.e., user just into Lot, finds an appropriate place (car/bike/truck/handicapped) and parks his car.
I think I don't need a ParkingSpace object, but instead, I can keep a count of no_of_free_places for each of the category_of_parking_space.
Since parking space is big, we just maintain three variables.
no_of_free_slots_Car
no_of_free_slots_Bike
no_free_slots_Truck
no_free_slots_Handicapped
when a vehicle comes in, we just decrease one of the above values (which means, of the available X places, user chooses one and parks there), & when vehicles goes out, we increase the corresponding value.
(in short, parking lot is not assigned by anyone, vehicle just goes to one of the eligible places & parks there)
2) Assuming we have a single global parking meter.
--> Should the start_time/end_time be an attribute of Vehicle?
Or
--> vehicle_id, start_time, end_time be part of ParkingMeter.
3) Assuming the need for parkingSpace object, should 4_wheeler, 2_wheeler, handicapped be a enum type, or a separate class altogether.
If its enum, we can use findEmptySlot(parkingSpace_type);
If they are separate class altogether, and ParkingLot has a method findEmptySlot();
How can we get the appropriate Slot?
ParkingMeter will be responsible to setting the vehicle's start, end times right?
If the has multiple amounts, 1hr - 20$, 2hr - 30$, 3hr - 40$, 5hr - 50$
is it good have these part of ParkingMeter class or, include them in a separate class "ParkingPrice"
Okay,
I didn't really understand the question, but I would indeed use an array ParkingSpace objects to describe the spaces in the park.
Since the time is per vehicle, the time should be set on the vehicle.
I'd use a different class for each, and each should be extending the abstract class Vehicle. It allows for flexibility with common and unique attributes of each vehicle type (serial number for all vehicles, but only 4_wheelers have doors for instance). As for how to find empty parking spaces, each ParkingSpace object would have a $takenBy property, which would hold an instance of the vehicle object occupies it. It should default to null, then, you simply iterate through the array of spaces in your ParkingLot class, and find the one with $space->takenBy == null.