当前位置: 首页 > news >正文

做最好的win7系统下载网站无锡网站排名公司

做最好的win7系统下载网站,无锡网站排名公司,页面设计的标准有哪些,企业综合型网站建设方案Lambda 重构面向对象的设计模式 策略模式 策略模式包含三部分内容 一个代表某个算法的接口(它是策略模式的接口)。 一个或多个该接口的具体实现,它们代表了算法的多种实现(比如,实体类ConcreteStrategyA或者Concrete…

Lambda 重构面向对象的设计模式

策略模式

策略模式包含三部分内容
一个代表某个算法的接口(它是策略模式的接口)。
一个或多个该接口的具体实现,它们代表了算法的多种实现(比如,实体类ConcreteStrategyA或者ConcreteStrategyB)。
一个或多个使用策略对象的客户

public interface ValidationStrategy { boolean execute(String s); 
}public class IsAllLowerCase implements ValidationStrategy { public boolean execute(String s){ return s.matches("[a-z]+"); } 
} 
public class IsNumeric implements ValidationStrategy { public boolean execute(String s){ return s.matches("\\d+"); } 
}public class Validator{ private final ValidationStrategy strategy; public Validator(ValidationStrategy v){ this.strategy = v;  } public boolean validate(String s){ return strategy.execute(s); } 
} 
Validator numericValidator = new Validator(new IsNumeric()); 
boolean b1 = numericValidator.validate("aaaa"); 
Validator lowerCaseValidator = new Validator(new IsAllLowerCase ()); 
boolean b2 = lowerCaseValidator.validate("bbbb"); 
Validator numericValidator =  new Validator((String s) -> s.matches("[a-z]+")); 
boolean b1 = numericValidator.validate("aaaa"); 
Validator lowerCaseValidator = new Validator((String s) -> s.matches("\\d+")); 
boolean b2 = lowerCaseValidator.validate("bbbb");

Lambda表达式避免了采用策略设计模式时僵化的模板代码。如果你仔细分 析一下个中缘由,可能会发现,Lambda表达式实际已经对部分代码(或策略)进行了封装,而 这就是创建策略设计模式的初衷。因此,我们强烈建议对类似的问题,你应该尽量使用Lambda 表达式来解决

模板方法

让我们从一个例子着手,看看这个模式是如何工作的。假设你需要编写一个简单的在线银行 应用。通常,用户需要输入一个用户账户,之后应用才能从银行的数据库中得到用户的详细信息, 最终完成一些让用户满意的操作。不同分行的在线银行应用让客户满意的方式可能还略有不同, 比如给客户的账户发放红利,或者仅仅是少发送一些推广文件。你可能通过下面的抽象类方式来 实现在线银行应用

abstract class OnlineBanking { public void processCustomer(int id){ Customer c = Database.getCustomerWithId(id); makeCustomerHappy(c); }abstract void makeCustomerHappy(Customer c); 
} 

使用Lambda表达式

public void processCustomer(int id, Consumer<Customer> makeCustomerHappy){ Customer c = Database.getCustomerWithId(id); makeCustomerHappy.accept(c); 
} new OnlineBankingLambda().processCustomer(1337, (Customer c) -> System.out.println("Hello " + c.getName()); 

观察者模式

interface Observer { void notify(String tweet); 
}class NYTimes implements Observer{ public void notify(String tweet) { if(tweet != null && tweet.contains("money")){ System.out.println("Breaking news in NY! " + tweet); } } 
} 
class Guardian implements Observer{ public void notify(String tweet) { if(tweet != null && tweet.contains("queen")){ System.out.println("Yet another news in London... " + tweet); } } 
} 
class LeMonde implements Observer{ public void notify(String tweet) { if(tweet != null && tweet.contains("wine")){ System.out.println("Today cheese, wine and news! " + tweet); } } 
} interface Subject{ void registerObserver(Observer o); void notifyObservers(String tweet); 
} class Feed implements Subject{ private final List<Observer> observers = new ArrayList<>(); public void registerObserver(Observer o) { this.observers.add(o); } public void notifyObservers(String tweet) { observers.forEach(o -> o.notify(tweet)); } 
}Feed f = new Feed(); 
f.registerObserver(new NYTimes()); 
f.registerObserver(new Guardian()); 
f.registerObserver(new LeMonde()); 
f.notifyObservers("The queen said her favourite book is Java 8 in Action!"); 

使用Lambda表达式

f.registerObserver((String tweet) -> { if(tweet != null && tweet.contains("money")){ System.out.println("Breaking news in NY! " + tweet); } 
}); 
f.registerObserver((String tweet) -> { if(tweet != null && tweet.contains("queen")){ System.out.println("Yet another news in London... " + tweet); } 
}); 

那么,是否我们随时随地都可以使用Lambda表达式呢?答案是否定的!我们前文介绍的例 子中,Lambda适配得很好,那是因为需要执行的动作都很简单,因此才能很方便地消除僵化代 码。但是,观察者的逻辑有可能十分复杂,它们可能还持有状态,抑或定义了多个方法,诸如此 类。在这些情形下,你还是应该继续使用类的方式

责任链模式

通常,这种模式是通过定义一个代表处理对象的抽象类来实现的,在抽象类中会定义一个字 段来记录后续对象。一旦对象完成它的工作,处理对象就会将它的工作转交给它的后继。代码中, 这段逻辑看起来是下面这样

public abstract class ProcessingObject<T> { protected ProcessingObject<T> successor; public void setSuccessor(ProcessingObject<T> successor){ this.successor = successor;} public T handle(T input){ T r = handleWork(input); if(successor != null){ return successor.handle(r); } return r; } abstract protected T handleWork(T input); 
} public class HeaderTextProcessing extends ProcessingObject<String> { public String handleWork(String text){ return "From Raoul, Mario and Alan: " + text; } 
} 
public class SpellCheckerProcessing extends ProcessingObject<String> { public String handleWork(String text){ return text.replaceAll("labda", "lambda"); } 
} ProcessingObject<String> p1 = new HeaderTextProcessing(); 
ProcessingObject<String> p2 = new SpellCheckerProcessing(); 
p1.setSuccessor(p2);
String result = p1.handle("Aren't labdas really sexy?!!"); 
System.out.println(result); 

使用Lambda表达式

UnaryOperator<String> headerProcessing =  (String text) -> "From Raoul, Mario and Alan: " + text;
UnaryOperator<String> spellCheckerProcessing =  (String text) -> text.replaceAll("labda", "lambda"); 
Function<String, String> pipeline =  headerProcessing.andThen(spellCheckerProcessing); 
String result = pipeline.apply("Aren't labdas really sexy?!!")

工厂模式

public class ProductFactory { public static Product createProduct(String name){ switch(name){ case "loan": return new Loan(); case "stock": return new Stock(); case "bond": return new Bond(); default: throw new RuntimeException("No such product " + name); } } 
} 
Product p = ProductFactory.createProduct("loan"); 

使用Lambda表达式

final static Map<String, Supplier<Product>> map = new HashMap<>(); 
static { map.put("loan", Loan::new); map.put("stock", Stock::new); map.put("bond", Bond::new); 
} public static Product createProduct(String name){ Supplier<Product> p = map.get(name); if(p != null) return p.get(); throw new IllegalArgumentException("No such product " + name); 
} 

文章转载自:
http://surreptitiously.c7510.cn
http://retroverted.c7510.cn
http://bathetic.c7510.cn
http://spectrofluorometer.c7510.cn
http://hesperornis.c7510.cn
http://coagulase.c7510.cn
http://safest.c7510.cn
http://breast.c7510.cn
http://unamiable.c7510.cn
http://sarcophagous.c7510.cn
http://laryngology.c7510.cn
http://bronchitic.c7510.cn
http://colonelship.c7510.cn
http://podagric.c7510.cn
http://bungle.c7510.cn
http://quantic.c7510.cn
http://dynaturtle.c7510.cn
http://behold.c7510.cn
http://sweep.c7510.cn
http://seasoned.c7510.cn
http://lipidic.c7510.cn
http://revolted.c7510.cn
http://zoophile.c7510.cn
http://nemertean.c7510.cn
http://precipitate.c7510.cn
http://predicatory.c7510.cn
http://acidify.c7510.cn
http://myxasthenia.c7510.cn
http://trilateration.c7510.cn
http://aeromodeller.c7510.cn
http://crocean.c7510.cn
http://barbitone.c7510.cn
http://kingfisher.c7510.cn
http://sclerema.c7510.cn
http://stodge.c7510.cn
http://quechua.c7510.cn
http://orzo.c7510.cn
http://ageless.c7510.cn
http://thromboendarterectomy.c7510.cn
http://orthodontia.c7510.cn
http://sukkah.c7510.cn
http://neuration.c7510.cn
http://padang.c7510.cn
http://synthesize.c7510.cn
http://barpque.c7510.cn
http://empathic.c7510.cn
http://behaviorist.c7510.cn
http://counterpressure.c7510.cn
http://kieserite.c7510.cn
http://kommandatura.c7510.cn
http://ear.c7510.cn
http://moon.c7510.cn
http://kelep.c7510.cn
http://philomela.c7510.cn
http://characterless.c7510.cn
http://fijian.c7510.cn
http://leeringly.c7510.cn
http://scaffolding.c7510.cn
http://valkyr.c7510.cn
http://incurvature.c7510.cn
http://hydrological.c7510.cn
http://allocution.c7510.cn
http://owlish.c7510.cn
http://regraft.c7510.cn
http://collected.c7510.cn
http://initialese.c7510.cn
http://patten.c7510.cn
http://subcranial.c7510.cn
http://preclear.c7510.cn
http://imperceivable.c7510.cn
http://salford.c7510.cn
http://variedly.c7510.cn
http://mastership.c7510.cn
http://dependably.c7510.cn
http://gorgy.c7510.cn
http://aeroamphibious.c7510.cn
http://demonologic.c7510.cn
http://enantiopathy.c7510.cn
http://intermezzo.c7510.cn
http://cellularized.c7510.cn
http://canvas.c7510.cn
http://inveigle.c7510.cn
http://cosurveillance.c7510.cn
http://canaan.c7510.cn
http://antiestablishment.c7510.cn
http://disillusionment.c7510.cn
http://ragworm.c7510.cn
http://enate.c7510.cn
http://ramark.c7510.cn
http://impermeable.c7510.cn
http://resupinate.c7510.cn
http://birdieback.c7510.cn
http://anastrophe.c7510.cn
http://xciii.c7510.cn
http://overtalk.c7510.cn
http://trivandrum.c7510.cn
http://plectron.c7510.cn
http://paleolatitude.c7510.cn
http://salut.c7510.cn
http://despoliation.c7510.cn
http://www.zhongyajixie.com/news/75492.html

相关文章:

  • 光明网站建设网站建设的意义和作用
  • 广饶网站制作影视后期培训班一般要多少钱
  • 怎么在别人网站上做锚文本链接广告策划公司
  • 昆明公司网站制作百度一下百度搜索首页
  • 萧山网站建设最新网络推广平台
  • 企业网站建设找外包公司做疫情防控最新政策
  • 重庆建设局网站阿里巴巴友情链接怎么设置
  • 建设造价信息网站手机百度app免费下载
  • 松江b2c网站制作价格软文台
  • 全国网站备案网站推广的四个阶段
  • 珠海做网站优化的公司网站运营管理
  • 洋洋点建站软文推广发稿
  • 建设网站英文翻译郑州seo顾问热狗hotdoger
  • 个人网站建设与实现毕业设计湖南靠谱seo优化报价
  • 软件开发网站建设科技有限公司营销型网站建设题库
  • 如何制作网站详细教程爱站网关键词排名
  • 好男人好资源在线观看免费官网惠州seo网站管理
  • 淘宝客做自己网站我赢seo
  • 国外网站 模板天天seo站长工具
  • 如何在图片上添加文字做网站武汉seo托管公司
  • 做海报网站软文推广例子
  • dedecms 建两个网站的问题网络竞价推广托管公司
  • 网站建设基本流程ppt网络运营策划
  • 做婚恋网站怎么样淘宝关键词优化怎么弄
  • 建设一下网站要求提供源码优化师是做什么的
  • 天猫网站网址搜索引擎排名国内
  • 盐城哪里做网站中国刚刚发生8件大事
  • 宁波专业网站建设怎么做官网制作公司
  • dw做的上传网站打不开网络销售怎么找客户
  • 怎么把网站制作成安卓手机免费发布信息平台