473,761 Members | 3,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this design pattern correct (builder)

Hello NG,

i am just learning various Design Patterns and now i am not sure, if
this design is correct (Builder) or if i should use an other pattern.
I have various classes (here ChildA and ChildB) derived from class Base.
Now i want to create an object, but i don't want to know which class to
instantiate.

All classes have "pseudo"-methods/codes to keep it very simple.

------------------ cut here ------------------
class Base
{
public:
virtual ~Base() {}; // hack!
virtual start() = 0;
virtual stop() = 0;

// with empty methods only for default implementation
void create (const char* last, const char* first) {} ;
void create (const char* last) {} ;
};

// --------------------------------------------

class ChildA : public Base
{
public:
ChildA();
void start();
void stop();

// ok, child_a implements this create method
void create(const char* name)
{ /* do something */}
};

// --------------------------------------------
class ChildB : public Base
{
public:
ChildB();
void start();
void stop();

// ok, child_a implements the other create method
void create (const char* name1, const char* name2)
{ /* do something else */ }
};

// --------------------------------------------
class Builder
{
public:
Base* createObject(in t type)
{
if (type = 1)
return new ChildA;
if (type = 2)
return new ChildB;

// exception handling
...
...
}
} ;

// --------------------------------------------

int main()
{
Base base = Builder::create Object(2); // ChildB
base->create("Doe" , "Johnny");
base->start();

/* do the rest */
...
...
}

------------------ cut here ------------------

Thanks a lot for your answers
Mike

May 22 '06 #1
2 2299
Mike wrote:
Hello NG,
Hello, Mike.
i am just learning various Design Patterns and now i am not sure, if
this design is correct (Builder) or if i should use an other pattern.
Do you want a critique on your choice of this pattern, your
implementation, or both?
I have various classes (here ChildA and ChildB) derived from class Base.
Now i want to create an object, but i don't want to know which class to
instantiate.
I don't have my DP book handy, but this sounds a lot more like Abstract
Factory or Factory Method to me, depending on the context. Builder is
about generalizing the construction of complex (as in having many
parts) objects which are different but have a similar set of
construction steps.
All classes have "pseudo"-methods/codes to keep it very simple.
Might as well take out all the start() and stop() business then, too.
------------------ cut here ------------------
class Base
{
public:
virtual ~Base() {}; // hack!
How is this a hack?
// with empty methods only for default implementation
void create (const char* last, const char* first) {} ;
void create (const char* last) {} ;
These should almost certainly be virtual, considering that you're
talking about a "default implementation, " and have written member
functions with the same signatures in derived classes. Creational
design patterns build heavily on the basic concepts and techniques of
polymorphism -- trying to use the one without a thorough understanding
of the other is unlikely to be fruitful.
class Builder
{
public:
Base* createObject(in t type)
{
if (type = 1)
return new ChildA;
if (type = 2)
return new ChildB;
Type codes are pretty ugly. At the very least, use an enumerated type
here to reduce worries about out-of-range values. Better, though,
would be to change the design. I don't believe GoF describes any
creational patterns involving passing a description of the type as a
parameter -- they mostly rely on polymorphism instead.
int main()
{
Base base = Builder::create Object(2); // ChildB
base->create("Doe" , "Johnny");
base->start();


Seems like either you've chosen a rather poorly-named member function
("create") for your example, or you're misunderstandin g the
responsibilitie s involved here. What is being created by create()?
You've already created a ChildA or ChildB, and create() doesn't return
anything. The creational part is going on in createObject(). Having
something else called create() just confuses the example, at best.

So, in sum... this doesn't look much like the Builder pattern; it looks
more like a somewhat ill-informed implementation of some other pattern.
I suggest you read up on all of the creational patterns, especially
Abstract Factory and Factory Method, and think carefully about which
one solves the problem you're facing. Use the UML diagrams as a quick
way to check whether your implementation even has the right number of
entities to represent the concepts used by the pattern.

Luke

May 22 '06 #2
"Luke Meyers" <n.***********@ gmail.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
Mike wrote:
Hello NG,


Hello, Mike.
i am just learning various Design Patterns and now i am not sure, if
this design is correct (Builder) or if i should use an other pattern.


Do you want a critique on your choice of this pattern, your
implementation, or both?

<snip other stuff>
class Builder
{
public:
Base* createObject(in t type)
{
if (type = 1)
return new ChildA;
if (type = 2)
return new ChildB;


Type codes are pretty ugly. At the very least, use an enumerated type
here to reduce worries about out-of-range values. Better, though,
would be to change the design. I don't believe GoF describes any
creational patterns involving passing a description of the type as a
parameter -- they mostly rely on polymorphism instead.


This seems to always return a pointer to a ChildA as well. Note that if(type
= 1) sets type to 1, checks whether it's non-zero (implicitly), finds that
it is and then returns a pointer to a new ChildA. Presumably the OP meant
if(type == 1) etc. But what you said about the whole type code thing in
general being pretty dodgy still stands (changing = to == here is akin to
putting a sticking plaster over a gaping wound - it doesn't really fix the
underlying issue, even if it makes you feel like you're doing something
constructive).

Cheers :)
Stu

<snip other stuff>
May 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1611
by: Stewart Rogers | last post by:
Hi all, I have been working on an ASP.NET application that is a kind of wizard ( a list of sequential pages ). We built that application for the CLIENT-A and it worked fine. After six months CLIENT-B came along and and requested the same application but with little bit different requirements (both in terms of the front-end and back-end processing), so we had to create a new application for that client. Now came CLIENT-C !!!!!! I am...
2
3488
by: qazmlp | last post by:
Builder pattern seem to suit my requirements perfectly except in one case as described below. I have used the same example diagram that is used in GOF Design pattern book, to explain the problem that I am facing. aClient aDirector aConcreteBuilder =============================================================================
3
4141
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability. The User Experience, or how the user experiences the end product, is the key to acceptance. And that is where User Interface Design enters the design process. While product engineers focus on the technology, usability specialists focus on the user...
4
1929
by: Frazer | last post by:
hi are there any good samples illustrating design patterns in C#? builder, adapter, facade etc. i found a few but the reviews of that article were bad. eg http://www.codeproject.com/csharp/csdespat_1.asp#xx327127xx thnx
1
1887
by: Josh28 | last post by:
Hi We are a group of two chaps just out of undergrad, we created a software to automate the use of Design Patterns. We have put it up at Source Forge--http://dpatoolkit.sourceforge.net/ The software has been designed using the .NET framework and coded in C#. The patterns can be stored as plug-ins in XML, adding any number of attributes like Intent, Behavior and the like... Class
4
2878
by: FluffyCat | last post by:
New on November 29, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Command Pattern. Since you all enjoyed the Visitor Pattern so much yesterday, today I have the Command Pattern for you. This one is pretty straight forward. In the Command Pattern an object encapsulates everything needed to execute a method in another object. http://www.fluffycat.com/SDCMSv2/PHP-Design-Patterns-Command/
0
1719
by: FluffyCat | last post by:
Last fall I started a series of design pattern examples using PHP5. I think the last pattern I did was the Singleton in January. Getting back to it here is my first cut at the Builder pattern. http://www.fluffycat.com/PHP-Design-Patterns-Builder/ In the builder pattern a director and builder work togther to build an object. The director controls the building and specifies any variations that can be done with the object. The builder...
7
3583
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id - FirstName - LastName - CompanyId (many-to-one )
10
3676
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these classes talk to front-end directly. Do I need to use any design pattern in this? or what kind of design pattern is this?
0
9538
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9353
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.