Friday, 20 July 2012

"Gang of Four" design paterns

There are times when you find something on the web and you go are that's useful! There are several reasons for this revelation, in this case it is a reminder that there are good ways and bad ways to write your code.

This article is one of those salutary reminders that we can all do better. So I have posted a link to the here with a short synopsis so I can find it when I need to be reminded.


Design patterns provide solutions to common software design problems. In the case of object-oriented programming, design patterns are generally aimed at solving the problems of object generation and interaction, rather than the larger scale problems of overall software architecture. They give generalised solutions in the form of templates that may be applied to real-world problems.
Design patterns are a powerful tool for software developers. However, they should not be seen as prescriptive specifications for software. It is more important to understand the concepts that design patterns describe, rather than memorising their exact classes, methods and properties. It is also important to apply patterns appropriately. Using the incorrect pattern for a situation or applying a design pattern to a trivial solution can overcomplicate your code and lead to maintainability issues.

The Gang of Four are the authors of the book, "Design Patterns: Elements of Reusable Object-Oriented Software". This important book describes various development techniques and pitfalls in addition to providing twenty-three object-oriented programming design patterns. The four authors were Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides.

The full article by , is published at http://www.blackwasp.co.uk/GofPatterns.aspx but the types of patterns he describes are listed here:

Creational Patterns

The first type of design pattern is the creational pattern. Creational patterns provide ways to instantiate single objects or groups of related objects. There are five such patterns:
  • Abstract Factory. The abstract factory pattern is used to provide a client with a set of related or dependant objects. The "family" of objects created by the factory are determined at run-time.
  • Builder. The builder pattern is used to create complex objects with constituent parts that must be created in the same order or using a specific algorithm. An external class controls the construction algorithm.
  • Factory Method. The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.
  • Prototype. The prototype pattern is used to instantiate a new object by copying all of the properties of an existing object, creating an independent clone. This practise is particularly useful when the construction of a new object is inefficient.
  • Singleton. The singleton pattern ensures that only one object of a particular class is ever created. All further references to objects of the singleton class refer to the same underlying instance.

Structural Patterns

The second type of design pattern is the structural pattern. Structural patterns provide a manner to define relationships between classes or objects.
  • Adapter. The adapter pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client.
  • Bridge. The bridge pattern is used to separate the abstract elements of a class from the implementation details, providing the means to replace the implementation details without modifying the abstraction.
  • Composite. The composite pattern is used to create hierarchical, recursive tree structures of related objects where any element of the structure may be accessed and utilised in a standard manner.
  • Decorator. The decorator pattern is used to extend or alter the functionality of objects at run-time by wrapping them in an object of a decorator class. This provides a flexible alternative to using inheritance to modify behaviour.
  • Facade. The facade pattern is used to define a simplified interface to a more complex subsystem.
  • Flyweight. The flyweight pattern is used to reduce the memory and resource usage for complex models containing many hundreds, thousands or hundreds of thousands of similar objects.
  • Proxy. The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object. The proxy provides the same public interface as the underlying subject class, adding a level of indirection by accepting requests from a client object and passing these to the real subject object as necessary.

Tuesday, 19 June 2012

Using Apache-IVY : Missing dependency

This is just in case I loose this reference.
I had an error with my Ivy dependencies which reported "org.eclipse.persistence#org.eclipse.persistence.jpa;2.2.0: not found". It took me a while to find it but this post on Stack-Overflow helped.
It means that the standard Maven repositories do not hold this JAR so I have connected to the Eclipse Maven repository @ http://download.eclipse.org/rt/eclipselink/maven.repo/.

How?

Well my ivysettings.xml has this setting in it <ibiblio name="Eclipse-libraries"   m2compatible="true" root="http://download.eclipse.org/rt/eclipselink/maven.repo/" /> and looks like this:

<!--
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.   
-->
<ivysettings>
    <properties file="${ivy.settings.dir}/ivysettings.properties"/>
   
    <settings defaultResolver="embedplaysafe" />
   
    <!-- Where the Cache is placed -->
    <!-- <caches defaultCacheDir="${ivy.settings.dir}/ivy-cache" /> -->
    <caches defaultCacheDir="${defaultCache.dir}" />
   
    <resolvers>
        <!-- Path is important as this is the final repository -->
        <chain name="embedplaysafe"
               returnFirst="true"> <!-- if a revision is found in the filesystem then ivyrep will not be queried -->
            <!--
            It is a filesystem resolver, so looks at a directory structure to retrieve the artifacts.
            This one is configured to look in the repository sub directory of the directory that contains the ivysettings.xml file
             -->
            <filesystem name="local">
                <artifact pattern="${repository.dir}/[type]/[artifact]-[revision].[ext]" />
                <ivy pattern="${repository.dir}/[type]/[module]-[revision].xml" />
            </filesystem>
            <!--  See http://www.buildmeister.com/articles/escape_from_jar_hell_with_apache_ivy_the_agile_dependency_manager -->
            <!-- 
            <url name="buildmeister"> 
               <ivy pattern="http://m2.buildmeister.com/[module]/[revision]/ 
                ivy-[revision].xml" /> 
               <artifact pattern="http://m2.buildmeister.com/[module]/[revision]/ 
                [artifact]-[revision].[ext]" /> 
            </url>
            -->
            <!--  See http://stackoverflow.com/questions/1033859/ivy-via-nexus-proxy  -->
            <!-- It looks in the ibiblio maven repository to retrieve the artifacts.  -->
            <!--   -->
            <ibiblio name="uk-libraries"         m2compatible="true" root="http://uk.maven.org/maven2" />
            <ibiblio name="global-libraries"     m2compatible="true" usepoms="true" />
            <ibiblio name="Eclipse-libraries"   m2compatible="true" root="http://download.eclipse.org/rt/eclipselink/maven.repo/" />
          
        </chain>
    </resolvers>

</ivysettings>


I hope this helps you.

Monday, 18 June 2012

Java 7 classpath broken on Windows XP

Please file under "for F*cks sake!!!"

It seems that the ability to assign a wild card as a class path when setting up a Java comand-line script is broken for Java 1.7 and windows users (On XP certainly).

Previously you could type .... java -cp "somewhere/*"
WHICH would do the trick now you have to add a magic character to make it work.
SO the command becomes java -cp "somewhere/*;"

Just file under GRRRR!

Or see this handy post by someone far less annoyed than I am.
http://stackoverflow.com/questions/9195073/broken-wildcard-expansion-for-java7-commandline-on-windows7

Thursday, 31 May 2012

Version Numbering


So many times I hear developers arguing about how to label versions so here is the "right" way to do it ... well a widely accepted way!

Software should be released with a version number based on the Apache Portable Runtime Project version guidelines. In summary, the version is denoted as three integers in the format of MAJOR.MINOR.PATCH.

Major versions are incompatible at the API level, as they can include any kind of change.
Minor versions are compatible with older versions at the API and binary level, and they can introduce new functions or deprecate old ones.
Patch versions are perfectly compatible, and they are released to fix defects.

Here are some examples to demonstrate the compatibility:
Original VersionNew VersionCompatible?
2.2.32.2.4Yes
Compatibility across patch versions is guaranteed.
2.2.32.2.1Yes
Compatibility across patch versions is guaranteed.
2.2.32.3.1Yes
Compatibility with later minor versions is guaranteed.
2.2.32.1.7No
Compatibility with prior minor versions is not guaranteed.
2.2.33.0.0No
Compatibility with different major versions is not guaranteed.
2.2.31.4.7No
Compatibility with different major versions is not guaranteed.
Note: while some of the cells say "no", it is possible that the versions may be compatible, depending very precisely upon the particular APIs used by the application.

Patch Versions

To retain perfect source and binary compatibility, a patch release can only change function implementations. Changes to the API, to the signatures of public functions, or to the interpretation of function parameters is not allowed. Effectively, these releases are pure bug fix releases.

Minor Versions

Minor releases can introduce new functions, new symbolic and enumerated constants, and deprecate existing functions. In practice, this will not be a problem since upgrading to newer versions is always safe.

Major Versions

Any kind of change can be made during a major version release. Particular types of changes that might occur:
  • remove or change constants
  • remove (deprecated) functions
So you might find that your code stops working completly.

Minor Versions

Minor Versions
Minor 

Data Replication for Stores

(Extract from an article on Oracle's Blog)

A tough problem for store systems is reliably moving data between the home office and stores in both directions. There are many solutions to this problem, each with its own positives and negatives. One solution is using the open-source software SymmetricDS, which was created for this very problem during the Oracle Retail POS implementation at BigLots. The original developers formed JumpMind, a company specializing in helping retailers use open-source to solve difficult problems.

SymmetricDS is an open-source, web-enabled, database independent, data synchronization software application. It uses web and database technologies to replicate tables between relational databases in near real time. SymmetricDS is database agnostic and can even synchronize data between different types of databases.
The idea of  SymmetricDS came from the need to synchronize many small, inexpensive databases at stores to a single, large, more expensive one at a corporate location. We needed something that would trickle sales data over a slow network and survive outages

It is possible to host SymmetricDS under Tomcat but it runs very nicely under Jetty as a single command line task.

(More..) (And on facebook...)

Log4j - Tricks

This post will grow as I find more little tricks and gotchas...

  1. Changing the timestamp format in the appender.
    Add %d{dd MMM yyyy HH:mm:ss,SSS} sooner than just %d and then edit the bits out you don't like.

Log4j manual...

Wednesday, 23 May 2012

"Cmd here" in windows 7

In Win XP I found very useful the power toy CmdHere to open a command window in the current folder.

Q: So what is the equivalent in Windows 7?
A: Hold the Shift key down while right-clicking.  You should see an entry "Open Command Window Here" appear.