Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

27 March 2011

Software Design Puzzle #8 - Manufacturing Soaps

Assume that you are an expert in manufacturing many varieties of soaps - bath soaps, detergent soaps and all soaps in the earth. You have a magic recipes of soap making right from procuring raw materials till manufacturing soaps and shipping to stores. There are many processes involved in soap making and there are various flavors for each process. And some of the processes are optional and specific types of soaps. For example, you know that you to have add a lot of scents for bath/toilet soap and lot of whitening material for detergent soaps. 

The puzzle is to design the soap manufacturing with lot of processes/steps and all these processes/steps are sequential meaning that one process cannot start until the previous processes are complete. For instance, the process of shipping cannot happen until you make soap and you cannot make soaps until you form soap base and you cannot start soap base without raw materials.

Can you design soap manufacturing process where one step depends on so many previous steps. To induce your thinking, let us assume that you have the following processes/steps

  • Buy raw materials (can be different based on the soaps)
  • Mix raw materials
  • Form soap base
  • Make soaps
  • Package soaps
  • Ship It
You are free to add more steps but the key is to come up with a process that can be changed dynamically based on soap types that are going to be invented in future :-)

16 February 2011

Software Design Puzzle #7.2 - Thread Pools & Tasks

Please refer to previous two posts on the same problem - Implementing Thread Pools and Tasks. Here is the link for your convenience.
Yesterday, i added a requirement that i want to have a priority for tasks (a problem on data structures and algorithms). Today, i thought about another feature that gives a lot of flexibility (real OO design). In the above examples, we didn't talk much about the threads in thread pools. Can we try to make threads in thread pool dynamic entity - based on the need, the number of threads in thread pool should grow or shrink. Here are my next set of questions.

  1. What are the design decisions that i should take so as to make thread pool dynamic.
  2. How can i make threads, tasks, thread pool at the topmost level of abstraction and yet get many different concrete implementations.
  3. How can i ensure that the code I am going to develop after two years (due to new requirements) doesn't affect my code now (seal the code from modification for new requirements)
  4. How can i bring in hierarchy, levels of abstraction and modularity for better design?
  5. Do i have any design patterns?

15 February 2011

Software Design Puzzle #7.1 - Thread Pools & Tasks

Refer to the previous post, Software Design Puzzle #7 - Thread Pools & Tasks. This post is an addendum to the previous post.

I m going to add one more enhancement to the thread pool design. The enhancement is to come up with a priority queue for tasks. Each task will have certain priority, an integer that increases as priority increases. At any given time, the task with highest priority has to be selected and run by threads in thread pool.

Can you refactor your design?

13 February 2011

Software Design Puzzle #7 - Thread Pools & Tasks

We know what is thread. The thread is an independent execution path or entity in a process. When you want to exploit multi-core (increased processing) or do some blocking operations, the thread is the default destination (increased interactivity, heavy input/output operations). In any programming language that supports multi-threading, you run the code that can be run as independent execution path as thread with each having its own context. The infrastructure that is needed to run threads in parallel like stack, program counter, registers are tightly coupled with the code that runs. Due to this tight coupling, each time you need an independent path (let us call it as task), you create a thread (the infrastructure) which has its own overheads. Is it possible to change code of the thread?

Obviously the next step is to separate out the infrastructure and the task which is achieved using thread pools. Here is the next problem.

Write a simple thread pool in Java with following requirements

  1. Have a fixed but configurable number of threads in thread pool. At any point of time, you can run up to N threads.
  2. Have a task queue (or a suitable data structure). This can be much bigger than number of threads. The tasks wait in the queue for their turn.
  3. The thread should consume tasks from the task queue and execute it. It can be any task (but you have to ensure some fairness). The role of the thread is to run task without bothering too much on what it does and how it does.

15 January 2011

Importance of Expressiveness in Programming Languages

It is a while i had a post here and in fact this is my first post in 2011. I happy to have my first post of the year on the topic that is of huge interest to me these days.

Why aren't we still programming in assembly languages? The threads, inter-process communication mechanisms can be done in assembly language but not easily though. With assembly language, there is only level of abstraction - ability to see the requirements as sequence of bits that are comprehensible by the machine. When you want make your program to run in another target platform, you may need to change both the abstraction and implementation. The assembly language is not as expressive as procedural language. In order to increment a number, you have locate the address, move it to accumulator, add it by one and store back to the same memory location. There is no clear division of responsibility. You have more number of steps to do in assembly but that is very primitive in procedural language.

Why aren't we writing program with procedural language alone? Procedural language is less expressive than object oriented program. The amount of code that a developer should write to accomplish a logic is more in the case of procedural language. While the level of abstraction is moved a level up from the bare machine, still the abstraction is not efficient to solve a problem in application domain. As far as the application is concerned (and in imperative languages), a task is accomplished with side effects. You call a method and it changes the state of few objects. The data and operation have to cohesive. So, in application development it is highly essential that we glue the data together with operations because these operations are specialized over the data.

There are other types of programming languages like functional programming inspired by lambda calculus and one can write their own language for a particular domain. There is one basic commonality in all types of programming languages - they try to be more expressive to make the life of developers easier. For example, today many developers are thinking about writing concurrent programs not only due to multi-cores being prevalent but also due to modern languages that made multi-threading highly expressive. It is easier to write multi-threaded in Java than in C or assembly.

As we move on, we will see many more expressive language, expressive than Java world and it is part of evolution of programming languages.

The article was written after reading Grady Booch's "OOAD with Application" and first chapter of "Programming Paradigms". Hoping to bring few posts on the subject this year. Watch out this space.

30 December 2010

A Question on Java Exception?

In Java, there are two types of exceptions - checked and unchecked.
Java compiler ensures that "checked" exceptions are handled and flags compilation error when checked exceptions are not handled. But it does not flag errors when "unchecked" exceptions are not handled. The subclasses of "RuntimeException" and the subclasses "Error" are "unchecked".

Why does Java have this as a thumb rule?

28 December 2010

Software Design Puzzle #6 - Design A Dictionary

Think about a dictionary now and kind of things that you will do with a dictionary. Normally a dictionary will have simple operations of add, delete, edit and search words. Being a software dictionary, won't it be good that it suggests you list of words based on the words you type.

For example, when you type "sim", it should display first "N" closest match with the prefix supplied. I don't need to emphasize that the first word should be the exact match (if any) and then followed by list of words that matches closely.

Assume that you have many data store in which the words are stored (database, XML file, txt file, CSV file etc). It is enough to perform the search/auto-suggestion on the data in local database. If the user is willing, the user can also hear the word through audio and for doing that you have to contact a remote web service that gives a audio file for a specific word. You can also assume that there exists a audio file for every word in the database.

Can you design the application, come up with set of classes, assign responsibilities to the classes and a brief architectural diagram too.

Put your design patterns knowledge in action :-)

03 October 2010

Presentation on "Basics Things on Objects"

Last week i shared with you one of my old presentations on Security. From this week and for few weeks, i ll be writing/sharing few presentations on "Objects", "Design", "Design Principles" and "Design Patterns". I m not sure whether i ll be regularly posting every week. At this point of time, i m trying to find my consistency to post the presentations every week. Please bear with me until i find my consistency.

Each deck will have
  • slides focusing on only one thing
  • short recap of things that are discussed
  • drill for the week (you can try to reflect on the problem until i post the next presentation)
  • the topic of next week
Here goes the first presentation. This is just a curtain raiser to help us to set in same frequency. [ I would love to hear from you - what do think and how can we make this much better.]

Basic Things on "Objects" - Part 1


20 September 2010

Can you Really Teach Abstraction?

I was talking to one of my friends about OOPS. While talking to him, a question suddenly flashed in my mind about OOPS and more specifically about abstraction - how can you teach abstraction and the methods to be used to abstract ideas. In order to answer this question, we need to understand the real meaning of abstraction - "a concept or idea that is not associated with any specific instance". In other words, it is our ability to take our learning to higher level so that it becomes independent of specific instances. 

Let us try to understand whether it is possible to teach abstraction with a help of simple mathematical addition. 1 + 1 = 2, 1 + 3 = 4,  6 + 5 = 11. Each of these additions are specific instances of addition. When we are learning "addition", we focus more on "+" (and put it in long term memory, yep that is abstraction) and we just remember the numbers until we solve the problem. Once the problem is solved, we just forget the numbers (short term memory). When we are early stages of learning, we tend to believe that specific instance is ultimate truth. When we practice, we face many types of similar problems and start to change our perspectives. The change of perspectives is critical that leads abstraction. With practice, we are programmed to abstract concepts (eg: addition).

So, i tend to believe that abstraction is directly proportional to practice and more specifically the number of "wow" moments that you create on specific subject and i agree if someone says "abstraction cannot be taught but can only be felt or facilitated".

19 September 2010

OOP - Bending, Stretching, Thinking My Mind

The past two days, the Saturday and Sunday, were really exciting. I lived in this world with a lot of imagination and i would say it was good exercise for my brain. When it comes to Object Oriented Programming, nothing comes near to practice (of course, practice with head/brain/mind or whatever we call it). Object thinking was good testimony that one has brain and trying to use it.

I started off with a nice relaxation at Marina Beach with Sandy (an amazing guy and rock star) who often gives you a lot of challenges in the form of questions. It is not so easy to answer him as it is quite difficult to convince him (i bet, if i have a presentation now, i ll be more comfortable in answering questions than i used to be few months back). We spent around two hours in Marina beach (and we both thanked each other for making a good start) and later an hour in Saravana Bhavan. Dropped of Sandy at Guindy and from Guindy, i started my week on OOP.

I was trying to come up with a design for a problem and so far came up with at least five designs. Whenever i tried redesigning, i got to go back and reread the requirements/problem statements. I m a poor guy and i got to read the requirements many times. But yet to settle down with a design. I believe, i ll change the design five more times :-)

I expect to complete the design and coding of the problem this week and hope the next Saturday morning will be intense with some OO discussion.

@Sandy
I hope, you will join me next Saturday too :-). But the next time, we will be more technical and let us make sure that there is good amount of learning. What do u think?

31 August 2010

Software Design Puzzle #4 - Building A Flexible Weather Station

The puzzle is on building a flexible weather station. This weather station's main job is to provide instantaneous updates to its clients on various weather conditions like temperature, atmospheric pressure, wind speed, pollution levels and a forecast on the rain. The clients are heterogeneous starting from a full blown weather forecasting websites which are interested in all updates (everything from temperature to rain forecast that are available time to time) to a roaming marketing guys who just want forecast of rain to plan their travel. The point is there are few clients that want all updates, few wanting all except few conditions and many wanting only few conditions. The clients will also be requesting for more weather parameters dynamically.

The requirement is to identify design pattern(s) that will suit these requirements. Model the class diagrams focusing on important aspects of the design. The following is a just a sample client (thanks to Google).

A question that comes to my mind on these puzzles. Does it really help me to build my OO expertise?

An honest answer is it depends. The idea of posting these puzzles is not make us OO guru with few hundred puzzles and in fact OO requires naked thinking. I feel that a lot of thinking can be stimulated by these examples. Memorizing a design doesn't help though. Just skim through the puzzle and solve it. When you are solving think about abstraction. Abstraction is your ability to see sand in a beach as a thin line when you are flying few thousand feet above and view them as a mighty rock when seeing through microscope.

20 August 2010

Software Design Puzzle #3 - Modeling TCP/IP Stack

You would have heard about TCP/IP Stack in Networking (if you haven't or want help in refreshing, please refer here). The puzzle to identify and come up with set of classes with properties and functionalities to represent TCP/IP model using object oriented principles. Also, identify the list of design patterns that can be used while you are designing the classes.

The answers to previous two puzzles on algorithm is out. Please check out all the puzzles here. Will be publishing the answers to all outstanding puzzles this weekend.

Also, please do let me know your comments/feedback/suggestions.

17 August 2010

Design Patterns - Tool to Think in Objects

There are many reasons to know and experience design patterns like finding answer to well known problems that are tested by time, it is a common language that can used for effective communication and many more. While getting exposed to design patterns helps us find solution, the experience of analyzing design patterns helps us to build a ever expanding knowledge base. It changes our perspective of seeking solution.

If one can analyze few design patterns, then it becomes all the more easy for someone to comprehend other design patterns. While learning and analyzing patters, without our conscious efforts, it helps us to see problem from objects point of view. Our structured thinking goes away step by step and we tend to "think" objects and "design" objects.

One of the key benefits of learning design patterns is "object thinking"

The answers for puzzles posted last week were published. Please visit Puzzle and Puzzle. Watch out for the next puzzle on "Binary Tree".

29 September 2009

OOPS - What is Abstraction?

There is song in a movie in Tamil (Alavanthan) and the song sequence happens in a party. Everyone would crack joke and our hero would share a joke. Here it goes. "A little kid asks his parent. What is the meaning of sex? And their parents explain with a lot of struggle. and try to explain the meaning of sex. Finally the kid just asks, Should I check male or female in the application form". We, the developers, without understanding what is abstraction try to develop application. For a kid it does not make sense to say intricacies of something as it incapable of understanding that something (example, sex). The parents should abstract the idea in a form that is sufficient or needed for the level. And sometimes the details are not required.

Another example of abstraction in real world could be appearance of beach sand. For someone who sits on the beach, the sand will appear course but for someone who is traveling in an aircraft few kilometers above the sea level, the beach itself appears as a thin line. In the real world, the abstraction is quite common and we human being appreciate abstraction quite well.

Let us take another example. How will you tell your find that you are going home? Assuming that your home is in "Nanganallur, Chennai, Tamilnadu, India, Asia" and you are in S"an Jose, California, US, North America". You just say that you are going to India. But when you are in India, you give more specific address.

Yet another example of abstraction is someone taking bath. If someone is taking bath, you hardly worry what he is doing inside. You never try to investigate what someone is doing in bathroom. But the guy who is taking bath, he does it with a lot of details. You shed out details when it is not required. Interestingly, in real world you clearly know when to shed out details.

So abstraction is something that gets built up as you move up the hierarchy and you leave out details intentionally to make the concept more generic. Now again, go back and reread the real world examples. It should make sense. Then correlate with abstraction in object oriented programming.

27 September 2009

OOPS - Rules of Overriding?

This is a typical interview question. What are the rules of overriding. Here goes the answer
  1. The method in the subclass should have the same signature as that of the method in super class
  2. The method in subclass should not be more restrictive in the context of access specifiers
  3. The method that overrides the method in super class cannot be through broader exception meaning that it cannot throw the super class of exception thrown by super class.
  4. "final" method cannot be overridden
Leaving out the last condition, as it is obvious, is there a reason for top three conditions. Why does Java (or any object oriented programming language) have this as rule? Is there any logic? BTW, this is not a standard interview question. But a question that provokes thinking. Will post the answer in couple of days (meanwhile if you know the answer, write in comments)