Reputation: 137
I have the following class:
public class A {
private String a;
private Long b;
private Date c;
//getters, setters, constructors
}
And I need to be able to export it as an excel with different templates. For example, one excel has the following headers: a b c
Another excel has the following template: b a c
And another template could look like this: b a
What would be a good way to design the code so that I could format the A object according to one template or another? Is there a particular design pattern I can look at?
Upvotes: 1
Views: 110
Reputation: 4701
I think you can use Adapter pattern, you need to make different adapter for different templates.
Bind your object with the adapter and some logic in adapter will assign value from your passed object to the template where you want to fill the values.
By making adapter you can make new format without making change in existing format, this will make code more maintainable and separate.
This is just a rough idea which concept can be apply here, you still have scope of making many modification in it.
Adapter Interface
public interface Adapter {
}
Adapter class
public class Format2Adapter implements Adapter {
private Long title;
String subtitle;
public void fillTemplate(Core c){
title = c.getB();
}
}
Format2Adapter class
public class Format2Adapter implements Adapter {
private Long title;
String subtitle;
public void fillTemplate(Core c){
title = c.getB();
}
}
Business Object class
import java.util.Date;
public class Core {
private String a;
private Long b;
private Date c;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public Long getB() {
return b;
}
public void setB(Long b) {
this.b = b;
}
public Date getC() {
return c;
}
public void setC(Date c) {
this.c = c;
}
// getters, setters, constructors
}
Upvotes: 2
Reputation: 3778
There are a lot of design patterns you can use, however, from first glance (this is also a matter of personal taste), it seems that the Strategy design pattern would be a perfect fit.
What I would do, is implement all of the different templates as different export strategies, that will implement a base interface (or extend a base subclass in case there's some common functionality):
public interface ExportStrategy {
public void export(A a);
}
The subclasses might implement different strategies:
public class ABCExportStrategy {
...
}
public class BACExportStrategy {
...
}
etc.
The A
class, then becomes:
public class A {
// all the code you wrote in the question body goes here ...
public void export(ExportStrategy strategy) {
strategy.export(this);
}
}
Or, alternatively, you can have the strategy be a state of the A class:
public class A {
// all the code you wrote in the question body goes here ...
private ExportStrategy strategy;
// setter for the strategy
public void export() {
strategy.export(this);
}
}
Upvotes: 1
Reputation: 37576
KISS:
public class A {
private String a;
private Long b;
private Date c;
//getters, setters, constructors
public void ExportData(Template template){
switch (template.type){
case t1:
formatT1();
break;
case c2:
formatT2();
break;
default:
defaultBehavious();
}
}
Upvotes: 0