哪种设计模式适合以下 api 实施?

Which design pattern would be appropriate for the following api implementation?

我想将 oEmbed 标签添加到我的站点(我是 oEmbed api 提供商)。我的 api 应该根据文件类型响应结果。

o嵌入类型有

我对照片的回复包含以下字段

{
    "author_name": "rajasuba.s",
    "author_url": <author_image_url>,
    "thumbnail_width": 130,
    "provider_url": <provider_url>,
    "thumbnail_url": "<thumbnail_image_url>",
    "title": "Picture.png",
    "provider_name": "XYZ",
    "type": "photo",
    "version": "1.0",
    "url": "<given_url>",
    "thumbnail_height": 120
}

我对 视频 的回复包含以下字段

{
    "author_name": "rajasuba.s ",
    "author_url": "<image_url_of_author>",
    "thumbnail_width": 130,
    "html": "<iframe src="<source_url>" width=\"480\" height=\"270\" frameborder=\"0\">",
    "provider_url": "<service_url>",
    "thumbnail_url": "<thumbnail_image_url>",
    "title": "video_small_resource.mp4",
    "provider_name": "XYZ",
    "type": "video",
    "version": "1.0",
    "thumbnail_height": 120
}

对于 linkrich 类型也是如此。

我正在通过以下方式实现此 api。我只有一个 servlet(api 请求到达的地方)。这里我有以下内容

public class OEmbedServlet extends HttpServlet {
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
    //Parse request uri

      String format = request.getParameter(“format”);
      String url = request.getParameter(“url”);
      String file_id = request.getParameter(“file_id”);

      String max_width = request.getParameter(“max_height”);
      String max_height = request.getParameter(“max_width”);

          if(authorised_user) {
            oembed.setFileInfo(file_id);
            oembed.setProviderInfo();
            oembed.setURL(url);
            oembed.setThumbnailInfo();
            oembed.setOEmbedType();
          }

     writeResponse(response, oembed.getJSONObject(), format);
   }
}

还有另一个 class 为这个 servlet 做所有实用工作

public class OEmbed {
private HttpServletRequest request;

public OEmbed(HttpServletRequest request) {
this.request = request;
this.oembedType = OEmbedType.LINK;
this.width = 0;
this.height = 0;
this.thumbnailWidth = 0;
this.thumbnailHeight = 0;
}

public enum OEmbedType {
RICH/*0*/,
LINK/*1*/,
PHOTO/*2*/,
VIDEO/*3*/
}

public void String author;
public void String file_id;
public void String extension;
public void String fileType;

//Getter and setter methods for all required info to be passed in the response like 

public String getAuthorName() {
return this.author;
}

public String setAuthorName(String name) {
this.author = name;
}

public void setURL(String url) {
this.url = url;
}

public String getURL(String url) {
return this.url;
}

//…. and other getter and setter methods
/*
- Few setter methods are invoked from the servlet
- Few setter methods are clubbed together and invoked from util classes
- The setter methods in util does some computation to assign value - or they are assigned based on inputted params
- All required getter methods are obtained while writing response json
*/

public JSONObject getJSONObject(boolean isAuthorised) throws Exception
{
JSONObject oembedObj = new JSONObject();
if(this.url != null && !this.url.isEmpty()) {
switch(this.oembedType) {
case PHOTO:
oembedObj.put("url", this.thumbnailUrl);
break;
case LINK:
oembedObj.put("url", this.url);
default:
oembedObj.put("url", this.url);
oembedObj.put("html", htmlContent);
break;
}

if(this.thumbnailUrl != null && !this.thumbnailUrl.isEmpty()) {
oembedObj.put(“thumbnail_url”, this.thumbnailUrl);
oembedObj.put(“thumbnail_width”, this.thumbnailWidth);
oembedObj.put(“thumbnail_height”, this.thumbnailHeight);
}

}
}

我还是觉得这个设计很繁琐。我觉得有以下几点不方便,

举个例子

public void setThubnailUrl(String url) {
this.thumbnail_url = url;
}
public void setThubnailUrl() {
setThumbnailInfo();
getThumbnailStatus();
setThumbnailUrl(url);    //So before initialising this url - i have to make sure manually - whether the required params for thumbnail url is initialised already (I'm not sure weather it is a best practice to do like this)
}

我怎样才能更好地组织它?哪种设计模式适合以下情况?欢迎提出任何建议:-)

首先,您现在有一个 header 和一个包含在单个消息中的内容,这使得在客户端和服务器端处理起来更加困难。

将消息分成两部分。

header 描述了一般元数据,例如作者、生成器服务和内容类型。每个 contentType 的内容都不同。

通过这种小的分离,您可以自由创建不同的内容构建器,而不会影响一般处理程序。

这样就可以在这部分引入工厂模式,让不同的classes产生不同的内容

由于我是一名 .NET 程序员,所以我无法为您提供代码。但通常你有一个 hashmap,其中键是 contentType,值是创建内容的 class 的因子方法。

伪代码:

var classInstance = _factoryMap[requestedContentType].CreateInstance();
var content = classInstance.CreateContent(someResourceId);

//create response here.