• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Confusion regarding which pattern!

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question I found in a site.I am confused as to why the builder pattern is not the correct answer.Is it simply because the constuction is not complex? Because I feel that the same construction process can be used to build different products.The product over here is a PC.Can somebody throw some light on this???

Compact Computers is a small computer assembly company. Any customer currently has the following choices for a PC:



(i) 800 MHz processor, 40 GB HDD, 128 MB RAM

(ii) 1 GHz processor, 60 GB HDD, 256 MB RAM

(iii) 1.2 GHz processor, 80 GB HDD, 512 MB RAM



The use of what design pattern would ensure that only the legal combinations could be sold?



A Factory Method

B Builder

C Prototype

D Abstract Factory

E Singleton



Choice D is correct.



This question needs you to apply your knowledge of design patterns. We are dealing with families of related objects.

Abstract Factory (GOF 87)"Provide an interface for creating families of related or dependent objects without specifying their concrete classes."

The applicability section of Abstract Factory (GOF 88) indicates that this pattern is to be used when:

A system should be configured with one of multiple families of products

A family of related product objects is to be used together and the constraint needs to be enforced.

Hence Abstract Factory is the right pattern for this problem. Choice D is therefore correct.

Factory Method (GOF 107)" Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses." Hence choice A is incorrect.

Builder (GOF 97)"Separate the construction of a complex object from its representation so that the same construction process can create different representations." Hence choice B is incorrect.

Prototype (GOF 117)" Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype." Hence choice C is incorrect.

Singleton (GOF 127)" Ensure a class only has one instance, and provide a global point of access to it." Hence choice E is incorrect.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And funny as it is, at the Moment i took a mock-test from
JavaQueries and there is
exactly the same Question. But the correct answer there is the
Builder....

But i agree with this, and your opinion.

Mike
 
Raghubir Bose
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now this is interesting ...look at this question ..paticularly the explanation to D.

Compact Computers is a small computer assembly company. Their online application allows customers to pick and choose accessories to build their own PCs. The accessories are:



i. Processor - 800Mhz, 1Ghz, 1.2Ghz

ii. HDD - 40 GB, 60 GB, 80 GB

iii. Memory - 128 MB, 256 MB, 512 MB



If a computer can have exactly 1 processor, 1 HDD and 1 memory stick, what pattern would be best used here?



A Factory Method

B Builder

C Prototype

D Abstract Factory

E Singleton



Choice B is correct.



Builder (GOF 97) separates the construction of a complex object from its representation so that the same construction process can create different representations. Here the complex object is a computer. A computer is always made up of exactly one processor, one HDD and one Memory stick (problem description.) However there is no predetermined formula for combining the parts. Hence Builder is the best pattern here and B is therefore the right answer.

Answer A is incorrect because Factory Method (GOF 107) defines an interface for creating an object but lets subclasses decide which class to instantiate. You may use factories of factories to construct a complex object, but by itself, the Factory method is good for creating one out of many. Example: create one processor out of a set of three processors.

Answer C is incorrect because Prototype (GOF 117) specifies the kinds of objects to create using a prototypical instance. Example: Given a processor, if you were asked to create a computer that used multi processors, this would be a good option.

Answer D is incorrect because Abstract Factory (GOF 87) provides an interface for creating a family of related or dependent objects. If the question had defined a relation such as 'A computer of 800 MHz processor can only be coupled with a 40 GB HDD and 128 MB RAM stick', this would have been an ideal solution.

Answer E is incorrect because Singleton (GOF 127) ensures that a class has only one instance (or a well-defined number of variable instances) and appropriate global pointers are available to the instance(s).

..what do you say now
 
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The builder pattern is typically used when you need to :

1. Encapsulate construction of a complex object
2. Create an object in multiple steps
3. Need flexibility to vary the steps while creating the object. For example, I may need to select / omit a few steps during creation.

ie You use the builder pattern to build an object in multiple steps and varying processes. Here you want to construct a computer in steps - you want to build a RAM, build a HDD, build a processor. But do we need the flexibility to vary the process? I dont think so. You would not have a computer with only RAM and HDD without a processor or with a processor and HDD and without RAM. You don't really need the granularity of varying the steps while constructing the object. Hence, builder I feel is overkill. You can have an abstract factory and define concrete factories to create the various families of related objects.You can ensure that only legal combinations are created in the factories.
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above answer answers the first question where the exact combinations are known. But in the second case, you do not know the exact combinations. If you use abstract factory, you would have to create factories for all possible combinations which would be inefficient. It would be better to use builder where you have a create method for creating each product (processor, HDD etc). Let the user call the create method for each product and worry about mixing them in whatever way he wants
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what I get from you is that

1 If we have a defined family of objects to build(meaning the object compostion is known at design time), go for Abstract Factoty Pattern

2.If we do not know the object composition meaning that the object compostion has to be dynamically determined , then go for a builder pattern.

Is that correct ?
If yes then

If no then
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, If you think about it that makes sense. If you know the exact families of objects that you need to construct, you can use the abstract factory pattern. You can define a concrete factory for each family you need to build. But if you do not know the exact combination of the family of objects, then if you use the AF pattern, you have to create concrete factories for all possible combinations, which will be a waste. If you use the builder pattern, you can expose a create method for the each component that goes into making the product as separate steps. Let the client use those steps to construct the product using whatever combination he wants. M
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Satish,
I totally agree with you in the first requirement. But the second one seem to be better match for the ProtoType pattern rather than Builder. The reason I think that was that you know all the parts that a computer must have, you can create a prototype with the default configuration and then based on customer choice you can get the copy of the prototype object and change the configuration right?
 
Raghubir Bose
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jogaraj,
Welcome to Javaranch ... the above two answers were correct .. the question that we got in the yahoo group is as follows ..


Compact Computers is a small computer assembly company. Its online
application allows customers to pick and choose accessories to build their
own PCs. The accessories are:



i. Processor - 800Mhz, 1Ghz, 1.2Ghz

ii. HDD - 40 GB, 60 GB, 80 GB

iii. Memory - 128 MB, 256 MB, 512 MB



Customers choose parts and quantities during the order. For example, a
customer could choose a second HDD as a secondary hard drive or purchase
additional RAM. What design pattern may be optimal for implementing a
suitable design here?


A Factory Method

B Prototype

C Singleton

D Template Method

E Business Delegate

F Builder

here please note the statement "a customer could choose a second HDD as a secondary hard drive or purchase additional RAM"

this means .. the construction process is same but we need to vary the structure ...this I think calls for the prototyping pattern.

So moral of the story :

1 If we have a defined family of objects to build(meaning the object compostion is known at design time), go for Abstract Factoty Pattern

2.If we do not know the object composition meaning that the object compostion has to be dynamically determined , then go for a builder pattern

3.If we do not know the object composition meaning that the object compostion has to be dynamically determined and furthermore the structure of the object can change dynamically ..go for prototype.

So may be creational patterns depending on complexity go like this in the increasing order of complexity

factory -- > abstract factory --> builder --> prototype.

Singleton is a supportive pattern in this thought process. In Ramu Meda's notes I was studying that Abstract factory and prototype are somewhat competing ..and they can also be used together.An abstract factory may also store a set of prototypes from which to clone and return product objects.


Any pattern expert please comment

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic