Warren Bell

Ranch Hand
+ Follow
since Dec 20, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Warren Bell

I want to do this

public List<? extends Foo> getFoos()
{
...
}

and get the json output for members of Foo and any members of subclasses of Foo. What JAXB annotations do I use in Foo and it's subclasses. All I have managed to be able to do is get the members of Foo as json. I have not been able to get any members of Foo's subclasses.

Thanks,

Warren Bell
5 years ago
OK, I am confused. I thought the newer version of the for loop can work off a Collection as long as the Collection implements Iterable which I thought all Collections do now after Java 5. I took this quote off a site, but have read this same thing elsewhere.

http://www.clanproductions.com/java5.html

On the up side, the new form can be applied to both arrays and collections seamlessly. In the expression for(<variable definition> : <expression>), the expression part can either be an instance of an array or an object that implements the Iterable interface. In Java 5 onwards, all java.util collection classes implement this interface, and so the 'new' form of the for loop can be used instead of an Iterator to iterate through any collection. This also gives developers a hook by making their classes Iterable, they can make them available for use within new forms of the for loop.

Here's an example using the java.util.LinkedHashSet.

Collection<Integer> scores = new LinkedHashSet<Integer>();
scores.add(99); // Use of auto boxing
scores.add(88);
scores.add(77);
for(Integer score : scores) {
System.out.println("This score is " + score);
}



Dosen't the loop get an Iterator to work from and therefore it is safe to remove an object from the Collection?
9 years ago
What is the correct way to remove an object from a List in a for loop? I have run into problems with the old for loop, not the while loop, doing the same thing. It would cause the loop to loop less missing the last object in the list. Worked around it, but have not tried it with Java5 and was not sure if there was a standard way of doing it. Here are some ways I am thinking of doing it:

for(Object o : listOfObjects)
{
listOfObjects.remove(o);
}

or

int i = 0;
for(Object o : listOfObjects)
{
listOfObjects.remove(i);
i++;
}

And is there some sort of default counter or do you have to do the i++ deal?

Warren Bell
9 years ago
The Repository pattern looks like what I was talking about. I did not see the exact example, but I saw a similar example in an article at:

http://in.relation.to/Bloggers/RepositoryPatternVsTransparentPersistence

They showed something like this:

lineItemDAO.findAllForOrderId(order.getId())

being done in a repository like this:

OrderRepository.getLineItems(order)

which is kind of what I do now, but I am calling it a service instead of a repository. They then inject this OrderRepository into the order object so all they have to do is this:

order.getLineItems()

Is that basically the Repository pattern? This is what I was thinking of doing, but I wanted to create the order object by looking up itself using a Service/Repository internally. It looks like the difference being is that this pattern is for an object that needs to look up internal members versus creating the object itself. Plus would you have to have two methods, one for retrieving line items from the db and one for simply retrieving them from the object itself? I am guessing I could just use this Service/Repository in a Factory and create my order objects that way.

Any other thoughts?

Warren




For Example, I have a data grid that lists Orders. The steps I have to take are.

1. Retrieve a list of orders from data source A based on what page data grid is displaying and how many records are to be shown.
2. Retrieve the same orders from data source B.
3. Map the order status of orders from data source B to orders from data source A.
4. Update data source A with the mapped order status from data source B
5. Sort orders based on input from data grid
6. Display orders.

I have full access to data source A but I have only SELECT access to data source B. The code I use to do this is about 100 lines and works fine. I can clean it up some, but I don't no where this type of code should be placed. I have thought about encapsulating the above type of code in its own class and using some kind of variation of the Command Pattern or a simple interface of some sort. I have about 50 different situations like the above and have been doing this sort of thing in 4 different service classes.

Warren
My service layer gets bloated with object creation. I have quite a few large objects that need to be constructed from different data sources, and use different DAOs. I don't like how I am doing it, but I have never figured out where else it could be done. I thought that I could do it in the object itself.

I will take a look at the two patterns Kengkaj mentioned.

Any other suggestions?

Thanks

Warren
I currently create objects from a db in a service layer using a DAO like this:

int primaryKey = 11
Object order = orderDao.getOrder(primaryKey);

Can the object look itself up from the db? something like this:

int primaryKey = 11
Order order = new Order(primaryKey);
order.getOrder();

The DAO would be in the getOrder() method of the object itself. Is this a good or bad design? and is there some sort of pattern or cleaner way of having an object created from a db using a DAO? My service layer gets very bloated.

Thanks,

Warren
I understand that. Maybe I did not explain my self very good. I am talking about the chaining of methods like this:

(new Foo()).setFooString1("something1").setFooString2("something2").setFooString3("something3");

The setters are returning the object. But this means they do not follow the bean definition, or do they?

Wicket uses this a lot. How is this being done?
9 years ago
I have seen in some apis the use of constructors that return themselves. Wicket uses this a lot. I have wanted to write these type of constructors in my own classes. I started to play around with it but have not tested it yet. Here is what I have come up with. I am not sure it will even work.


public class Foo
{

private String fooString;

public Foo Foo()
{
super();
return this;
}

// not sure if this will be ok to use instead of public void setFooString(String fooString), since it is needed to conform to the java bean spec.

public Foo setFooString(String fooString)
{
this.fooString = fooString;
return this;
}

Rest of class ...
}

Use it like this:

return (new Foo()).setFooString("something");

What is the correct way to use this kind of pattern?

Thanks,

Warren
9 years ago
I am looking for a design pattern that allows a method to return an object along with a message. I have a singleton service object with various methods that return various objects.

public boolean doSomething()
{
return true;
}

but I also need to return a message.

public boolean doSomething()
{
String message;
// I need to return this message also
message = "Success"
return true;
}

I had one solution that returned an object that had the return object in it and the message. But, that doesn't seem like the correct way of doing it. I can't put the message as a member of the service class since many threads will be using this service object.

private String message;

public boolean getMessage()
{
return message;
}

public boolean doSomething()
{
message = "Success"
return true;
}

Is there a design pattern that does what I am looking for?

Thanks,

Warren



Is this considered good design? It just seems like there is too many checks that would have to be made.

isEmpty() == false

size() ==1

plus I would have to make sure the List was empty every time I retrieved a new LineItem.
10 years ago
I have got this post in the wrong forum. It should be in Intermediate.

Warren
10 years ago
I have a class called Order and a class called LineItem.





Now this is fine if I am retrieving from the db the entire order with all the lineItems. But most of the time I am retrieving just one lineItem. I still need the order info too. I have thought of doing this:



This seems sloppy using one member currentLineItem and a List lineItems together. I have also thought of doing this to retrieve the current lineItem from order:

LineItem lineItem = order.getLineItems().get(0);

This seems sloppy too, since I can never really be sure that the List will only have one LineItem in it without checking and it is the one I retrieved. I like the first example better, but it still doesn't seem right. What would be the best way of representing this Order class given that most of the time I am only needing to retrieve one Order with one lineItem?

Thanks,

Warren
[edit]Please use the code button; I have edited this post so you can see how much better it is to read. CR[/edit]
10 years ago
So, I have to list every jar in my classpath? There is no other way of doing this? My command line to execute this is very long. How does an executable jar work? The app in an executable jar may still need to access other outside jars. I just want to easily make changes to my app like adding another library and not have to worry about classpaths all the time. It would be nice to just copy a new jar of my app and execute it like a war file works in a servlet container.

Warren
10 years ago
I have a stand alone app, public static void main(String[] args), that uses several other libraries (jars). I am able to get it to work, but listing all of the jar files in the classpath is a pain. How do I put everything including the extra jars in one jar and execute it and get my app to see all of the other jars it needs?

Most all of my experience is with j2ee. I have not done a stand alone app of this scope before.

Thanks,

Warren
10 years ago