Design Technique: How to design a complex system for processing orders, products and units.

Posted by Shyam on Stack Overflow See other posts from Stack Overflow or by Shyam
Published on 2010-04-15T15:11:53Z Indexed on 2010/04/15 15:13 UTC
Read the original article Hit count: 300

Hi,

Programming is fun: I learned that by trying out simple challenges, reading up some books and following some tutorials. I am able to grasp the concepts of writing with OO (I do so in Ruby), and write a bit of code myself. What bugs me though is that I feel re-inventing the wheel: I haven't followed an education or found a book (a free one that is) that explains me the why's instead of the how's, and I've learned from the A-team that it is the plan that makes it come together. So, armed with my nuby Ruby skills, I decided I wanted to program a virtual store. I figured out the following:

My virtual Store will have:

  • Products and Services
  • Inventories
  • Orders and Shipping
  • Customers

Now this isn't complex at all. With the help of some cool tools (CMapTools), I drew out some concepts, but quickly enough (thanks to my inferior experience in designing), my design started to bite me. My very first product-line were virtual "laptops". So, I created a class (Ruby):

class Product
  attr_accessor :name, :price
  def initialize(name, price)
    @name = name
    @price = price
  end
end

which can be instantiated by doing (IRb)

x = Product.new("Banana Pro", 250)

Since I want my virtual customers to be able to purchase more than one product, or various types, I figured out I needed some kind of "Order" mechanism.

class Order

  def initialize(order_no)
    @order_no = order_no
    @line_items = []
  end 

  def add_product(myproduct)
    @line_items << myproduct
  end

  def show_order()
    puts @order_no
    @line_items.each do |x|
     puts x.name.to_s + "\t" + x.price.to_s
    end 
  end
end 

that can be instantiated by doing (IRb)

   z = Order.new(1234)
   z.add_product(x)
   z.show_order

Splendid, I have now a very simple ordering system that allows me to add products to an order. But, here comes my real question.

What if I have three models of my product (economy, business, showoff)? Or have my products be composed out of separate units (bigger screen, nicer keyboard, different OS)? Surely I could make them three separate products, or add complexity to my product class, but I am looking for are best practices to design a flexible product object that can be used in the real world, to facilitate a complex system.

My apologies if my grammar and my spelling are with error, as english is not my first language and I took the time to check as far I could understand and translate properly!

Thank you for your answers, comments and feedback!

© Stack Overflow or respective owner

Related posts about object-oriented-design

Related posts about ruby