473,651 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help: composition, static factory method, immutability,.. .and beans

for better or worse, my free time is consumed by two Java series books
from Sun:

Java Platform Performance, 2000, by Wilson and Kesselman
Effective Java, 2001, by Bloch

1.) opinions on these books? good/bad/mediocre/great/etc? are they
up to date, or out of date?

2.) please do check out my code at
http://www.geocities.com/cjavacjava/src/ comments welcome :)

3.) i'm very impressed with the idea(s) of using small immutable
classes for composition. it's massively simplified my efforts towards
a "sim" of a cat/dog ecosystem. is this a common technique? is it
"modern" and "trendy", or "old" and "tired"? or am i just scratching
the surface of how things are really done in java? (ignoring
smalltalk/c++/c#) are static factory's common?

4.) I presently have two classes, LifeForm and Location, and'll add a
"Driver" class. the idea is the driver class will hold the collection
(vector for now) of LifeForm objects, who'll wander around
independantly. maybe twice a second I'd like to get a LifeForm object
to make a call(?) to incrementX. I want each life form to move
totally independantly. good/bad idea? if ok, pls point me in the
direction to get started on that.

5.) if small, immutable classes truly give better performance and are
_easier_ to design/use/etc, then what's the big deal with beans?
seems a major contradiction.

thanks,

ja***@mail.com
Jul 17 '05 #1
2 3650
A bean is one thing, for one purpose; an immutable object is another, for
another purpose. Incidentally, there's nothing that says a bean can't be
immutable, but they often aren't. But just because a class (bean) has
getters and setters, that doesn't means it's mutable. For instance, a bean
that accepts only Strings in its getters is still immutable, because the
Strings themselves are immutable. And even if the object stored is mutable,
the bean can still be made mutable:

public setMyProperty(M yObject o)
{
_o = new MyObject(o);
}

I.e., we're storing a new copy of the object passed in, rather than a
reference to the one passed in.

In general, immutability is always good unless there's an overriding reason
not to use it. In other words, always prefer it, and make any given
situation "prove" that mutability is necessary.

Joshua Bloch's "Effective Java" talks about this stuff at length. I highly
recommend it. I'm only scratching the surface here, and he understands and
explains this stuff much better than I do.

As far as static factory methods, again, they make sense in certain
situations. I'm not sure if you're talking about the GoF static factory
method pattern, or just using static factory methods in place of
constructors.

An instance of the GoF pattern is iterator(). It meets three critera: 1)
It provides an instance of an object. 2) It provides a type, rather than a
specific implementation. (You don't know what specific implementation of
the Iterator interface is being returned). 3) It's used by several classes.

For Java patterns I recommend Steven Metsker's "Design Patterns Java
Workbook." It covers all of the GoF patterns in Java. And the author is
more than willing to respond to emails with questions.

You might be talking about simply using a static method to provide an
instance of a class, rather than constructors. For instance, you could
declare a class that has no public constructor, but has a getInstance()
method that returns an instance of the class. It might be a singleton, or
it might not; it doesn't matter to the client. This isn't a GoF pattern
usage, but it can be a very useful technique, avoiding the inherent problems
involved with constructors, but it has its own drawbacks. Again, Bloch
covers this topic in great detail and with wonderful clarity and relevance.

The most important thing I glean from your post is that you are thinking
critically about OO concepts in Java, and you obviously enjoy doing it, and
sharing your thoughts with other OO developers. That's always good.
Jul 17 '05 #2
"Dan Nuttle" <d_******@hotma il.com> wrote in message
news:a9******** ********@newsre ad2.news.atl.ea rthlink.net...
A bean is one thing, for one purpose; an immutable object is another, for
another purpose. Incidentally, there's nothing that says a bean can't be
immutable, but they often aren't. But just because a class (bean) has
getters and setters, that doesn't means it's mutable. For instance, a bean that accepts only Strings in its getters is still immutable, because the
Strings themselves are immutable.


That's not accurate. If the bean has setters, it is mutable. Mutability
refers to the "changabili ty" of the contents of an object. If a bean is
composed of three strings and has a setter for each (or even just one), can
the contents of the bean change? Yes, so it is mutable.

To the OP: You'd be better off posting to comp.lang.java. help in the future.
This group is not carried by all news servers, so many people don't see
posts here.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
9870
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ********************************** package Celcom.Client;
0
1509
by: javac | last post by:
http://java.sun.com/docs/books/performance/ listing 7-11: --------------- If the following code is written in a package seperate from the Location class, it will now cause compile-time errors: Location loc = body.getLocation(); loc.x = 5; //field x is not accessible; loc.setX(5); //method setX not found in class Location Be aware, however, that it is possible to cast the returned Location
1
2197
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being allocated on the stack but that's not a real reason. Which is OK because this isn't a real question, it's just a complaint dressed up to look like a reason Second question. If a language doesn't support a fairly obvious feature, one has to wonder if...
17
2345
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
11
3815
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
3
5701
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I have a base class with several sub classes. In runtime I want to create a instance of a sub class and assign it to a base class pointer. Nothing fancy about that. I also want to be able in runtime to decide witch type of sub class that is to be...
5
2213
by: Dinsdale | last post by:
I was discussing class architecture with one of the senior developers at my new job and him and I have a similar idea on how to work with data access and class libraries. That said, our implementations vary slightly and I wanted to post the question to the .Net community to get some feedback. So here is the issue: When designing classes both the senior developer and I agree that data access should be abstracted out from a business object...
11
2108
by: Bryan Kyle | last post by:
Hi All, I'm fairly new to C# and Generics and I'm wondering if anyone has some suggestions for me. I'm trying to implement a simple DAO framework using generics to keep my code as clean as I can, however I'm getting an error with what seems to me to be correct code. The error I'm getting is: Error 1 Cannot implicitly convert type 'Sample.PersonDao' to
9
1719
by: Marc De Schrijver | last post by:
I'm designing an OO Model for a large application, and I have some question on how to model a particular situation; it's not directly related to C# but rather to general OO. The applicaiton will be developed in C# 2.0 though, which may have some influence on the OO model. Here's what I'm trying to model: I have a class Company, a class Publisher, a class Manufacturer and a class Distributor. Their relationships are as follows: 1. A...
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8694
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.