File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Builder and Composite Patterns? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Builder and Composite Patterns?" Watch "Builder and Composite Patterns?" New topic
Author

Builder and Composite Patterns?

Glenn Castro
Ranch Hand

Joined: Aug 24, 2003
Posts: 78
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?


Glenn Castro<br />Sun Certified Web Component Developer
Sathvathsan Sampath
Ranch Hand

Joined: Oct 03, 2000
Posts: 96
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}.


- Sathvathsan Sampath
Glenn Castro
Ranch Hand

Joined: Aug 24, 2003
Posts: 78
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

Joined: Oct 03, 2000
Posts: 96
Please narrow down the scope of your question
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
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.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
 
Consider Paul's rocket mass heater.
 
subject: Builder and Composite Patterns?