Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,476 members, 7,808,731 topics. Date: Thursday, 25 April 2024 at 04:03 PM

Design Patterns - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Design Patterns (2019 Views)

Essential Javascript Design Patterns For Beginners / Javascript And Design Patterns / Data Structures And Algorithms With Object-oriented Design Patterns In Java (2) (3) (4)

(1) (2) (Reply) (Go Down)

Design Patterns by harryobas: 9:30am On Jul 21, 2012
Hello my fellow nairaland developers, if you have been doing object-oriented design and programming for a long time u certainly must have come across design patterns. Now i am not claiming to be a design patterns guru but i do apply some design patterns on my development projects from time to time and i have found them very useful. The patterns that i apply on a regular basis are singleton, abstract factory and facade.If there are any developers out there applying patterns i would like to know which patterns u are applying and your experiences with them.
Re: Design Patterns by ektbear: 9:41am On Jul 21, 2012
Discuss further the factory design pattern. When would you want to use it?
Re: Design Patterns by harryobas: 10:06am On Jul 21, 2012
ekt_bear: Discuss further the factory design pattern. When would you want to use it?

The factory design pattern helps to improve cohesion and promotes separation of concerns by segregating the responsibility of object creation from domain objects to a pure fabrication "factory" object.
Re: Design Patterns by lordZOUGA(m): 10:13am On Jul 21, 2012
harryobas:

The factory design pattern helps to improve cohesion and promotes separation of concerns by segregating the responsibility of object creation from domain objects to a pure fabrication "factory" object.
an instance of where it can be used?
Re: Design Patterns by ektbear: 10:19am On Jul 21, 2012
this is one of those posts for which concrete examples are critical.

otherwise, nobody will gain anything from your post.
Re: Design Patterns by ektbear: 10:19am On Jul 21, 2012
In other words:

1. A poorly-designed piece of code
2. State what the problems are
3. Use the factory design pattern to improve it
Re: Design Patterns by harryobas: 10:38am On Jul 21, 2012
lordZOUGA:
an instance of where it can be used?

For example if u are building a class library that contains complex creation logic u can decide to introduce a factory object with the sole responsibility of handling object creation for clients of your library.
Re: Design Patterns by harryobas: 10:56am On Jul 21, 2012
ekt_bear: In other words:

1. A poorly-designed piece of code
2. State what the problems are
3. Use the factory design pattern to improve it

The purpose of this post was not to teach design patterns as there are many books and web resources that do that already the post was actually aimed at experienced developers who are familiar with and have applied design patterns.
Re: Design Patterns by Chimanet(m): 11:07am On Jul 21, 2012
For instance u have a code for returning a Vehicle object, all d different Vehicles instances lyk bus, lorry, saloon implement d Vehicle interface, u can have a factory class dat does nothing than using a a static method dat has a switch statement 2 return a particular implementation of Vehicle depending on d parameter u pass 2 it. public static Vehicle getVihecle(String type){.....} method. If you pass a bus string, it switches 2 bus case statement and instantiate a bus and returns 2 u. In an oop language lyk java, u can abstract all ur database connection creation code wit dis pattern since all d different jdbc implementation of database connection implements java.sql.Connection interface, oracle connection, mysql connection, etc, so u can easily swap d implementation of d connection u r using by jst changing d parameter u pass 2 a static getConnection method of a factory class. D purpose of d factory class is jst 2 return different implementations of connection objects shikena!
Re: Design Patterns by lordZOUGA(m): 11:42am On Jul 21, 2012
Chimanet: For instance u have a code for returning a Vehicle object, all d different Vehicles instances lyk bus, lorry, saloon implement d Vehicle interface, u can have a factory class dat does nothing than using a a static method dat has a switch statement 2 return a particular implementation of Vehicle depending on d parameter u pass 2 it. public static Vehicle getVihecle(String type){.....} method. If you pass a bus string, it switches 2 bus case statement and instantiate a bus and returns 2 u. In an oop language lyk java, u can abstract all ur database connection creation code wit dis pattern since all d different jdbc implementation of database connection implements java.sql.Connection interface, oracle connection, mysql connection, etc, so u can easily swap d implementation of d connection u r using by jst changing d parameter u pass 2 a static getConnection method of a factory class. D purpose of d factory class is jst 2 return different implementations of connection objects shikena!
so, in fact, I can replace a factory class with a function like this in C++;
enum Transport{CAR, BUS};
Vehicle getVehicle(Transport aTrans){switch(aTrans).... Return Car();}
put it in a header file and include it in all class defnitions. Yes?. Or is there an added benefit in making it a singleton? Encapsulation?
Re: Design Patterns by Chimanet(m): 12:22pm On Jul 21, 2012
Wen u use a factory class, u jst do Factory.getVehicle(parameter), because d getVehicle method is jst lyk a global function. in java we call it a static method, u dnt need 2 create an instance of d factory class (object) in d jvm heap, cos creating an object wit d new operator in java eats up jvm memory and is expensive and cause tight coupling. So factory pattern saves memory, makes our objects loosely coupled, and our code even looks more elegant. No nid 2 b creating objects of factory classes wit new operator. Jst load d class in d jvm and use its static method. Shikena!
Re: Design Patterns by ektbear: 12:26pm On Jul 21, 2012
Eh

Fair enough. If this post is targeted at a narrow audience, then so be it
Re: Design Patterns by harryobas: 12:30pm On Jul 21, 2012
lordZOUGA:
so, in fact, I can replace a factory class with a function like this in C++;
enum Transport{CAR, BUS};
Vehicle getVehicle(Transport aTrans){switch(aTrans).... Return Car();}
put it in a header file and include it in all class defnitions. Yes?. Or is there an added benefit in making it a singleton? Encapsulation?

A header file in C++ usually contains only function declarations and not implementations so even if u put the function in a header file u will still have to provide implementation in some sort of factory class definition.
Re: Design Patterns by lordZOUGA(m): 12:48pm On Jul 21, 2012
harryobas:

A header file in C++ usually contains only function declarations and not implementations so even if u put the function in a header file u will still have to provide implementation in some sort of factory class definition.
no. In C++, a header file can contain only function defnitions...
Re: Design Patterns by Chimanet(m): 12:52pm On Jul 21, 2012
Pls o, The factory pattern doesnt apply 2 java alone, it i can b illustrated with any oop language lyk csharp easily, i use java 2 illustrate it cos is the language am more proficient with, and most design pattern books normally use java as d language of illustration. U can easily implement it wit dynamic languages lyk php and python and i think is even more simpler 2 write, bt d concept of d pattern might b harder to grasp.
Re: Design Patterns by harryobas: 1:03pm On Jul 21, 2012
lordZOUGA:
no. In C++, a header file can contain only function defnitions...

No. I still maintain that a header file contains function declarations(prototypes) and not definitions. For example the cout function is declared in the iostream header file but defined/implemented in the standard runtime library.
Re: Design Patterns by lordZOUGA(m): 1:07pm On Jul 21, 2012
Chimanet: Wen u use a factory class, u jst do Factory.getVehicle(parameter), because d getVehicle method is jst lyk a global function. in java we call it a static method, u dnt need 2 create an instance of d factory class (object) in d jvm heap, cos creating an object wit d new operator in java eats up jvm memory and is expensive and cause tight coupling. So factory pattern saves memory, makes our objects loosely coupled, and our code even looks more elegant. No nid 2 b creating objects of factory classes wit new operator. Jst load d class in d jvm and use its static method. Shikena!
Elegance, I understand elegance.... So I do this instead
Car aCar = Factory::getCar(CAR):
I jus don't trust static methods shaa...
Re: Design Patterns by Chimanet(m): 1:21pm On Jul 21, 2012
@lordzouga, from ur analogy i think wat u are trying to depicts using header file in cplusplus looks more lyk interfaces 2 me in java and csharp
Re: Design Patterns by lordZOUGA(m): 1:21pm On Jul 21, 2012
harryobas:

No. I still maintain that a header file contains function declarations(prototypes) and not definitions. For example the cout function is declared in the iostream header file but defined/implemented in the standard runtime library.
really? How do you suppose inline statements work??
Foo.h
class Foo{
int aFoo;
public:
Foo(int num){aFoo = num;}
int getFoo(){return aFoo;}
};
you think the above class won't run?
Re: Design Patterns by Chimanet(m): 1:24pm On Jul 21, 2012
Yea, bt from my earlier illustration, wat u return is a vehicle, which can b cast down to a Car if u r sure is really a car. Static modifiers r very useful in an oop world if u understand how dey work
Re: Design Patterns by lordZOUGA(m): 1:25pm On Jul 21, 2012
Chimanet: @lordzouga, from ur analogy i think wat u are trying to depicts using header file in cplusplus looks more lyk interfaces 2 me in java and csharp
hmmm... I think java interface is more like virtual functions in C++.
Re: Design Patterns by Chimanet(m): 1:29pm On Jul 21, 2012
Anyway av not used cpp b4, i started programming wit java den csharp, php, python of recent
Re: Design Patterns by harryobas: 1:41pm On Jul 21, 2012
lordZOUGA:
really? How do you suppose inline statements work??
Foo.h
class Foo{
int Foo;
public:
Foo(int num){Foo = num;}
getFoo(){return Foo;}
};
you think the above class won't run?

Are u trying to define a class inside a header file? or are u including the header file in your class definition.
Re: Design Patterns by ektbear: 1:53pm On Jul 21, 2012
If anyone else is interested in providing a concise, clear explanation with code examples of when to use the factory design pattern, I am sure that I am not the only person who would find that useful.
Re: Design Patterns by harryobas: 1:58pm On Jul 21, 2012
@lordZOUGA I guess u are right with regards to inline functions becos inline functions must be defined in every source file in which they are used. This is typically accomplished by defining them in a header file which u must include in each source file that calls the inline functions.
Re: Design Patterns by lordZOUGA(m): 2:03pm On Jul 21, 2012
harryobas:

Are u trying to define a class inside a header file? or are u including the header file in your class definition.
I defined the class in a header file. I could have included the #IFNDEF pre-processor directive but this is trivial...
So in main.cpp, I do this:
#include "Foo.h"
#include <iostream>
int main(){
int x = 6;

Foo myFoo(x);
int y = myFoo.getFoo();

cout << y;
return 0;
}

this works
Re: Design Patterns by harryobas: 2:13pm On Jul 21, 2012
lordZOUGA:
I defined the class in a header file. I could have included the #IFNDEF pre-processor directive but this is trivial...
So in main.cpp, I do this:
#include "Foo.h"
#include <iostream>
int main(){
int x = 6;

Foo myFoo(x);
int y = myFoo.getFoo();

cout << y;
return 0;


}

this works
Re: Design Patterns by harryobas: 2:16pm On Jul 21, 2012
Ok i see what u mean, i must confess i am more of a C# developer but i do play around with C++ from time to time.
Re: Design Patterns by harryobas: 2:29pm On Jul 21, 2012
Chimanet: Wen u use a factory class, u jst do Factory.getVehicle(parameter), because d getVehicle method is jst lyk a global function. in java we call it a static method, u dnt need 2 create an instance of d factory class (object) in d jvm heap, cos creating an object wit d new operator in java eats up jvm memory and is expensive and cause tight coupling. So factory pattern saves memory, makes our objects loosely coupled, and our code even looks more elegant. No nid 2 b creating objects of factory classes wit new operator. Jst load d class in d jvm and use its static method. Shikena!

You could also implement a factory as a singleton which creates one global instance of the factory class.
Re: Design Patterns by Chimanet(m): 2:42pm On Jul 21, 2012
Who among us is a java developer? D Calendar class, Calender.getInstance() is d clearest example of factory pattern in the java class library
Re: Design Patterns by harryobas: 2:49pm On Jul 21, 2012
Chimanet: Who among us is a java developer? D Calendar class, Calender.getInstance() is d clearest example of factory pattern in the java class library

I am a C# developer but i think your above Calender.getInstance() method looks more like a singleton to me than a factory
Re: Design Patterns by Chimanet(m): 2:54pm On Jul 21, 2012
harryobas:

You could also implement a factory as a singleton which creates one global instance of the factory class.
yeah, is true, lyk d hibernatefactory in hibernate orm framework and the spring beanfactory in spring framework are all singletons. But in gwt d GWT Class is a classic example of a factory cause i have not seen where it was instantiated, i think d constructor is private jst lyk d Calender class constructor. Ok not all factories r singletons, a singleton has only 1 instance in an entire jvm

(1) (2) (Reply)

Final Year Web Development Project: Help Needed Please. / Sharepoint Database Recovery Tool / Lets Talk Node.js

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 44
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.