Wednesday, December 05, 2007

More patterns

http://www.java-interview.com/design_patterns_interview_questions.html

Factory method

public abstract class EiskugelAutomat{
Eiskugel eiskugel; //programmieren gegen Abstraktion (hier abstrakte Klasse)

// factory-Methode muss von konkreten Eiskugelautomaten impl. werden
protected abstract Eiskugel createEiskugel(String typ);

public Eiskugel getEiskugel(String typ){
eiskugel = createEiskugel(typ); // uns interessiert nicht, welche Art von Eiskugel wir bekommen
return ek;
}
}
public class StandardEiskugelAutomat extends EiskugelAutomat{
protected Eiskugel createEiskugel(String typ){
if(typ.equals("erdbeer")){
eiskugel = new ErdbeerEiskugel();
}
else if(typ.equals("schoko")){
eiskugel = new SchokoEiskugel();
} ...
return eiskugel;
}
}
public class DiätEiskugelAutomat extends EiskugelAutomat{
protected Eiskugel createEiskugel(String typ){
if(typ.equals("erdbeer")){
eiskugel = new DiätErdbeerEiskugel();
}
else if(typ.equals("schoko")){
eiskugel = new DiätSchokoEiskugel();
} ...
return eiskugel;
}
}

Abstract Factory Pattern

public abstract class Eiskugel{
//allgemeine Eigenschaften und Methoden
...
}
public class ErdbeerEiskugel extends Eiskugel{
public String toString(){
return "Erdbeereiskugel";
}
}
public class SchokoEiskugel extends Eiskugel{
public String toString(){
return "Schokoeiskugel";
}
}

public class EiskugelFactory{
Eiskugel eiskugel;
public Eiskugel createEiskugel(String typ){
if(typ.equals("erdbeer")){
eiskugel = new ErdbeerEiskugel(); //konkrete Implementierung!
}
else if(typ.equals("schoko")){
eiskugel = new SchokoEiskugel(); //konkrete Implementierung!
} ...
return eiskugel;
}
}

Delegate Pattern

Example

class A {
void f() { System.out.println("A: doing f()"); }
void g() { System.out.println("A: doing g()"); }
}

class C {
// delegation
A a = new A();

void f() { a.f(); }
void g() { a.g(); }

// normal attributes
X x = new X();
void y() { /* do stuff */ }
}

public class Main {
public static void main(String[] args) {
C c = new C();
c.f();
c.g();
}
}

Visitor Pattern

Advantage of the visitor pattern
- Easy to add new opeartions to the datastructure by adding new visitors: (see http://de.wikipedia.org/wiki/Besucher_(Entwurfsmuster))

Disadvantages of the visitor pattern
- New classes of datastructure elements (e.g. Body) requires to change all visitors, see also classes marked with * in example



Example:

interface Visitor {
void visit(Wheel wheel);
void visit(Engine engine);
* void visit(Body body);
void visit(Car car);
}

class PrintVisitor implements Visitor {

public void visit(Wheel wheel) {
System.out.println("Visiting "+ wheel.getName()
+ " wheel");
}
public void visit(Engine engine) {
System.out.println("Visiting engine");
}
* public void visit(Body body) {
System.out.println("Visiting body");
}
public void visit(Car car) {
System.out.println("Visiting car");
}

}

class DoVisitor implements Visitor {
public void visit(Wheel wheel) {
System.out.println("Steering my wheel");
}
public void visit(Engine engine) {
System.out.println("Starting my engine");
}
* public void visit(Body body) {
System.out.println("Moving my body");
}
public void visit(Car car) {
System.out.println("Vroom!");
}
}
interface Visitable {
void accept(Visitor visitor);
}

class Body implements Visitable{
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

class Car implements Visitable {

public void accept(Visitor visitor) {
visitor.visit(this);
engine.accept(visitor);
body.accept(visitor);
for(Wheel wheel : wheels) {
wheel.accept(visitor);
}
}

Hole example you find here:
http://en.wikipedia.org/wiki/Visitor_pattern

Thursday, November 01, 2007

Increase Heap space in Tomcat

set JAVA_OPTS=-Xmx1024m -Xms512m

Wednesday, September 26, 2007

change user in subversion

Delete files in the following directories and resync
../Subversion/auth/svn.simple
../Subversion/auth/svn.ssl.server
../Subversion/auth/svn.username

Tuesday, June 19, 2007

Add Java Exception breakpoint in Eclipse

1. Switch to debug perspective
2. Run
3. Java Exception breakpoint

Thursday, June 14, 2007

Java and Final

final is one of the most under-used features of Java. Whenever you compute a value and you know it will never be changed subsequently put a final on it. Why?
final lets other programmers (or you reviewing your code years later) know they don't have to worry about the value being changed anywhere else.
final won't let you or someone else inadvertently change the value somewhere else in the code, often by setting it to null. final helps prevent or flush out bugs. You can always remove it later.
final helps the compiler generate faster code.

Thursday, June 07, 2007

Arbeitsstation per Mausklick sperren

Um Ihre Arbeitsstation mit einem Mausklick zu sperren, erstellen Sie eine neue Verknüpfung, und geben Sie im Textfeld "Speicherort des Elements" folgenden Befehl ein:

rundll32.exe user32.dll,LockWorkStation

Wednesday, June 06, 2007

Check wether a String is a number

private boolean isNumber(String s) {
 for (int j = 0;j < s.length();j++) {
  if (!Character.isDigit(s.charAt(j))) {
   return false;
  }
 }
 return true;
}

Singleton pattern in Java

What is the Singleton Pattern, and when is it used?
The singleton design pattern is used when only one instance of an object is needed throughout the lifetime of an application. The singleton class is instantiated at the time of first access and the same instance is used thereafter till the application quits. To ensure that the user can create only one instance of this class, the following things have to be taken care of.

1. Make sure the user cannot explicitly create an instance of this object.
2. Provide access to this object from all areas of the application so that the need to create another instance never arises.

Uses:
The Singleton class can be used in various places where one would need a common repository of information that can be accessed from all objects in an application. A common example could be Preferences for the user. The preferences can be loaded from a file into memory into a singleton class which all objects in the application can then access to get preferences information.

See also: http://www.javareference.com/jrexamples/viewexample.jsp?id=25

Example:

public class Singleton{

private Singleton(){}

private static Singleton instance;

public static Singleton getInstance(){
if (instance == null)
instance = new Singleton();
return instance;
}

Friday, May 25, 2007

SPEED UP YOUR 9500 / 9300 /9300i COMMUNICATOR

1. Clear your Logs --> Desk-->Menu-->Tools-->Logs
Press Menu --> Tools --> Log Duration (SET THIS TO 1 /3 or 5 DAYS)

The default number is 30 days. Example your receive at least 20 calls
p/day + 20 outgoing calls p/day + 20 sms's (All this data gets
captured into this log) the following symptons appear with the phone:

Slow response of the telephone cover buttons ; out of memory errors;
if u press the green answer button; the bloody thing Answers a call
after 3/5 seconds later.

2. Change the Search Criteria for your contacts
goto contacts-->menu-->tools-->settings--> "SEARCH TAB"; turn COMPANY
& EMAIL -OFF.

Ideally nokia needs to release better firmware software for the
phone; at the moment this was tested with V5.22(01) , to my
knowledge, there is later beta software, SO PLEASE CHECK YOUR PHONE
BY PRESSING *#0000# then book your phone in if older; BACKUP YOU
PHONE BEFORE THE BOOKING because the upgrade will WIPE OFF EVERYTHING
IN THE PHONE MEMORY.

Zip / Unzip in the shell

zip -r test.zip *

unzip test.zip

 

Towel Day

From Wikipedia, the free encyclopedia (http://en.wikipedia.org/wiki/Towel_Day)

Jump to: navigation, search



Towel Day is celebrated every May 25 as a tribute by fans of the late author Douglas Adams. The commemoration was first held in 2001, two weeks after his death on May 11, and since then has been extended to an annual event. On this day, fans carry a towel with them throughout the day. The towel is a reference to Adams's popular science fiction comedy series The Hitchhiker's Guide to the Galaxy.

Wednesday, May 23, 2007

Wednesday, May 16, 2007

Tuesday, May 15, 2007

Nice examples of shell commands for OSX

http://www.westwind.com/reference/OS-X/commandline/admin.html

Useful shell commands for OSX: pbcopy and pbpaste

Try “history | pbcopy” in the shell and “APPLE-V” in your favourite editor.

 

There is the also the opposite command: pbpaste

Useful resources for working with VIM on MAC-OSX

http://wiki.macvim.org/wiki/

Linux: Find command ssh in your history

history | grep -i ssh

Thursday, May 03, 2007

Upserts in Oracle

MERGE <hint> INTO <table_name>
USING <table_view_or_query>
ON (<condition>)
WHEN MATCHED THEN <update_clause>
WHEN NOT MATCHED THEN <insert_clause>;

 

Source: http://www.psoug.org/reference/merge.html

Wednesday, May 02, 2007

UML FAQ: Association vs. Aggregation vs. Composition

Example:
Account -> Transaction, should be association, because they can live independently
(XMI: aggregation="none")

Order -> Order Line, should be aggregation, because the part cannot live without the whole.

(XMI: aggregation="shared")

Navigability is important when coding:
In the Account case, it means it will had an attribute of type Transaction.

Last point about aggregation, is in case when it is a composition, the whole will be responsible for the construction/destruction for the part also. It means there would be works for the whole to do.

(XMI: aggregation="composite")

Source: http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?forum=346&message=13776783&thread=101811&cat=24

Tuesday, April 17, 2007

BEA Weblogic: What to to do if the deployment is becoming slower and slower?

- Important: Make a copy of directory /myserver
- Delete directory myserver
- Reconfigure Users and Groups

Wednesday, April 11, 2007

How to call ant from a batch file

example:

call ant clean
call ant products
call ant whatever
cd "C:\Programme\Internet Explorer"
c:
iexplore http://www.google.com

Tuesday, April 10, 2007

EJB-QL using BEA Weblogic

Define Queries in "ejb-jar.xml"
Define Mapping in "weblogic-cmp-rdbms-jar.xml

How to select the first table row in Oracle?

select * from table where rownum = 1

Grep & Find

Find all files which contains " test ":
grep -lri "[[:space:]]test[[:space:]]" *

Find all files with the name test
find . -name "test"

Tuesday, March 20, 2007

ps

User processes: ps -ux
All processes: ps -aux
kill -9 processNr

Tar

tar -cvf directory/my.tar directory1 directory2 directory3

ssh & scp

ssh -l username servername
scp * username@servername:/directory

Friday, February 23, 2007

Tuesday, January 23, 2007

Tuesday, January 16, 2007

Send mails from a Linux/Unix Shell

mail -s "my header" mymail@mydomain.com < myfile

Monday, January 08, 2007

Command nohup in Linux

nohup

Funktion:
nohup läßt ein Programm die Signale SIGHUP SIGINT SIGQUIT und SIGTERM ignorieren

Syntax:
nohup Kommando [Argument ...]
Beschreibung:
nohup schützt ein Programm vor den HANGUP-Signalen. Dadurch kann es im Hintergrund weiterlaufen, auch wenn der Benutzer sich ausloggt. Normalerweise würden mit der Loginshell alle Prozesse des Anwenders durch ein SIGHUP beendet.