Professional Documents
Culture Documents
Factory pattern
In Factory pattern, we create object without exposing the creation logic to the client and refer to
newly created object using a common interface.
Interface
OS:class
public interface OS {
void spec();
}
IOS.class
@Override
public void spec() {
System.out.println("Good, but closed");
}
Windows.class
@Override
public void spec() {
System.out.println("Not so good");
}
}
Linux.class
@Override
public void spec() {
System.out.println("The BEST");
}
Objects factory
if(str.equals("closed")){
return new IOS();
} else if (str.equals("good")){
return new Linux();
} else {
return new Windows();
}
}
}
Main
Factory Generator
OSAbstractFactory.class
LaptopFactory.class
@Override
Device getDeviceInfo(DeviceType deviceType) {
switch(deviceType){
case HP:
return new HP();
case DELL:
return new Dell();
case ASUS:
return new Asus();
}
return null;
}
MobileFactory.class
@Override
Device getDeviceInfo(DeviceType deviceType) {
switch(deviceType){
case ANDROID:
return new Android();
case DELL:
return new Dell();
case NOKIA:
return new Nokia();
}
return null;
}
}
DeviceType.class
DELL,
HP,
NOKIA,
ASUS,
ANDROID
}
Device.class
Dell.class
@Override
public String getDetails() {
String str = "Dell";
System.out.println(str);
return str;
}
}
Singleton
In object-oriented programming, a singleton class is a class that can have only one object (an
instance of the class) at a time.
return single_instance;
}
}
Builder
Builder pattern is used to create instance of very complex object having telescoping
constructor in easiest way. Constructors in Java are used to create object and can take
parameters required to create object. Lets see an example and learn how to implement
builder pattern.
RobotPlan.class
Robot.class
@Override
public void setRobotHead(String head) {
robotHead = head;
}
@Override
public void setRobotTorso(String torso) {
robotTorso = torso;
}
@Override
public void setRobotArms(String arms) {
robotArms = arms;
}
@Override
public void setRobotLegs(String legs) {
robotLegs = legs;
RobotBuilder.class
void buildRobotHead();
void buildRobotTorso();
void buildRobotArms();
void buildRobotLegs();
Robot getRobot();
}
RobotBuilderImpl.class
public RobotBuilderImpl() {
this.robot = new Robot();
}
@Override
public void buildRobotHead() {
robot.setRobotHead("Head");
}
@Override
public void buildRobotTorso() {
robot.setRobotTorso("Torso");
}
@Override
public void buildRobotArms() {
robot.setRobotArms("Arms");
}
@Override
public void buildRobotLegs() {
robot.setRobotLegs("Legs");
}
@Override
public Robot getRobot() {
return this.robot;
}
}
RobotEngineer.class
BuilderMain.class
public class BuilderMain {
public static void main(String[] args) {
RobotBuilder builder = new RobotBuilderImpl();
RobotEngineer engineer = new RobotEngineer(builder);
engineer.makeBot();
Prototype
Prototype is a creational design pattern that allows cloning objects, even complex ones,
without coupling to their specific classes.
Book.class
@Override
public String toString() {
return "Book{" + "id=" + id + ", name=" + name + '}';
}
}
BookShop.class
}
}
@Override
public String toString() {
return "BookShop{" + "shopName=" + shopName + ",
books=" + books + '}';
}
return bs;
}
MainPrototype.class
Logger.getLogger(MainPrototype.class.getName()).log(Level.SEVE
RE, null, ex);
}
bs1.setShopName("Shop 2");
System.out.println(bs1);
System.out.println(bs == bs1);
}
Structure patterns
Adapter
Convert the interface of a class into another interface clients expect. Adapter lets classes
work together that couldn't otherwise because of incompatible interfaces. Wrap an existing
class with a new interface. Impedance match an old component to a new system