• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Patterns

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If possible give me some simple examples involving
Prototype,Singleton,Factory Method,Builder patterns.
-Nats
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singleton is pretty simple. If you want only one instance of a class to exist at any one time, you use the Singleton pattern. The trick is to make the constructor private and add a static instance() method. The instance() method would check to see if an instance already exists...
if it doesn't it creates one, stores the pointer to it in a static variable, and returns the pointer.
If it does exist, it returns the pointer.
<pre>
public class SingletonSample {
private static SingletonSample singletonSample = null;
private SingletonSample() {}
public static SingletonSample instance() {

if (singletonSample == null)
singletonSample = new SingletonSample();
return singletonSample;
}
}

</pre>
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Builder is when you wish to create a complex or aggregate object and builder encapsulates the creation and assembly of the parts. For example let's just say a car consists of the wheels, the engine and the remainder (body, etc). So to create the car, I need to create the wheels, create the engine, create the remainder and assemble this into the car. The CarBuilder would handle all of these details avoiding a very messy, overbloated Factory.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
take a look at the pattern depo it has examples
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember when I was reading about Prototype in GoF, I decided that in Java it's equal to defining interface - which clients will know about - and set of concrete implementations to fill it. Or an abstract class with a Factory method. Is there anything I missed?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface can not implement any method while an abstract class can still have concrete implementations of its member functions. So by default an interface is a special case of an abstract class.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic