• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Builder and Composite Patterns?

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is a Builder Pattern and Composite Pattern? What practical applications they are used for?

Can you give me a good site for these and other patterns?
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can easily find explanation of these patterns googling on the net. I found myself applying these two patterns like this:
1) Builder pattern - I had a complex component that was in turn made of many other sub-components. I had to abstract the creation of a table model to be displayed on the UI.
The table comprised of a header and one or more rows.
- Each header contained info about the no of cols displayed & info about those cols etc etc.
- Rows in turns contained multiple cells for each row and some associated info.

Some of the design forces were:
1) Abstract the complexity of creating the table thro a simple interface.
2) A table once created must have all of its sub-components correctly initialized (invariants). This operation should be atomic.
3) Not all table had its set of sub-components initialized in the same way. Some tables were read-only while others were editable. Based on that there were some variations in the behavior of sub-components.
4) Putting the responsibility of creation of entire table component in a Table class would complicate its responsibility and reduce its simplicity.

A Builder pattern helped me solve this.

2) Composite pattern - I have to validate editable cells on the displayed table.
- Some cells required a numeric check, mandatory check, a float check, date range checks and so on.
- Some cells needed more than one primitive validation checks; e.g. - mandatory numeric field (mandatory check + numeric check).
Potentially, validation checks can be expressed as a series checks recursively or as a single check.

Some of the design forces were:
1) Validation rules are volatile and may not change along with the Table class or its sub-components.
2) The same rule could be applied to multiple cells.
3) Client code (say of table class) should not be aware of the way the validation rules are expressed (they may change too). All the client code is bothered is - some validator that validates user value (simple validate() interface).

A composite pattern helped me solve this. The validator component was a composite. It was composed multiple child validators or standalone. Client code was unaware of this.
Take a simplistic example Mandatory numeric range check - A quantity field had to be numeric, non-negative, within 0-100 (say) and mandatory. This will translate as {mandatory check + numeric check + range check}.
 
Glenn Castro
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank Sathvathsan for your reply!

BTW, if I'll have to create a simple API for the client's UI(for desktops, mobiles, etc.) Which pattern is best so that the code will be reusable.

TIA
 
Sathvathsan Sampath
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please narrow down the scope of your question
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Composite pattern is used when you want to treat groups of items the same way you do individual items. This is done by using the same interface for both groups of items and individual items. One example is that often you want to treat groups of files or directories the same way you want to treat individual files. i.e. you may want to copy(), encode(), zip(), delete(), rename(),... This way your code doesn't need to know if it is dealing with many files or one.

I just created an ArrayFilter. It allows you to query arrays much like SQL allows you to query database tables. I used the composite pattern to implement the where clause conditionals. Whether you have one or many all conditinals must evaluate to true or false.

For example "select * from array where col1='jones'" is one conditional, but by using the composite pattern I had to code very little to do much more complex logic like "select * from array where col1='jones' && col2='john' || col1='smith'". The code is available via my website http://www.fdsapi.com

Another example would be to treat a group of gui controls in a similar way to one. You may draw() a button or draw() a window, where the window contains buttons, listboxes, etc that all have draw() methods. One powerful thing about composites is that because they have the same interface as the individual items that they contain, they can also contain other composites. In this example it would mean windows could contain other windows as well as simple controls.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic