开发者

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

开发者 https://www.devze.com 2022-12-27 04:58 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 5 years ago.

Improve this question

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 I'm 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 com开发者_如何学Goe 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)

Because I want my virtual customers being able to purchase more than one product, or various configurations, 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!


One of the cool things I discovered: using integers is one, but translating them into a better currency format, using a string format

puts "%s : $ %.2f" % [@name.to_s, @price]

You can also use the printf, but it looks more C-ish than sparkling Ruby. Also learned that I could make small Units as seperate objects and put these inside an array.

def initialize(name)
  @my_name = name
  @my_stack = []
end

def add_unit(unit)
    if @my_stack.length < 8
        @my_stack << unit
        puts "Added unit"
    else
        puts "Only room for 8 units."
    end

end


More discovery, reading a serious bunch of books which were mind dazzling. But then I remember some memories from last year, where I was doing some nuby web development and the combination of learning some Rails. I thought of CRUD! So, I think all objects need a CRUD interface. I am still wondering if destroying by setting instance variables to nil makes it marked for garbage collection. I use this class to be added inside a balance array. I think using a CRUD approach is the first step to a conventional API.

class Product

    attr_accessor :my_name, :my_description, :my_amount

    # Create
    def initialize(name, description, amount)
        @my_name = name
        @my_description = description
        @my_amount = amount
    end

    # Read/Show
    def show
        x = [@my_name, @my_description, @my_amount] 
    end

    # Update
    def alter(name, description, amount)
        @my_name = name
        @my_description = description
        @my_amount = amount 
    end

    # Destroy
    def clean
        @my_name = nil
        @my_description = nil
        @my_amount = nil
    end

    # Diagnose is Development/Test Mode 
    def diagnose()
     puts "initialize(name, description, amount)"
     puts "{#@my_name} :: {#@my_description} :: {#@my_amount}"
     puts "%s :: %s :: %.2f" % [@my_name, @my_description, @my_amount] 
    end

end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号