I have two employees in my organization : Manoj and Kumar
I wish to have Manoj's employee data in text format and Kumar's employee data in HTML format
What I would do to get it done :
1. I will create Employee instance called Manoj
2. I will ask Manoj to accept Textformatter as it's formatter
3. Once Manoj accept it , it will ask it's Formatter to apply text formatter on it's data
Code for this is as below :
------------------------------------------
package com;
public class Employee {
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
String name;
public Employee() {
// TODO Auto-generated constructor stub
}
public void accept(Formatter visitor) {
visitor.visit(this);
}
}
I wish to have Manoj's employee data in text format and Kumar's employee data in HTML format
What I would do to get it done :
1. I will create Employee instance called Manoj
2. I will ask Manoj to accept Textformatter as it's formatter
3. Once Manoj accept it , it will ask it's Formatter to apply text formatter on it's data
Code for this is as below :
------------------------------------------
package com;
public class Employee {
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
String name;
public Employee() {
// TODO Auto-generated constructor stub
}
public void accept(Formatter visitor) {
visitor.visit(this);
}
}
--------------------------------------------
package com;
public interface Formatter {
void visit(Employee emp);
String getResult();
}
------------------------------------------
package com;
public class TextFormat implements Formatter {
String formatting;
public TextFormat() {
// TODO Auto-generated constructor stub
}
@Override
public void visit(Employee emp) {
formatting= "formatted the "+ emp.getName()+ " employee data in text format";
}
public String getResult(){
return formatting;
}
}
--------------------------------------------
package com;
public class Test {
public Test() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
Employee emp =new Employee();
Formatter visitor =new TextFormat();
emp.setName("Manoj");
emp.accept(visitor);
System.out.println(visitor.getResult());
}
}
----------------------------------------------
Similarly for Kumar to have it's data formatted in HTML format
1. I will create Employee instance called Kumar
2. I will ask Kumar to accept HTMLformatter as it's formatter
3. Once KUMAR accept it , it will ask it's Formatter to apply HTML formatter on it's data
Code for this is :
---------------------------------------
package com;
public class HTMLFormat implements Formatter {
String formatting;
public HTMLFormat() {
// TODO Auto-generated constructor stub
}
@Override
public void visit(Employee emp) {
formatting= "formatted the "+ emp.getName()+ " empolyee data in HTML format";
}
public String getResult(){
return formatting;
}
}
-----------------------------------
package com;
public class Test {
public Test() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
Employee emp =new Employee();
Formatter visitor =new TextFormat();
visitor =new HTMLFormat();
emp.setName("Kumar");
emp.accept(visitor);
System.out.println(visitor.getResult());
}
}
-----------------------------------------
Test class to collectively apply formatters on Manoj and Kumar below Code could be used in single Test class:
----------------------------------------
package com;
public class Test {
public Test() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
Employee emp =new Employee();
Formatter visitor =new TextFormat();
emp.setName("Manoj");
emp.accept(visitor);
System.out.println(visitor.getResult());
visitor =new HTMLFormat();
emp.setName("Kumar");
emp.accept(visitor);
System.out.println(visitor.getResult());
}
}
-------------------------------
No comments:
Post a Comment