Skip to main content

Posts

Servant (Design Pattern) in Java - example

The servant design pattern - or better idiom is used to provide the functionality (methods) to some group of objects. This functionality is common for all these object and therefor should not be repeated in every of these classes. The object, which should be served is passed to the method of servant as a parameter. All the served objects should implement common interface - in this particular example IMovable interface. Also the type of argument passed to the servand method is of type IMovable . The servant in this example is used to move objects from one position to another. In real life application these methods should change the position of object in small steps so that the final change would look like smooth movement (animation). In my servant method, only some message are printed instead for demonstration. IMovable interface: package com.shimon.servant; import java.awt.Point; /** * Movable interface * @author shimon * */ public interface IMovable { public void setPos...

Java Crate (design pattern/idiom) example

Another example, based on example explained in the book "Navrhove vzory" (Design patterns) from Rudolf Pecionvsky . I have re-made this example just to somehow get more familiar with this design pattern (or better idiom). The "crate" is used to store the set/list of object in one place, so that the moving (passing) these objects is easier. The example from the book is very easy, and helps to understand, how this design pattern could be applied to som very usefull application (e.g. day planner) Code example: package com.sim.crate.common; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; /** * The Day Plan class demonstrates the usage of crate to create simple day plan, with items that do not collide. * @author shimon * */ public class DayPlan { private final List actions = new ArrayList (); /** * Tries to add an item to the day plan with entered start, end time and duration. Returns true, if * the try was successf...

Immutable object example

After reading some chapters of  the book "Design patterns" (Navrhove vzory) from czech author Rudolf Pecinovsky , I decided to test my own examples of usage of the design patterns mentioned in this book. In this first post I will publish here a code that is an example of usage of the immutable objects. As you maybe already know - the immutable objects cannot be changed after their creation. That can be very useful in some situation. In this particular example (which is kind of a english copy of the czech example mentioned in the book "Navrhove vzory") the object, which represents fraction and its operations is created. The main thing here is, that during every operation (method) new instance of class Fraction is created instead of modifying the existing Fraction instance. Immutable objects can be very useful in concurrent applications - their state cannot be changed, so there is no way they could be corrupted by other threads. Code example: package com.shi...

Creating trigger for DB2

Recently I was solving a problem, where a trigger for DB2 database was needed. Using few of available tutorials on the internet, I started to build my first DB2 trigger. Everything seemed to be easy understand, until my first test of the trigger. After couple minutes of googling I have eventually discovered what did the mysterious error in the console mean. The problem was in termination character for the SQL query. As the query, which should be ran after triggering event has to be terminated with a semicolon, the trigger query itself has to be terminated with something else apparently - so that the SQL engine won't start the execution after first semicolon it sees after the action query. Solution The solution is quiet simple - change the termination character. --#SET TERMINATOR @ CREATE TRIGGER trg1 BEFORE INSERT ON table FOR EACH ROW BEGIN ATOMIC VALUES 1; --here you can see the semicolon, which normally causes problems :) END @ --SET TERMINATOR ; ...

Jesenné Tatry 2011

O tom, že jeseň v horách - a to dokonca v slovenských najvyšších horách - nemusí byť len sychravá, upršaná a šedivá sme sa presvedčili počas uplynulého víkendu spolu so Zuzkou, mojimi kolegami a kamarátmi. Výhľad na všetky svetové strany, boľavé nohy a opálené tváre sú toho dôkazom. Väčšina z nás už skúsenosti s Tatrami mala, avšak aj tak sme to s túrami nechceli príliš prehnať (v pondelok predsa len treba ísť opäť do práce). Ako jedného z najlepších kandidátov na túru sme vybrali tatranskú klasiku - Rysy Rysy Ubytovanie na Štrbskom plese nám zabezpečilo pohodlný ranný začiatok túry, nakoľko sme na izbu prišli už v piatok večer. V sobotu okolo osmej sme už šľapali po ceste na Popradské pleso. Túto cestu pozná snáď každý, kto podnikal nejakú tú túru do Tatier. Ako každá túra na Rysy aj táto bola vďaka nádhernému počasiu masovou akciou. Húfy ľudí sa hrnuli strminou na Chatu pod Rysmi, o vrchole ani nehovorím. V momente keď sme dorazili na vrchol to na ňom vyzera skôr ako na bidle, na ...

Resolution configaration using xrandr - Ubuntu

I was recently solving a problem with my secondary monitor screen resolution. I have 24'' 16:9 widescreen LCD monitor but the maximum resolution available in GUI monitor config tool was only 1680x1050. What I needed was 1920x1080 to make my monitor even sharper. After some googling I have found this Ubuntu Wiki page which describes many possible display configurations using the xrandr command line tools. Xrandr enables you to add new resolution mode for your monitor. To add new mode use this command: xrandr --newmode "1920x1080_60.00"  172.80  1920 2040 2248 2576  1080 1081 1084 1118  -HSync +Vsync xrandr --addmode VGA-0 1920x1080_60.00 After that, you can easily set the new resolution mode for your output device (in this case name of the device is VGA-0) xrandr --output VGA-0 --mode 1920x1080_60.00 You can do also other stuff with xrandr command line tool (such as setting primary monitor xrandr --output VGA-0 --primary ) I would recommend to use this comm...

Moravský kras a Macocha II

O krásach Moravského krasu a najmä priepasti Macocha som sa už rozširoval v tomto príspevku . Počas návštevy mojej sestry v Brne sme sa boli na Macochu a Punkevní jaskyňu pozriet znova. O to väčšie bolo naše sklamanie, keď sme si vyšlapali cestu od zastávky Skalní mlýn až nad priepasť macocha a zistili sme, že pozrieť sa do Macochy už nie je také jednoduché ako minulý rok. Nebránil nám v tom strach z výšky, hĺbky alebo čosi podobné.  Prístup k zábradliu vrchnej lávky nad priepasťou macocha bol blokovany reťazou a brigádnikom vyberajúcim 20 Kč za nazretie do tohto krasového útvaru.  Na retiazke sa krásne vynímal nápis "Pozemek obce Vílemovice". Nebránim sa plateniu vstupného do jaskýň alebo samotnej priepasti, ale zmysel tohto poplatku mi akosi uniká.