Fwd: How design patterns are used in JDK, nice summary


java enum differences from regular classes

How is an enum class E different from regular classes?

* static field? ok
* inst field? ok
* static method? ok
* inst meethod? ok
* static methods returning an instance of E? ok
* inst methods returning an instance of E? ok

#2 difference — flat tree. enums can’t form a class hierarchy. All enum classes are on the same level — all leaf-nodes under the same parent.

* can an enum E extend ..? E’s superclass is always java.lang.Enum
* subclasse E? no
* E implementing interfaces? ok

#1 difference — constructor:

* constructors? private only => not subclass-able
* how many instances of the class can be created in a JVM? If there are 5 enum values declared, those are the only instances. All the pointers of type E point to one of the 5 instances.
* constructor with arg? ok
* no-arg constructor? ok

java DB entity object HAS-A DAO?

Say you have an Account.java representing one row in Account table. You have an AccountDAO.java that read/write the table.

Q: Shall Account.java HAS-A AccountDAO instance?
A: I feel Account.java should not import AcountDAO at all. Acocount.java should be compilable without AccountDAO.java. Assumption is, Account.java is a more generic class. It should be usable in other projects (web, xml serialization, file-based …) without database.

Bottom line — If you want AccountDAO to be compile-time dependent on Account.java, then don’t have the reverse-dependency. DAO is one layer above the DB entity objects.

If Account table relates to Customer and Branch tables, it’s perfectly legitimate and perhaps best practice to use HAS-A between these classes. I think Hibernate and many ORM systems do that. These classes exist on the same “layer” so compile-time dependency is perhaps tolerable. To reduce compile-time dependency, use interfaces rather than classes.

entry point in a java reusable component

What does it mean when a java developer says

“download xyz jar”
“Give me your java api”
“You own that api”
“use this java toolkit”

They are referring to some java classes (or interfaces), usually organized into a package. This toolkit is maintained by one team (owner) to be used by other teams.

Now, good OO design requires encapsulation, so there are usually a limited number of entry points to the toolkit. I mean the first thing you write to reference the toolkit. I think there are only 2 entry points — static methods and constructors.

In some toolkits, api methods are instance methods. Question is how do you get the first instance from this toolkit? The entry points are all about instantiation.

Factory are often static methods.

share variables between methods within a class

* a special map instance field to hold all such data. Map of Objects, so casting needed everywhere.

* (laziest) instance field? reserved for instance attributes. Don’t abuse.
* (laziest) static field? reserved for class attributes. Don’t contaminate.
* return a collection? A bit clumsy but recommended by many.
* best option: avoid sharing. Stick to local vars if possible.
* 2nd best: sometimes the shared variable really belongs to another class. That class/object perhaps is already shared between your methods. Essentially use that object as a courier. More work than the laziest.

java abstract classes +! concrete method?

Often u make it an interface but there are a few justifications

Q: I think an interface must be public ??
A: non-public interface can be nested inside a class.

* all fields in interfaces must be public static final
* all methods in interface must be public
* #1 justification: Say you plan to add concrete methods to your abstract class. You make it abstract now (instead of interface) to forbid subtypes extending something else. Consider the nightmare — U make it an interface, a subtype extends something else, and now u want it to extend your abstract class instead …. the required change could be huge and painful.

multiple inheritance:unrelated classes sharing methods

This is a limited definition of multiple-inheritance — 2 unrelated classes with unrelated base classes need to share a method (evolving). If you can achieve this, then a class C can inherit methods from many classes.

Let’s say the class hosting the shared method is H. If C can somehow inherit the method from H, then C can also inherit other methods from class G, provided G can immitate[1] H.

[1] sometimes impossible.

Solution 2: IS-A -> HAS-A. Strategy Pattern. Put the concrete method into a helper class, and instantiate an instance of it in each of your “unrelated classes”. P 18 [[ head first design patterns ]] FlyBehaviour.java and QuackBehaviour.java

Solution 1: “global method” ie static method in a utility class. Similar to Arrays .java and Collections .java?

My own design: In nextgen-fttp, 2 classes need the same method parseClli() but the 2 classes already have their own parents

public static String[] parseClli(String clli){
String clliBase, clliExt;
if (clli.length() < 9){
return new String[] {clli, ""};
clliBase = clli.substring(0,8);
clliExt = clli.substring(8, clli.length() );
return new String[] {clliBase, clliExt};

an obj ref = a remote control

each rcontrol is programmed to a target.
u can program your rcontrol to an Animal, or a Mammal, or a Dog, or erase all programming.

When your rcontrol is programmed for an Animal, bark() method is not available — the “bark” button is in a detachable extension and removed from the rcontrol. You need to cast.

Q: objects are passed by…?
A: well, u duplicate the rcontrol. A method receiving an arg need to duplicate the rcontrol and can’t reprogram the original rcontrol

Q: FINAL var (of a ref type) means?
A: non-reprogramable