问题描述
我有很多方法,每个方法检查一组相同的条件,如果不满足任何条件,则返回null值,否则返回不同类的对象。 有没有一种方法不必为每个函数编写所有这些术语并使用更少的代码?
public A methode1()
{
if ///something
return A("xxx")
else if ///something
return A("yyy")
else if ///something
return A("zzzz")
else
return Error() // or return null
}
public B methode2()
{
if ///something
return B("mmmm")
else if ///something
return B("nnn")
else if ///something
return B("bbbb")
else
return Error() // or return null
}
public C methode3()
{
if ///something
return C("oooo")
else if ///something
return C("ggg")
else if ///something
return C("llll")
else
return Error() // or return null
}
解决方法
T GetIf<T>(Func<T> case1,Func<T> case2,Func<T> case3,Func<T> error)
{
if (condition1)
return case1();
else if (condition2)
return case2();
else if (condition3)
return case3();
else
return error();
}
public A methode1() => GetIf<A>(() => A("xxx"),() => A("yyy"),() => A("zzzz"),Error);
public B methode2() => GetIf<B>(() => B("mmmm"),() => B("nnn"),() => B("bbbb"),Error);
,
标准方法是,您可以将工厂类与接口/抽象类一起使用
public interface IOutput {
}
public class Output1 : IOutput{
}
public class Output2 : IOutput{
}
public class MyFactory
{
public IOutput Get()// add args list of any
{
if(condition) // you can use args in condition if necessary
return new Output1();
else
return new Output2();
}
}
,
您可以将template method pattern与generics结合使用:
use AppleScript version "2.4"
use framework "Foundation"
on run {input,parameters}
set foundList to my findPattern:"(?<!\\d)\\d{6}(?!\\d)" inString:((item 1 of input) as text)
return foundList
end run
on findPattern:thePattern inString:theString
set theText to current application's NSString's stringWithString:theString
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern ¬
options:0 |error|:(missing value)
set theResult to (theRegEx's matchesInString:theText ¬
options:0 ¬
range:{location:0,|length|:theText's |length|})'s valueForKey:("range")
set outputArray to {}
repeat with thisRange in theResult
copy (theText's substringWithRange:thisRange) as text to end of outputArray
end repeat
return outputArray
end findPattern:inString:
并在您的方法中使用它:
public abstract class AbstractTemplate<T>
{
public T methode()
{
if ///something
return Do1();
else if ///something
return Do2();
else if ///something
return Do3();
else
return Error() // or return null
}
protected abstract T Do1();
protected abstract T Do2();
protected abstract T Do3();
}
public class ConcreteATemplate : AbstractTemplate<A>
{
protected override T Do1() => A("xxx");
protected override T Do2() => A("yyy");
protected override T Do3() => A("zzzz");
}
,
您可以使用带有通用类型参数和一组初始值的方法。
private T GetIfOkay<T>(string a,string b,string c)
where T : new()
{
if (something)
return new T(a);
else if (something else)
return new T(b);
else if (yet something else)
return new T(c);
else
return null;
}
public A methode1()
{
return GetIfOkay<A>("xxx","yyy","zzzz");
}
public B methode2()
{
return GetIfOkay<B>("mmmm","nnn","bbbb");
}
// etc.
如果您需要更动态的行为,请使用@ donggas90的解决方案。
请参阅:
,您可以使用通用方法和存储库模式。这是C#的通用方法示例。我想这可能会给您一些想法。
add_parser
,
根据您的评论,我试图重写您的示例,希望对您有所帮助。自由地询问是否需要更清楚的说明。
class Program
{
static void Main(string[] args)
{
var objFactory = new MyFactory();
// Get and cast return object
A a1= (A) objFactory.ConsolidatedMethod("Condition 1 for Objct A","xxx");
// Or Directly assian to base object
IOutput a2 = objFactory.ConsolidatedMethod("Condition 2 for Objct A","yyyy");
// use anonymous object
var b1= objFactory.ConsolidatedMethod("Condition 1 for Objct B","mmmm");
var nullcheck1 = objFactory.ConsolidatedMethod("null conditionj","i'm null");
}
}
interface IOutput
{
}
class A : IOutput
{
public A(string objParam)
{
}
}
class B : IOutput
{
public B(string objParam)
{
}
}
class NullOutput : IOutput
{
public NullOutput(string objParam)
{
}
}
class MyFactory
{
/// <summary>
/// Demo
/// </summary>
/// <param name="arg">you can use this based on your requirement </param>
/// <param name="objparam">you can use this based on your requirement </param>
/// <returns>IOutput</returns>
public IOutput ConsolidatedMethod(string arg,string objparam)
{
IOutput _output=default;
if (arg == "Condition 1 for Objct A")
_output = new A(objparam);
else if (arg == "Condition 2 for Objct A")
_output = new A(objparam);
else if (arg == "Condition 1 for Objct b")
_output = new B(objparam);
else if (arg == "Condition 2 for Objct B")
_output = new B(objparam);
else
_output = new NullOutput(objparam);
return _output;
}
}