Knowledge7

The Linux and Mobile Solution Provider

  • About
  • Training
  • Services
  • Clients
  • In the news
  • Blog
  • Contact Us
You are here: Home / Topic / Design Patterns in Java

Design Patterns in Java

This topic is part of our Object-Oriented Programming in Java training

Once someone understand sound object-oriented analysis, design and programming principles, typically by reading a good book on Object-Orientation (such as Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development by Craig Larman) and programming real applications a lot, he/she can then embark upon mastering design patterns.

A Design Pattern is a solution which experienced software developers have been using over the years when they have to solve a given type of problem. Useful design patterns include:

  • creational patterns like Abstract factory and Singleton,
  • structural patterns like Adapter (or Façade) and Composite and
  • behavioral patterns like Observer and Strategy

One good way to learn about Design Patterns is to write real software applications which, at their core, use patterns in order to solve real problems.

First exercise: SecurityManager

In a lot of applications, there exist classes which need to have only one instance (one object created at runtime). Examples are database connections, security managers to verify passwords, credit card processors, etc.

Write a Java class called SecurityManager which only have one instance throughout the application. Use the Singleton design pattern.

Second exercise: IOFacade

Frequently, we need to hide a set of classes with a complex interface behind a simpler class. This happens, for example, when reusing code written by another team of programmers.

Write a Java class called IOFacade which simplify the use of the Java I/O facilities by making exceptions less intrusive. Use the Façade design pattern. (This pattern is somewhat related to the Adapter as used in InputStream -> InputStreamReader -> BufferedReader.)

Third exercise: Observers

In a lot of operating systems and online services, observers register their interest for a specific event and, when this event happens, the subject of the event notifies all the observers. This allows all the observers to know that the event has occurred without having to resort to polling (which is wasteful of resources).

Write a Java program allows Observers to be added to Subjects. When the state of the Subject changes, all Observers should be immediately notified. Use the Observer design pattern.

Fourth exercise: Solar System

Finally, quite a lot of applications e.g. desktop environments such as Gnome or Windows, vectorial drawing apps, word processors, ERP systems, etc. need to model hierarchies e.g. graphical widgets, shapes, words and people / products.

As a tribute to Dennis Ritchie, write a Java program that models the whole of the Solar System including the star, planets, natural satellites and artificial satellites. Use the Composite design pattern. The class diagram for the Solar System application is:

This topic is part of our Object-Oriented Programming in Java training

Our forthcoming training courses

  • No training courses are scheduled.

Comments

  1. Diren says

    31 October 2012 at 14:14

    Hi Avinash,

    Can you please notify me if you offer Java Training courses apart from week days?
    Thanks

  2. liliane says

    31 October 2012 at 15:28

    Dear Diren
    We have scheduled the Java courses during week-days. In case we have other demand for such courses to be run on Saturdays, we will inform you right away. Can you please mail us your phone number at .

  3. Abdallah says

    17 September 2013 at 10:49

    Hi,
    I would like to know if you offer Java Training Courses during week ends….I have gone through Java ebooks but am still struggling to get a strong grasp of Java….I need help 😛 Java is driving me crazy.

  4. Liliane Chan says

    18 September 2013 at 09:42

    Dear Abdallah,

    We have not scheduled Java training during week-ends for the coming months. Thanks for your info details, we will inform you should there be any Java training coming up soon.

  5. Ingic says

    2 January 2017 at 16:54

    I would like to know if you offer Java Training Courses during week ends….I have gone through Java ebooks but am still struggling to get a strong grasp of Java….I need help ? Java is driving me crazy.

  6. Avinash Meetoo says

    6 January 2017 at 11:48

    Hi, we have not planned any Java training for the coming months. Sorry about that. Thanks for asking.

Leave a Reply

Your email address will not be published. Required fields are marked *

Looking for something?

Want to know more?

Get our newsletter

Discover the latest news, tips and tricks on Linux, the Web and Mobile technologies every week for FREE

This work is licensed by Knowledge7 under an Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.