Singleton pattern – the wrong way

I’m currently maintaining application that I inherited from a colleague. So in one of his classes he wanted to implement singleton pattern. Somehow, he failed. I don’t know how, because singleton is one of the easiest design patterns around. So let’s start with a little bit of theory.

According to “GOF” the purpose of singleton is to restrict the instantiation of a class to one and only one object. In order to achieve this, we have to prevent others to access our constructors by declaring them private. This was his first mistake. He has a public constructor, ergo, anyone can instantiate this class.

The second step is to create a method that will return that one object. This is his implementation:

public static Client getInstance()
 {
 return instance;
 }

What is wrong with this? Well, for one thing, he assumes that instance is not null. But there is no base for that. Because he hasn’t created it until he called init() method.

So, here is my question. How do you call init() method if you haven’t instantiated the class. And you can’t call the instance of class through getInstance() method because you haven’t started init() method. And then I saw the ugliest singleton call ever. There it was at the beginning of main() method. And it looked like this:

public static void main(String[] args)
 {
 Client applet = new Client();
 ...
 }

The very thing we are trying to avoid is at the beginning of application. I will not give you the code for correct implementation of singleton. It can be found all over the Internet. Just check Wikipedia.

Posted in Pattern, Programming | Tagged , , | Leave a comment

Strategy VS Bridge pattern

Recently, I had to explain difference between strategy and bridge pattern on several occasions. So for further notice, I thought about writing a little article about the subject. If you Google (Bing) it, you could find quite a lot of articles about each, but not so much about their similarities and differences.

If we look at UML diagrams of these patterns, it seems identical. However, UML diagram is not a pattern.

Bridge pattern - UML diagram

Bridge pattern - UML diagram

Strategy pattern diagram

If you do not know, or you are not certain what is pattern, I would suggest the following link[http://en.wikipedia.org/wiki/Design_pattern_(computer_science)] for start, and than several books on the subject. Starting with this one.

So what is the key difference? Let’s start with Problem.

Bridge: The derivations of an abstract class must use multiple implementations without causing an explosion in the number of classes

Strategy: The selection of an algorithm that needs to be applied depends upon the client making the request or the data being acted upon.

As you can see, the problem they solve is different. It is very important to notice that bridge is structural pattern, and strategy behavioral, and in that way they solve their respective problems. While strategy allows clients to be independent from algorithm they need (behavioral), bridge is making sure that abstraction and implementation are separated (structure).

Possibly the best way to explain it is to say that in strategy pattern, client(or context in upper image) is the one responsible for working/choosing strategy. In bridge pattern abstraction is not aware of any implementation. The work is delegated to refined abstractions.

You should think of bridge pattern when you have two structures that criss cross each other in one structure. For example, let’s say you have abstract class Control with implementations like Button and TextBox. And you want to add methods Draw. You will offcourse, add this method to abstract class. Then you will realise that this can be extracted to another class Draw with implementations like DrawActive and DrawDisabled. This is where Bridge pattern comes in. Instead of mixing all these classes you mix only abstractions.

Posted in Programming, database | Tagged , , , | Leave a comment

Shared One note 2007

I’ve been playing with Microsoft One note 2007 for quite some time now. I love the no save function and write anywhere. If you have pencil it can recognize your handwriting. You can even insert audio and video in it, and make it searchable. Lately I’ve been talking with some of my colleagues, about sharing one note notebooks. We could have done it through shared folders, but it is so much better to use some Internet service for it.

So what is better than Microsoft’s own officelive.com? There you can create a free account for your small business, and you have 5 GB of space. It is easy to add your own domain to it and it is connected with your msn mail.

But it looks like it isn’t so easy to integrate two services. Maybe there is another way, but I failed to find it.

First you have to crate a folder on officelive.com in which one note will be saved. Here is how you can do it:
1. Go to  Business applications

Select business applications

Select business applications

2. Create a new application

Create a new application

Create a new application

3. Make sure it is Document Manager type of application

Select type of application

Select type of application

4.  There you should create a new folder for your one note notebook. I’ve crated a one note folder folder.

Make a new folder

Make a new folder

This should be all you have to do in your browser.

Now we go to One note 2007.

1. First go to Share->Create Shared Notebook…
2. Give a name to your new shared book and select a template, then click Next

Create a shared notebook

Create a shared notebook

 

Give a name to shared notebook
Give a name to shared notebook

3. Select Multiple people will share the notebook and sub section On a server. It would be even better if you had your own sharepoint portal.

Choose type of sharing
Choose type of sharing

4. Insert path to your officelive (or your sharepoint portal). it should be something like http://usernameofficelive.officelive.com/ or something like that.  Than click Browse button.

inserting notebook location
inserting notebook location

5. Doubleclick your application, Document Library and folder. Than click select.
6. After that you should be returned to wizard where you will click on Create button.

And that is all you have to do to have a shared one note notebook on internet.

What about you? Do you have any good advice on one note + office live combination?

Posted in Uncategorized | Tagged , , | 1 Comment

Simple error with crystal reports

I’ve been working on update of one of our projects written in Java. The IDE is JDeveloper, and for reporting we are using Crystal Reports.

One of the tasks I had was adding a new report. Since we have many other reoprts, I decided to copy->paste (I know, make reusable code…) the code and make a modification in path to report. Though it is very simple task, I’ve made a mess. Without any apparent reason i’ve been receiving an error saying Java.Lang.Integer. What is this? I’ve never had and error like this. As a matter of fact, this doesn’t look like exception at all. So, I figured, there must be something wrong with report. My coleague went through entire report and found nothing. Sorry Ilija.

Finaly, I’ve discovered a bug. Path to report was wrong. Instead of “../report.rpt” I had jus “report.rpt”. Two dots made my app to return that stupid Java.lang.Integer message. One little change and build was all I had to do to fix this stupid error. I still do not know where error came from, Java or Crystal.

Hope this will help someone.

Posted in Programming | Tagged , , | Comments Off

Great wiki site

I’ve just stumbled on this great site: The Net Objectives Design Patterns Repository
of course, it is part of my Bookmarks now.

It all started as material from Scott Bain’s book Emergent Design: The Evolutionary Nature of Professional Software Development, which I highly recommend.

It contains some of the most used software design patterns. Even more important, it is a wiki, so you can add your own patterns. Registration form, which requires some personal info, is here. but I believe that it is worth giving some personal info to gain access to resources and webinars at netobjectives.com

I like the structure they have chosen to use for patterns pages.  It is simple, yet informative and clear. I hope there will be more patterns anytime soon.

Posted in Pattern, Programming | Tagged , , | Comments Off

Google’s new search box

I stumbled on this post on official Google blog, and couldn’t stop laughing when I read this:
“… our search box is growing in size. Although this is a very simple idea and an even simpler change, we’re excited about it — because it symbolizes our focus on search and because it makes our clean, minimalist homepage even easier and more fun to use. …”

Can anyone explain to me, what is so fun in bigger search box? When I type a search query I am not having fun. I am searching for something! Maybe I am searching for something fun, but search itself is not fun. And it is not getting funnier with bigger search box.

Since it is posted by Marissa Mayer, Vice President, Search Products & User Experience, I’ll just assume it is for market. Still who falls for that?

P.S. Funny thing just happened. I went on http://www.google.com  and the search box is still the same size as before. I guess they don’t want Opera users to have the same experience as chrome/IE/Firefox users.

Posted in Uncategorized | Tagged , | Comments Off

Google blocked my AdSense account

So, for some (for now) unknown reason Google has blocked my AdSense account. From what could be read on their support page, most probable reason for this are invalid clicks. Dear readers, please do not click on advertises if you are not genuinely interested in it. The only reason I have added AdSense is to see if the site could pay itself, that is if in a year the site could earn enough to pay to my ISP for hosting.

I have filed a complain on their contact form. We’ll se if they will even bother checking it out. Any ideas why this happened is appreciated.

Posted in Uncategorized | 2 Comments

Microsoft will discontinue Oracle Data Provider for .NET

Two weeks ago, Microsoft announced on ADO.NET blog it will discontinue System.Data.OracleClient. As they say in this blog post most of Microsoft’s Clients use third party providers.

System.Data.OracleClient will be available in .NET framework 4, but labeled as deprecated. This will not influence old applications in any way, and those should continue to work as usual.

However, developing new applications targeting .NET framework 4 will generate warnings, but not errors. This means that new applications could be developed with OracleClient provider, but not recommended. Once compiled, warnings will not be generated when running these applications. There are several Microsoft partners who develop providers for Oracle database. Among them are DataDirect Technologies and  Devart, as well as Oracle itself. However, only Oracle data provider is free.

If most Microsoft’s customers are using third party providers, then this is very logical move. Why would Microsoft waste money on development of something nobody uses?

Microsoft will work on fixing bugs and critical updates according with their support policy for the .NET 4 framework.

Posted in database | Tagged , , | Comments Off

Visual studio TS 2010 will support Oracle and DB2

Back in February on VsLive conference in San Francisco, Microsoft announced Oracle Database Schema Provider (DSP) with Quest Software.The DSP will give developers using Visual studio team system edition ability to work with Oracle database without limitations. If you want to keep up with latest news, check out http://www.teamfuze.net/ .

Microsoft has already started similar project with IBM on creating DSP for DB2 database. Before using this, you might want to check out this FAQ.

Now that Microsoft has acquired support for three major database vendors, the next logical thing is support for open source databases. MySQL and Postgre are far from implementation. Perhaps for VS 201x?

Posted in Programming | Tagged , , , | Comments Off

Hacking .NET framework

2BSecure’s Application security dept. Manager Erez Metula has discovered a way to replace any .NET module and change .NET framework behaviour. This method is not specific only on .NET, because it can, with slight modification, be easily applied on Java Virtual Machine. He even created a tool .Net-Sploit 1.0 which allows modification of .NET. The only problem for the attacker is that he/she must have total control of a machine before this tool can be used. However, once used, possibilities are endless.

Microsoft is already informed about this. Actually, MS was notified in September 2008, but they refused to consider it as vulnerability, rather weakness.

Complete instructions and examples can be found in this whitepaper.

Posted in Security | Tagged , | Comments Off