Object Oriented Design Tips

Even though functional programming is more and more popular nowadays, but Object Oriented programming is still the main stream in industry. Object Oriented Design (OOD) skills are basics for professional programming and used widely in interviews and professional development because they establish a clear view for the whole project and architect for all elements and events.

When we talk OOD and we always want to know six Ws (when, where, who, what, how, why). UML is a very helpful tool to understand these factors, like event time, location, actors involved, actions, relationship between actors. The process we extract these elements from real life scenes and then draw a Unified Modeling Language (UML) classes diagram and finally implement with actual code is the OOD. By the way, UML is a powerful tool which help us to construct the system but don’t focus too much or limited by UML, the goal is to clarify questions and solve the problem.

This is the only basic structure and design for the problem which fits in specific scenario and has some bottleneck to scale to a much larger application. In that case, you need do some calculation with more specific data based on the requirement, then gradually improve the system until you are satisfied.

For example, if you want to design a restaurant system, you can start from imaging the restaurant in real life, it will definitely have some tables, a front desk for a host or cashier, a kitchen for chefs to cook foods and some waiters or waitresses walk back and forth to get orders, place them and notify chefs via the system. As you can see, dining tables are the core parts since customers come in and out just to have food and drink in indoor or outdoor seats.

If we focus on tables and ignore front desk and kitchen first, anyone can have some ideas about properties and actions related to a table. For instance, we can find a card or label on table to identify it and it’s the ID, then how many persons it can hold (size, i.e. 5 to 10), the table is idle, reserved or occupied (status), it’s indoor or outdoor (location), what kind of foods (food list) customers ordered and how about drink (drink list), how much totally for the whole order (price), customers choose to split or not (split flag), finally there is a waiter or waitress (waiterId) server the table.

public class Table {
  private int tableId;
  private int size;
  private int status; // idle, reserved, occupied
  private boolean indoor; // true for indoor, false for outdoor
  private double price;
  private boolean split; // true for split, false for paying together int waiterId;
  private ArrayList<Food> foodList;
  private ArrayList<Drink> drinkList;

  public void orderFoods();
  public void orderDrinks();
  public void checkAndPayBill();
  public void callForWaiter();

Even for the simplest application, there should be a central management system to schedule and maintain dining tables list and it’s operated by a manager.

public class ManageSystem {
  private ArrayList<Table> tableList;
  private int managerId;

  public void appendTable();
  public void checkAndPrepareBill();

In summary, imagine the real world and extract when, where, who, what, how, why, then draw a UML class diagram to help understand the whole picture, finally write down classes with real code.

Buy me  a coffee with PayPal if you like my blogs and visit my web app, WhizWallet, if you want to apply for credit, store or gift cards.


About liyao13

Yao Li is a web and iOS developer, blogger and he has a passion for technology and business. In his blogs, he shares code snippets, tutorials, resources and notes to help people develop their skills. Donate $5 to him for a coffee with PayPal at About Me page and read more professional and interesting technical blog articles. Follow him @Yaoli0615 at Twitter to get latest tech updates.
This entry was posted in CS Research&Application, IT, Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s