我是否混淆了不同的设计模式?
Am I mixing up different design patterns?
在我的应用程序中,我目前有一个 URL 模型 class 和一个 URL 查询 class。
我的 URL class 有一些属性和一个使用注入数据库 class 将对象及其属性保存到数据库的保存方法。
我的 URL 查询 class 有一种方法可以接受某些条件并 return 从数据库中获取行并将它们实例化为 URL 来自 URL class.
但是最近,我开始觉得我的代码可能混合了不同的设计模式。例如,URL 模型 class 需要将数据库 class 注入其中,实例化 URL class 并在另一个 class 中设置它的依赖关系感觉很乱=28=] 这可能只是为了抓取 URL 等
所以这是我考虑使用具有数据库依赖项的工厂 class 并将其注入我的其他 classes 的时候。它可以有这样的方法:
- create - 这将接受参数并在那里实例化一个 URL class 然后 return 它。
- 保存 - 这将获取一个 URL 对象并将其保存到数据库
这有意义吗?
我的 URL 查询是否也有意义,或者我错过了使用另一种设计模式?
我只是很困惑,正如我在很多教程中看到的那样,直接在模型中使用保存方法 class 等
让我们从现有的开始。首先 URL 是一个模型 class 并且 URL 查询似乎是一个 handler/controller class。你有一个需要 CRUD 操作的数据库,它可以进入控制器 class 本身,比方说 DB.
现在让我们尝试分离出功能。也就是说,我的意思是模型不应该关心它的数据来自哪里。同样,模型和控制器都不需要知道数据库是如何设置的 and/or 无论数据库是否是关系数据库。将控制器仅视为逻辑流处理程序。 DBWrapper 不需要知道它正在保存哪个 class 的什么对象,控制器应该只告诉它以 Y 格式保存 X 数据以保存在 Z table.
那么,我会怎么做呢
class URL
{
// Variables/methods go here
String Serialize()
{
// This is the data that gets saved to DB. You could return Object, to hide the type
}
static URL Deserialize(String serializedURLModel)
{
Object xyz = /* extract data from serializedURLModel here*/;
return new URL(xyz);
}
}
然后控制器 class 接管控制权,并驱动逻辑...
public class URLQuery
{
DB db;
public void createURL(URL url)
{
if(!db.HasTable(URLTable))
db.createTable(URLTable); //Assume that you are creating columns and their types here.
db.put(URLTable, url.GetID(), url.Serialize());
}
public void getURL(String urlID)
{
return URL.Deserialize(db.getObjectforColumn(URLTable, urlID));
}
}
最后,我会有一个 class DBWrapper 并拥有它 return DB,这是一个接口。所以在 DBWrapper class 之外,controller 并不知道它是哪个 db 以及它是如何实现的。类似于以下内容:
interface DB
{
void createTable(String Tablename, ... etc);
void GetObjectForColums(String tableName, String column);
}
public class DBWrapper
{
public static getDB()
{
return new MySQL_DB();
}
private class MySQL_DB implements DB
{
void createTable(String Tablename, ... etc) // implement it here
void GetObjectForColums(String tableName, String column) //implementation
}
}
希望对您有所帮助:)
我理解你的困境。当我们开始学习设计模式时,我们开始认为它们 should/must 总是被使用而忘记了我们试图解决的问题。在识别问题之前识别设计模式本身就是一个问题。
你的场景其实很简单,所以代码也要简单。 KISS.
你只需要一个 model/entity URL,一个 class 到 save/read 来自 db,然后是一个只需要 URLs 的爬虫,没有别的.
public class URL {
// only properties
}
// You inject this repository whenever you want to work with db
public class URLRepository {
// This will talk to db
public URL Get(<param>) { } // fetch from db
public int Save(<param>) { } // save to db, and return it's ID
// .. and so on
}
public class Crawler {
// This is just concerned about URL objects
// nothing else
}
在我的应用程序中,我目前有一个 URL 模型 class 和一个 URL 查询 class。
我的 URL class 有一些属性和一个使用注入数据库 class 将对象及其属性保存到数据库的保存方法。
我的 URL 查询 class 有一种方法可以接受某些条件并 return 从数据库中获取行并将它们实例化为 URL 来自 URL class.
但是最近,我开始觉得我的代码可能混合了不同的设计模式。例如,URL 模型 class 需要将数据库 class 注入其中,实例化 URL class 并在另一个 class 中设置它的依赖关系感觉很乱=28=] 这可能只是为了抓取 URL 等
所以这是我考虑使用具有数据库依赖项的工厂 class 并将其注入我的其他 classes 的时候。它可以有这样的方法:
- create - 这将接受参数并在那里实例化一个 URL class 然后 return 它。
- 保存 - 这将获取一个 URL 对象并将其保存到数据库
这有意义吗? 我的 URL 查询是否也有意义,或者我错过了使用另一种设计模式? 我只是很困惑,正如我在很多教程中看到的那样,直接在模型中使用保存方法 class 等
让我们从现有的开始。首先 URL 是一个模型 class 并且 URL 查询似乎是一个 handler/controller class。你有一个需要 CRUD 操作的数据库,它可以进入控制器 class 本身,比方说 DB.
现在让我们尝试分离出功能。也就是说,我的意思是模型不应该关心它的数据来自哪里。同样,模型和控制器都不需要知道数据库是如何设置的 and/or 无论数据库是否是关系数据库。将控制器仅视为逻辑流处理程序。 DBWrapper 不需要知道它正在保存哪个 class 的什么对象,控制器应该只告诉它以 Y 格式保存 X 数据以保存在 Z table.
那么,我会怎么做呢
class URL
{
// Variables/methods go here
String Serialize()
{
// This is the data that gets saved to DB. You could return Object, to hide the type
}
static URL Deserialize(String serializedURLModel)
{
Object xyz = /* extract data from serializedURLModel here*/;
return new URL(xyz);
}
}
然后控制器 class 接管控制权,并驱动逻辑...
public class URLQuery
{
DB db;
public void createURL(URL url)
{
if(!db.HasTable(URLTable))
db.createTable(URLTable); //Assume that you are creating columns and their types here.
db.put(URLTable, url.GetID(), url.Serialize());
}
public void getURL(String urlID)
{
return URL.Deserialize(db.getObjectforColumn(URLTable, urlID));
}
}
最后,我会有一个 class DBWrapper 并拥有它 return DB,这是一个接口。所以在 DBWrapper class 之外,controller 并不知道它是哪个 db 以及它是如何实现的。类似于以下内容:
interface DB
{
void createTable(String Tablename, ... etc);
void GetObjectForColums(String tableName, String column);
}
public class DBWrapper
{
public static getDB()
{
return new MySQL_DB();
}
private class MySQL_DB implements DB
{
void createTable(String Tablename, ... etc) // implement it here
void GetObjectForColums(String tableName, String column) //implementation
}
}
希望对您有所帮助:)
我理解你的困境。当我们开始学习设计模式时,我们开始认为它们 should/must 总是被使用而忘记了我们试图解决的问题。在识别问题之前识别设计模式本身就是一个问题。
你的场景其实很简单,所以代码也要简单。 KISS.
你只需要一个 model/entity URL,一个 class 到 save/read 来自 db,然后是一个只需要 URLs 的爬虫,没有别的.
public class URL {
// only properties
}
// You inject this repository whenever you want to work with db
public class URLRepository {
// This will talk to db
public URL Get(<param>) { } // fetch from db
public int Save(<param>) { } // save to db, and return it's ID
// .. and so on
}
public class Crawler {
// This is just concerned about URL objects
// nothing else
}