JetCracker

Life-time learner's blog

Tag Archives: software

Java REST client for Android

In one of my previous posts I told you that I started learning how to code for Android, and created my first Android app. But most of mobile apps are useless when they do not exploit the advantages of the Internet. That is why in order to create a good application you should know how to use the power of the Internet in your app.

There are different ways of how your application is interacting with the Internet and its resources, for instance an app can download data, connect to remote databases, but the most popular way of interaction is RESTful services  (in the development of mobile apps). It just works more effectively than SOAP when the bandwidth is limited.

In this article I’ll show briefly how to create a client for a RESTful service in Android (this should also work in any device supporting Java). Read more of this post

Advertisements

[Java Web] [QapTcha] How to add captcha to JSF page

If you want to have prevent spam in your website, you probably need to use captcha.

CAPTCHA (play /ˈkæp.tʃə/) is a type of challenge-response test used in computing as an attempt to ensure that the response is generated by a human being. The process usually involves a computer asking a user to complete a simple test which the computer is able to grade. (Wikipedia)

Early CAPTCHAs such as these, generated by the...

You can see the examples of captchas on the pictures. Captcha is intended to protect the web site from spam, and it helps to avoid “unwanted” registrations, block robots. But it can also deter some “proper” users. As for me, I hate recognizing captchas!

What we need is a simple and user-friendly mechanism to detect robot. Fortunately there are plenty of different solutions for captchas on the internet!

Take a look at QapTcha – a simple, easy-to-use and intuitive captcha system. It needs human action instead of to read a hard text and it is a very lightweight jQuery plugin.

QapTcha Demo Screenshot

All you need is to drag the button from one side to another, and that makes it perfect for mobile devices.

Here I will show you an example of how we can embed this captcha into JSF web-application.

Read more of this post

[Java] [Glassfish 3] Wrong character encoding in multipart servlet request

Hello!

It’s been a long time since I posted here, but I’m currently very busy with my study, work and projects.
Recently we had a problem with processing multipart HTTP requests in Java servlet. We are using Glassfish 3.1.1 as application server. The thing is despite of the fact that HTTP request had UTF-8 encoding, all string data actually were in ISO-8859-1 (a.k.a. ISO-LATIN-1).

I tried several different approaches to set encoding to UTF-8… until I came across the following: http://java.net/jira/browse/GLASSFISH-18516. As I understood, It says that there is a known issue in Glassfish 3.1.2 with incorrect interpretation of character encoding of multipart HTTP request parameters. Instead of UTF-8 it applies ISO-8859-1 ignoring settings in configuration files (web.xml, glassfish-web.xml) and in HTTP request. Turns out, this issue also affects Glassfish 3.1.1, which is used in our project.

Read more of this post

Presentation: Software design patterns

Hello.

For my previous English class a had a task to create a presentation on design. This topic was pretty hard for me because I am not very good at describing design and I am not an expert in it!

I’ve spent nearly 2 hours just to choose what I wanted to speak about in my presentation. And finally I came up with an idea to create a presentation about software design patterns. I know, it is rather boring topic to speak about because is quite technical, but I’m just know about it way more than I know about design of furniture or design of cars.

Presentation: Software Design Patterns

Good morning everyone. My name is Anton. I’m a student of MIPT. Today in my presentation, I’m going to speak about Software Design patterns. My presentation will take about 15 minutes. Feel free to interrupt me and ask a question.

The plan of my presentation is shown on the slide. Let’s start!

Design Patterns

Simplicity is the soul of efficiency.
— Austin Freeman, “The Eye of Osiris”

The first thing I want to tell you about is what a software design pattern is. Does anyone know the answer to this question?

In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn’t a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

Now that we know what it is we can move on to the question “Why do we use design patterns?”

Why do we use design patterns?

Design patterns can speed up the development process by providing tested, proven development paradigms. Effective software design requires considering issues that may not become visible until later in the implementation. Reusing design patterns helps to prevent subtle issues that can cause major problems in the future.

In addition, patterns allow developers to communicate using well-known, well understood names for software interactions. More than that, common design patterns can be improved easily.

Also, design patterns improve code readability for coders and solution architects. The code becomes understandable and maintainable.

So, the design patterns is actually a good thing! And there are over 30 patterns to solve almost any problem. This brings me to the next point.

Classification of design patterns

There are a lot of design patterns and they all are divided into four classes: creational, structural, behavioural, and concurrent.

Creational design patterns

These design patterns are all about class instantiation. This pattern can be further divided into class-creation patterns and object-creational patterns. While class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation effectively to get the job done.

Structural design patterns

These design patterns are all about Class and Object composition. Structural class-creation patterns use inheritance to compose interfaces. Structural object-patterns define ways to compose objects to obtain new functionality.

Behavioural design patterns

These design patterns are all about Class’s objects communication. Behavioural patterns are those patterns that are most specifically concerned with communication between objects.

Concurrent patterns

These patterns help to control the execution flow of the applications and provide solutions to problem of concurrent access.

Now, lets have a look at an example of a simple but very useful creational design pattern – Singleton

SINGLETON

Intent

  • Ensure a class has only one instance, and provide a global point of access to it.
  • Encapsulated “just-in-time initialization” or “initialization on first use”.

Problem

Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

Discussion

Singleton should be considered only if all three of the following criteria are satisfied:

  1. Ownership of the single instance cannot be reasonably assigned
  2. Lazy initialization is desirable
  3. Global access is not otherwise provided for

The example of the implementation of Singleton in Java you can see on the slide.

The main disadvantages of this implementation are that deleting a Singleton class/instance is a non-trivial design problem and this implementation is not thread-safe.

OK. Design patters are good. But what about the other side of the coin? Let’s  have look at its bad points.

Criticism

The concept of design patterns has been criticized by some in the field of computer science.

Targets the wrong problem

The need for patterns results from using computer languages or techniques with insufficient abstraction ability. Under ideal factoring, a concept should not be copied, but merely referenced. But if something is referenced instead of copied, then there is no “pattern” to label and catalogue. Paul Graham writes in the essay Revenge of the Nerds.

Peter Norvig provides a similar argument. He demonstrates that 16 out of the 23 patterns in the Design Patterns book (which is primarily focused on C++) are simplified or eliminated (via direct language support) in Lisp or Dylan.

Lacks formal foundations

The study of design patterns has been excessively ad-hoc, and some have argued that the concept sorely needs to be put on a more formal footing. At Oopsla 1999, the Gang of Four were (with their full cooperation) subjected to a show trial, in which they were “charged” with numerous crimes against computer science. They were “convicted” by ⅔ of the “jurors” who attended the trial.

Leads to inefficient solutions

The idea of a design pattern is an attempt to standardize what are already accepted best practices. In principle this might appear to be beneficial, but in practice it often results in the unnecessary duplication of code. It is almost always a more efficient solution to use a well-factored implementation rather than a “just barely good enough” design pattern.

And final bad point is that design patterns are very bad when they are used improperly or overused. So, DO NOT OVERUSE IT!

Let’s move on to my next point which I want to cover in my presentation.

Anti-patterns

Anti-patterns are complete opposite to patterns. Patterns, as you now know, are the examples of good programming practice. In contrast, anti-patterns are the examples of mistakes, which are made in solving different problems.

Here are the most common anti-patterns:

  1. Copy and Paste Programming
  2. Spaghetti Code
  3. Golden Hammer
  4. Magic Numbers
  5. Hard Code
  6. Soft Code
  7. Reinventing the Wheel
  8. Reinventing the Square Wheel
  9. Blind Faith
  10. Brute Force Coding
  11. Boat Anchor
  12. God Object
  13. Unnecessary comments
Choose the one you like, and I’ll explain what this anti-pattern means.

Conclusion

Now, I’d like to make a conclusion to my whole presentation. Let’s go over the main points.

  1. Design pattern is a general solution to a commonly occurring problem in software design
  2. Using design patterns makes your code better
  3. Design patterns are divided into:
    • Creational
    • Behavioral
    • Structural
    • Concurrent
  4. Do NOT OVERUSE design patterns
  5. Creating your software avoid anti-patterns.

Sources:

Here you may find all about software design patterns and anti-patterns.

  1. http://sourcemaking.com/design_patterns
  2. http://en.wikipedia.org/wiki/Software_design_pattern
  3. http://habrahabr.ru/blogs/refactoring/59005/

Thank you very much for your attention! 🙂

Download my presentationSoftware Design Patterns (.pps, 500 kB)

If you find any mistake, let me know. Good luck with your English.

Anton Danshin

P.S. My presentation should have been shorter. But it is not possible to cover this topic in 10 minutes! Sorry, my dear teacher. I should have also learn the speech by heart, but we had too little time to get prepared.  🙂