Win a copy of Head First Android this week in the Android forum!

Billy Vandory

Ranch Hand
+ Follow
since Mar 23, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
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 Billy Vandory

Jim Hoglund wrote:Billy : Let's construct a scenario to illustrate how polymorphism is really used. Consider a
hypothetical TV remote control.

Let's say that interface BasicControl declares methods: on(), off(), volume() and channel() and
that class RemoteV1 implements this interface; nothing extra, just the declared functions. The
methods available on 'x' would be the same for either "BasicControl x = new RemoteV1()" or
"RemoteV1 x = new RemoteV1()". But since we've been told to "design to the interface", we
deploy our program with the first "interface" definition.

Now it's time for an upgrade, adding methods mute() and sleepTimer(). Let's declare the new
methods in interface BetterControl which extends BasicControl. The new implementing class will
be RemoteV2 which extends RemoteV1. Also, let's say that an improved channel() method has
been developed and will be included in RemoteV2.

So what do we have? In writing RemoteV2 we didn't disturb the validated RemoteV1 methods
as they were inherited. The new mute() and sleepTimer() methods were coded and validated.
We decided, however, to include an overriding channel() method with better performance.

Older remotes can be upgraded by replacing RemoteV1 references with RemoteV2 and no other
changes. This works because "BasicControl x = new RemoteV2()" will work fine; even better
because RemoteV2 provides an improved channel() method (override). New remotes will need
"BetterControl x = new RemoteV2()" to access the new methods. Plus other program changes to
take advantage of the new behaviors.

Jim ... ...



Thanks for that excellent answer, Jim. If I have a class RemoteV1 and I want to add mute functionality as you mentioned, why can't I just add it to the RemoteV1 class? Adding a new method wouldn't break anything, so is this just a best practice?

And If I should extend V1, when does that happen? I mean, if I am in the progress of writing RemoteV1 and a day later, after I'm done implementing RemoteV1 I say "darn, I forgot the MUTE button", I wouldn't extend the class, I would just add the functionality to the RemoteV1 class, ? But if the app has gone through QA and is in production and an upgrade proposes a Mute button, then it would make sense to extend the class RemoteV1 at that point ? Just wondering how to decide when to extend a class... Seems like OO is more of an art than science. I try to make an effort to make sure my classes provide methods that fit that class and the class can take care of itself, but sometimes it feels like I am including every method under the sun and wonder if I shouldnt break it up a bit. So I ask myself, is there any advantage in breaking up a class? Could an invoker of my class benefit from instantiating a lesser version of my class? If so, I break it up. But this adhoc method of coding is a result of not pre-planning the work I suppose.....

Thanks again,

Billy
11 years ago
..construct may not be the right word, but take this example:


Ok I understand this, and how polymorphism work. but the following line:



Why would someone ever instantiate the B with A? Is there a real world example that could used to explain where this could be used?

The only reason I could think of is it limits the scope of the methods available on 'a', but then you could just down? cast. (is it down or up? B b = (B) a;)

I have no specific reason for asking this, just a thought that entered my narrow mind. But if it's handy for some reason, maybe I can incorporate it.

Honestly, in my code, I don't find many opportunities to extend my classes, and I think thats a big indicator im not ooping correctly. I did procedural C for years and OOP
is still kinda strange to me. I'm the type of guy that writes procedural code using objects, if you get what im saying. But I am trying hard to break that habit and write good code.

Thanks

Booly.
11 years ago

David Newton wrote:What magic numbers?

XML vs annotations really depends on your needs. Personally, I still use XML for some things--it keeps classes leaner, more clear, and more exportable into other environments. For some things, annotations are awesome, though.



I agree they are awesome, but here's an example of what i mean:



the number 1 is hardcoded, the name for the generator (im not too concerned) and I managed to use a String constant for the sequence name.
But i was hoping there would be a better way than using constants. Obviously I can't say



was just wondering what other folks are doing
11 years ago

Tim Holloway wrote:If free is the limiting factor, PostgreSQL might be the better choice. MySQL added stored procedures and related refinements relatively recently (MySQL 5). User-defined Functions (UDFs) have to be coded in C. In comparison, PostgreSQL has supported triggers, stored procedures and user-defined functions for a long time now, and alows a broader selection of languages to be used.

Of course, given a UDF that does an OS exec() call, you can do just about anything.



Yeah, I totally agree. I used to use mysql, but when I couldn't even use sequences I became concerned and moved to postgresql. LAMP should be renamed to LAPP. (sorry, didn't mean to insult any MySql people out there)

Mark Spritzler wrote:You need to inject the EntityManagerFactory, then in your methods call the getEntityManager() of the factory. This is needed because you can't create an EntityManager up front, with a Connection, and have your Repository hold on to it forever. This is bad resource holding. So it isn't possible for you to inject an EntityManager.

I hope that helps and makes sense. It is an extremely correct and best implementation to not allow you to inject an EntityManager.

Good Luck

Mark



Hi Mark, I was under the impression that the EM injected by spring or via the @PersistenceContext is a proxy only. I'm concerned now because in my code I
inject the EM via the @PersistenceContext annotation, managed by spring. Can you clarify? Do I need to change my code to inject a factory and grab the EM from the factory instead?

thanks...

billworth
11 years ago
If I have clustered web servers, and I have a user object, say, in the session, and the session is about to passivate, other than implementing Serializable
on the user class, is there anything else i need to do in order to ensure the user object is moved over to the new session?

sorry if this is the wrong forum
11 years ago
I've been using JPA lately, with struts and spring and most of my classes have a lot of annotations. It seems every framework utilizes annotations, it's really starting
to get out of hand when all my methods have 1-5 annotions each all with hardcoded values. Ok, some frameworks allow you to use configuration xml files instead of
annotating, so is it better to use config files when possible over annotations? How do you deal with all these magic numbers in the annotation parameters?

Any ideas or suggestions welcome...

Billy
11 years ago
I'm using JPA with hibernate as the provider. I've implemented an entity with a self referencing relationship in order to be
able to do nested categories. When I load the root node, hibernate generates one select for each entity in the tree in order
to load the children. Im beginning to think, by the very nature of this type of tree, that this is unavoidable. can someone confirm?

Here's the mapping:



and here's the query. I just query the root node. Maybe this part is wrong



Here's the data:



and here's what hibernate does.



Too many selects (1 per entity to load all the children)

So, am I doing something wrong?

Sean Clark wrote:Hey,

Whilst this isn't a direct answer I know that Spring uses cookies to save this kind of information and can use it to store information about the Locale and Theme settings which (I think) can be used between session. So maybe using cookies is an option? Here is a link with some basic info on cookies

Sean



Hey Sean, thanks for the link. I guess I should have mentioned that this is for users that have cookies disabled. The site needs to operate with and without cookies the same way. When cookies are enabled, everything is cool.

11 years ago
The use case for the site im working on allows a user to browse pages without first logging in. The user may logon at anytime, and if authenticated, the user gets an extra menu of functionality.

However, the site requires knowledge of the users location for every page (as the location is used on 99% of the pages in some way), so right up front, if the user accesses the site and the locationId is not set, the user is forwarded to a page that asks for the locationId.

At first, I wanted to try and persist the locationId via rewriting the URL but that got to be too cumbersome, so I decided to just put it on the session.

Problem is, if the user enters his locationId which gets stored on the session, when he logs on, the session id changes, so the user is asked once again to enter the info.

Is there a way to detect a sessionid change so I can move the contents of one session to another? im looking at the listeners, but not sure if any of these will do the trick.

thanks... bi||y

11 years ago
In my index.jsp I have a link that goes to a registration.jsp page. The book I am reading (Struts 2 in Action) says best practice is to route simple requests through actions. Ok, so in my index.jsp I have a link:



And in my struts-config.xml I have a simple action to act as a passthrough

In Register.jsp I display a form:


The action is Register and I define that in struts-config.xml


So far so good. In my Register action (RegisterAction.class), I do some validation:


If there are validation errors, the error gets printed - great.

If there are no validation errors, the page is *redirected* to the homePage action.

If the user ends up on the Register.jsp page and bookmarks it, when he returns to that page via the bookmark, an NPE is generated because emailAddress is null. But in all examples I've seen with validation, I do not see any checks for null when validating a field.

How does one prevent bookmarking this page, (because that kind of breaks the workflow of the pages anyway) or is there a way not to validate if the page is being rendered for the first time?

Thanks,

Billy

11 years ago
I have a tag file which is responsible for building the header (which is the same on every page).

If the user is authenticated I show 'logoff', otherwise i show 'signin | register'

in reading these forums, I see time and time again that the jsp should be as dumb as possible. which makes sense. and im wondering how I can change the following. I don't think I can get away from using logic in the jsp page, but maybe the jsp page should not be aware of the session and authentication object. how would you guys handle this? I will have to do something similar for which menu buttons are available based on the users roles. So this page will end up having a lot of logic if I continue down this road. thanks! Billy (in training)



11 years ago

David Newton wrote:http://struts.apache.org/2.1.8/docs/why-cant-i-use-jstl-style-el-expressions-in-struts-tags.html

Or use <s:set>. Either I or someonebody else wrote a thing about accessing JSP tag attributes, but I can't find it at the moment. In this case, if it's a string, it's just as easy this way.



Thanks David and Ankit!


11 years ago

I have the following in my tag file, and I get an exception:





If i replace myMap[${groupName}] with myMap['Jobs'] it works perfectly. From the JSP that calls the tagfile i have




I also tried:



(this was someone elses solution from another forum with the same problem) but I still get the same error.

I have made sure EL is working, etc.

Is there any way to pass a variable that contains a string into my myMap object (I assume myMap is OGNL) ?

Thanks,

Billy[]



11 years ago

David Newton wrote:No, that's the way Java works: it's like crack for people that like to type. For things like this I usually just create another base class if it's functionality used all over the place. Unfortunately, Java provides no trivial mechanism for sharing implementation.



Thanks David, for your reply.

Taking it one step further, the content that I'm displaying, that uses that columnData getter, in multiple pages looks as follows:



I was thinking of making a custom tag, or struts component or something that contains that markup so that in the pages I can just use a <me:showColumnData/> tag.. But my problem now is, to use that tag, the columnData has to be in the action (I guess on the ValueStack in struts world?) or the tag will not work. Here are the two options I thought of:

1. The tag would work on every page I need it, if the tag queries the controller instead of having the action do it

2. The tag could just have the columnData passed to it, and it would render the markup

I think #2 is the better way to go, but perhaps there are other options?

Thanks again
11 years ago