Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 哪种设计模式适用于以下api实现?_Java_Api_Oop_Design Patterns_Oembed - Fatal编程技术网

Java 哪种设计模式适用于以下api实现?

Java 哪种设计模式适用于以下api实现?,java,api,oop,design-patterns,oembed,Java,Api,Oop,Design Patterns,Oembed,我想向我的站点添加oEmbed标记(我是oEmbedAPI提供商)。我的api应该根据文件类型响应结果 oEmbed类型具有 照片 录像带 链接 丰富的 我对照片的回复包含以下字段 { "author_name": "rajasuba.s", "author_url": <author_image_url>, "thumbnail_width": 130, "provider_url": <provider_url>, "thu

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

oEmbed类型具有

  • 照片
  • 录像带
  • 链接
  • 丰富的
我对照片的回复包含以下字段

{
    "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
}
还有一个类,它为这个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);
}

}
}
我仍然觉得这个设计很麻烦。我在以下事情上感到不方便

  • 从servlet调用的setter方法很少,从util类调用的方法也很少
  • 另外,在使用util类中的类变量时,我必须小心那些属性值是否已经初始化
比如说

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)
}

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

首先,您现在将一个标题和一个内容烘焙到一条消息中,这使得在客户端和服务器上都很难处理

把信息分成两部分

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

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

因此,您可以为该部件引入工厂模式,并让不同的类生成不同的内容

因为我是一个.NET程序员,所以我不能给你代码。但通常您有一个hashmap,其中键是contentType,值是创建内容的类的factor方法

伪代码:

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

//create response here.

请添加进一步改进问题的建议..请查看(在发布问题之前一定要阅读他们的帮助中心)谢谢@ErwinBolwidt。。当然可以(y)
var classInstance = _factoryMap[requestedContentType].CreateInstance();
var content = classInstance.CreateContent(someResourceId);

//create response here.