Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Advanced Programming: Effective Java - Chapter 2 - Item 1&2

No description
by

Ahmed Abd El-Kader

on 3 March 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Advanced Programming: Effective Java - Chapter 2 - Item 1&2

Effective Java Item 1
Consider providing static factory
methods instead of constructors Item 2 Chapter 2 : Creating and Destroying Objects Item 1 : Consider providing static factory methods instead of constructors. Item 2 : Enforce the singleton property with a private constructor Team 1 :
- Ebrahim Mohamed.
- Ahmed Ahmed Abd El-Kader.
- Fady Magdy.
- Marwa Mohamed.
- Moustafa Mahmoud. How can we get an instance of a class ?!! 1- Public Constructor.
2- Static Factory Method. What does the factory method do ?!! Example public static Boolean valueOf (boolean b)
{
return (b ? Boolean.TRUE : Boolean.FALSE);
} Factory Constructor Why do we use factory method ?!! Advantage 1 Static factory methods have names Well chosen names make
the code easier to read Not restrictive like constructor Advantage 2 Not required to create an object Avoid creating unnecessary duplicate objects Maintain strict control over instances Advantage 3 Can return an object of any subtype of
the return type Gives you flexibility in choosing
the class of the return object What is wrong with factory ?!! Disadvantage 1 Classes without public or protected
constructors cannot be subclassed Disadvanatge 2 They are not readily distinguishable
from other static methods Enforce the singleton property
with a private constructor A singleton is simply a class that is instantiated exactly once Singletons typically represent some system component that is intrinsically unique, such as a video display or file system. What is a singleton ?!! How to implement singletons ?!! Two approaches: Both based on keeping the constructor private and providing a public static member to allow clients access to the sole instance of the class. First approach the public static member is a final field: // Singleton with final field
public class Elvis
{
public static final Elvis INSTANCE = new Elvis();
private Elvis()
{
}
... // Remainder omitted
} Second approach static factory method public class Elvis
{
private static final Elvis INSTANCE = new Elvis();
private Elvis()
{
...
}
public static Elvis getInstance()
{
return INSTANCE;
}
... // Remainder omitted
} Which approach should be used ?!! Why first approach? The public static field is final, so the field will always
contain the same object reference. Why second approach ?!! change your mind about whether the class should be a singleton without changing the API. The static factory method for a singleton returns the sole instance of the class but could easily be modified to return, a unique instance for each thread Questions !!! Thank You Example public class DbConnection
{
private static final int MAX_CONNS = 100;
private static int totalConnections = 0;
private static Set<DbConnection> availableConnections = new HashSet<DbConnection>();

private DbConnection()
{
totalConnections++;
}

public static DbConnection getDbConnection()
{
if(totalConnections < MAX_CONNS)
{
return new DbConnection();
}

else if(availableConnections.size() > 0)
{
DbConnection dbc = availableConnections.iterator().next();
availableConnections.remove(dbc);
return dbc;
}

else
throw new NoDbConnections();
}

} Example public interface Car{ //...}
public class BMW implements Car {//...}
public class Toyota implements Car {//...}

Main()
{
BMW x= new BMW();
.
.
} Example public class CarFactory
{
public static Car GetCar()
{
return new BMW();
}
}

Main()
{
Car x=new CarFactory.GetCar();
.
.
}
Full transcript