To apply flyweight pattern, we need to divide Object property into intrinsic and extrinsic properties. Intrinsic properties make the Object unique whereas extrinsic properties are set by client code and used to perform different operations.
But my question is why can't we have both intrinsic and extrinsic property as an instance variable (See Email class below) and just create one object outside loop and set the parameters in loop and send multiple emails with different parameters.
public class Test {
public static void main(String[] args) {
Email ob = new Email();
for (int i = 0; i < 10; i++) {
ob.sender = String.valueOf(i);
ob.sendEmail();
}
}
}
public class Email {
public String sender;
public void sendEmail()
{
System.out.println("Email sent to sender:"+sender);
}
}
Aucun commentaire:
Enregistrer un commentaire