我通常遵循全局变量/函数是邪恶的规则,并且每一段代码都应该存在于它所属的类中.
这是一个非常容易遵循的规则,我相信到目前为止我还没有遇到过这条规则的问题.
然而,今天,我需要的功能添加到我的组装,而不是一个特定的类.也就是说,我的几乎所有类都可以用于这个特定的功能.
我应该把这个功能放在哪里(+1重载)?
如果我把它放在"公用事业"课程中,我会觉得很脏.如果我将它添加到一个半相关的类,并让其他类直接调用它,我会感觉更糟.
这段特殊的代码基本上是IList<PointF>一个规范化的列表.我觉得现在添加它作为扩展方法IList<PointF>可能是最好的选择...
我想创建一个带有实用程序方法的类
public class Util {
public static void f (int i) {...}
public static int g (int i, int j) {...}
}
Run Code Online (Sandbox Code Playgroud)
哪个是创建实用程序类的最佳方法?
我应该使用私有构造函数吗?
我应该为抽象类创建实用程序类吗?
我什么都不做?
在React-Redux应用程序中,我有一个这样的状态:
state = {
items: [
{ id: 1, first_name: "John", last_name: "Smith", country: "us" },
{ id: 2, first_name: "Jinping", last_name: "Xi", country: "cn" },
]
}
Run Code Online (Sandbox Code Playgroud)
我使用React组件渲染.
现在我需要一个能给我一个人"全名"的功能.所以它不仅仅是"first_name + last_name",而是取决于国家(例如,它在中国将是"last_name + first_name"),所以有一些相对复杂的逻辑,我想把它包装在一个可以从任何地方使用的函数中反应组件.
在OOP中,我会创建一个Person::getFullName()方法来提供这些信息.然而,state对象是一个"哑"的对象,其中子对象没有任何专门的方法.
那么一般来说,在React-Redux中管理它的推荐方法是什么?我能想到的只是创建一个全局函数,例如,user_getFullName(user)它会占用用户并返回全名,但这不是很优雅.有什么建议吗?
在过去十年左右的时间里,我一直在使用下面的模式来实现Java实用程序类.该类仅包含静态方法和字段,声明final为无法扩展,并且具有private构造函数,因此无法实例化.
public final class SomeUtilityClass {
public static final String SOME_CONSTANT = "Some constant";
private SomeUtilityClass() {}
public static Object someUtilityMethod(Object someParameter) {
/* ... */
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,随着Java 8 中接口中静态方法的引入,我最近发现自己使用了一个实用程序接口模式:
public interface SomeUtilityInterface {
String SOME_CONSTANT = "Some constant";
static Object someUtilityMethod(Object someParameter) {
/* ... */
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
这使我摆脱构造,和大量的关键字(个public,static,final),这些接口中隐含的.
这种方法有什么缺点吗?在实用程序界面上使用实用程序类有什么好处吗?
有没有在Java中的实用工具方法它转换Boolean成boolean和自动处理空引用Boolean假?
这是一个实用方法的例子:
public static Long getFileSize(String fileString) {
File file = new File(fileString);
if (file == null || !file.isFile())
return null;
return file.length();
}
Run Code Online (Sandbox Code Playgroud)
将String而不是File传递给这样的方法是一种好习惯吗?一般来说,在制作这种风格的实用方法时应该采用什么推理?
在这里,我有一个实用程序类,其中有一个用于显示 DialogBox 的函数,所以我正在尝试制作一个 AlertDialog Box,它可以在整个项目的任何地方使用。
因此,我必须将 Title、description 作为参数传递,并且还想传递 Class name,以便在按下警报对话框内的按钮时我们可以导航到该屏幕
class DialogBox {
static DialogBox dialogBox = null;
static DialogBox getInstance() {
if (dialogBox == null) {
dialogBox = DialogBox();
}
return dialogBox;
}
showAlertDialog(BuildContext context, String alertTitle, String alertMessage) {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
title: Center(child: Text(alertTitle)),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
alertMessage,
textAlign: TextAlign.center,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
FlatButton(
child: …Run Code Online (Sandbox Code Playgroud) 我正在使用C#和Razor在ASP.NET MVC3中开发一个Web应用程序.
我需要创建一个实用程序类,我将函数转换为将字符串转换为日期(年,月,日等等).
在ASP.NET Web窗体中,我曾经将这种类放在App_Code文件夹中.在MVC中没有这样的文件夹,我不认为实用程序类既不属于Models也不属于Helpers(我创建的文件夹,用于将我的扩展放在HTML Helpers上).
我读到将实用程序类放在不同的程序集中是一种很好的做法.我想一个不同的项目应该做的工作,但我应该创建什么样的项目?普通的类库项目对我来说似乎是最合乎逻辑的选择.
但是在我的情况下,我只需要使用多个方法放置一个单独的类,因此,如果我们忽略了可重用性,那么将实用程序类放在我的MVC3 Web应用程序中的某个地方是不是更合乎逻辑?
我有一个文件和目录列表List<string> pathes.现在我想计算每个路径彼此共享的最深的公共分支.
我们可以假设它们都有共同的路径,但这在开始时是未知的.
假设我有以下三个条目:
这应该得到结果:C:/ Hello /因为地球正在打破子目录的这个"链".
第二个例子:
- > C:/ Hello/World/This/Is /
你会怎么做?我尝试使用string.split(@"/")并从第一个字符串开始,并检查此数组的每个部分是否包含在其他字符串中.但是,这将是一个非常昂贵的调用,因为我正在迭代(list_of_entries)^ list_of_entries.有没有更好的解决方案?
我目前的尝试将类似于以下(C#+ LINQ):
public string CalculateCommonPath(IEnumerable<string> paths)
{
int minSlash = int.MaxValue;
string minPath = null;
foreach (var path in paths)
{
int splits = path.Split('\\').Count();
if (minSlash > splits)
{
minSlash = splits;
minPath = path;
}
}
if (minPath != null)
{
string[] splits = minPath.Split('\\');
for (int i = 0; i …Run Code Online (Sandbox Code Playgroud) 钳位功能是 clamp(x, min, max) = min if x < min, max if x > max, else x
我需要一个像钳位函数一样的函数,但它是平滑的(即具有连续导数).
utility-method ×10
java ×4
c# ×2
oop ×2
boolean ×1
class ×1
code-reuse ×1
flutter ×1
interface ×1
java-8 ×1
javascript ×1
numpy ×1
pandas ×1
python ×1
reactjs ×1
redux ×1
smoothstep ×1
string ×1
url ×1
utility ×1