这是什么设计模式?打印PersonsOlderThan
Which design pattern is this? printPersonsOlderThan
我阅读了很多关于设计模式的内容,但我仍然难以确定何时必须使用它们。今天我正在阅读有关 lambda 的 oracle 文档,看到 class "evolution" 并说 "Hey, clearly there's some decoupling here"。我认为这里有一个众所周知的模式,但不知道具体是哪个。
另一个我对此也有的问题是,如果我没有使用 SPRING,其中关于接口和实现的文件夹结构非常清晰,这将是 - 根据 goog 实践 - 项目结构在哪里我必须创建接口。
示例以此代码开头:
public static void printPersonsOlderThan(List<Person> roster, int age) {
for (Person p : roster) {
if (p.getAge() >= age) {
p.printPerson();
}
}
}
然后继续:
public static void printPersonsWithinAgeRange(
List<Person> roster, int low, int high) {
for (Person p : roster) {
if (low <= p.getAge() && p.getAge() < high) {
p.printPerson();
}
}
}
并以此结束:
public static void printPersons(
List<Person> roster, CheckPerson tester) {
for (Person p : roster) {
if (tester.test(p)) {
p.printPerson();
}
}
}
创建了这个界面:
interface CheckPerson {
boolean test(Person p);
}
这就是实现:
class CheckPersonEligibleForSelectiveService implements CheckPerson {
public boolean test(Person p) {
return p.gender == Person.Sex.MALE &&
p.getAge() >= 18 &&
p.getAge() <= 25;
}
}
基本上你实现了一个filter like a Java FileFilter, this is close to the visitor pattern:
Could someone in simple terms explain to me the visitor pattern's purpose with examples if possible
我以最高分通过了大学的设计模式科目,但我似乎仍然无法识别任何熟悉的模式。
很可能没有使用预定义的模式,但是 CheckPerson
出于显而易见的原因已被抽象化。
在大学里,我们将 类 分组在包中,接口通常与实现 类.
放在同一个包中
除了Visitor,可以考虑使用Strategy模式。
public abstract class PrintStrategy {
abstract protected List<Person> checkPerson(List<Person> list);
public void printPerson(List<Person> roster){
List<Person> filteredRoster = this.checkPerson(roster);
for (Person person : filteredRoster) {
person.print();
}
}
}
public class PrintOlderThanStartegy extends PrintStrategy {
private final int ageWaterMark;
public PrintOlderThanStartegy(final int ageWaterMark){
this.ageWaterMark = ageWaterMark;
}
protected List<Person> checkPerson(List<Person> roster) {
List<Person> filteredRoster = new ArrayList<Person>();
for (Person person : roster) {
if(person.getAge() > ageWaterMark){
filteredRoster.add(person);
}
}
return filteredRoster;
}
}
public class Test {
public static void main(String[] args) {
List<Person> roster = new ArrayList<Person>();
Person p1 = new Person();
p1.setAge(50);
Person p2 = new Person();
p2.setAge(20);
roster.add(p1);
roster.add(p2);
PrintStrategy printStrategy = new PrintOlderThanStartegy(30);
printStrategy.printPerson(roster);
}
}
我阅读了很多关于设计模式的内容,但我仍然难以确定何时必须使用它们。今天我正在阅读有关 lambda 的 oracle 文档,看到 class "evolution" 并说 "Hey, clearly there's some decoupling here"。我认为这里有一个众所周知的模式,但不知道具体是哪个。
另一个我对此也有的问题是,如果我没有使用 SPRING,其中关于接口和实现的文件夹结构非常清晰,这将是 - 根据 goog 实践 - 项目结构在哪里我必须创建接口。
示例以此代码开头:
public static void printPersonsOlderThan(List<Person> roster, int age) {
for (Person p : roster) {
if (p.getAge() >= age) {
p.printPerson();
}
}
}
然后继续:
public static void printPersonsWithinAgeRange(
List<Person> roster, int low, int high) {
for (Person p : roster) {
if (low <= p.getAge() && p.getAge() < high) {
p.printPerson();
}
}
}
并以此结束:
public static void printPersons(
List<Person> roster, CheckPerson tester) {
for (Person p : roster) {
if (tester.test(p)) {
p.printPerson();
}
}
}
创建了这个界面:
interface CheckPerson {
boolean test(Person p);
}
这就是实现:
class CheckPersonEligibleForSelectiveService implements CheckPerson {
public boolean test(Person p) {
return p.gender == Person.Sex.MALE &&
p.getAge() >= 18 &&
p.getAge() <= 25;
}
}
基本上你实现了一个filter like a Java FileFilter, this is close to the visitor pattern:
Could someone in simple terms explain to me the visitor pattern's purpose with examples if possible
我以最高分通过了大学的设计模式科目,但我似乎仍然无法识别任何熟悉的模式。
很可能没有使用预定义的模式,但是 CheckPerson
出于显而易见的原因已被抽象化。
在大学里,我们将 类 分组在包中,接口通常与实现 类.
放在同一个包中除了Visitor,可以考虑使用Strategy模式。
public abstract class PrintStrategy {
abstract protected List<Person> checkPerson(List<Person> list);
public void printPerson(List<Person> roster){
List<Person> filteredRoster = this.checkPerson(roster);
for (Person person : filteredRoster) {
person.print();
}
}
}
public class PrintOlderThanStartegy extends PrintStrategy {
private final int ageWaterMark;
public PrintOlderThanStartegy(final int ageWaterMark){
this.ageWaterMark = ageWaterMark;
}
protected List<Person> checkPerson(List<Person> roster) {
List<Person> filteredRoster = new ArrayList<Person>();
for (Person person : roster) {
if(person.getAge() > ageWaterMark){
filteredRoster.add(person);
}
}
return filteredRoster;
}
}
public class Test {
public static void main(String[] args) {
List<Person> roster = new ArrayList<Person>();
Person p1 = new Person();
p1.setAge(50);
Person p2 = new Person();
p2.setAge(20);
roster.add(p1);
roster.add(p2);
PrintStrategy printStrategy = new PrintOlderThanStartegy(30);
printStrategy.printPerson(roster);
}
}