zoukankan      html  css  js  c++  java
  • 使用Lambda 重构面向对象的设计模式

      新的语言特性常常让现存的编程模式或设计黯然失色。比如, Java 5中引入了for-each循环,由于它的稳健性和简洁性,已经替代了很多显式使用迭代器的情形。Java 7中推出的菱形操作符(<>)让大家在创建实例时无需显式使用泛型,一定程度上推动了Java程序员们采用类型接口(type interface)进行程序设计。
      对设计经验的归纳总结被称为设计模式。设计软件时,如果你愿意,可以复用这些方式方法来解决一些常见问题。这看起来像传统建筑工程师的工作方式,对典型的场景(比如悬挂桥、拱桥等)都定义有可重用的解决方案。例如,访问者模式常用于分离程序的算法和它的操作对象。单例模式一般用于限制类的实例化,仅生成一份对象。
      Lambda表达式为程序员的工具箱又新添了一件利器。它们为解决传统设计模式所面对的问题提供了新的解决方案,不但如此,采用这些方案往往更高效、更简单。使用Lambda表达式后,很多现存的略显臃肿的面向对象设计模式能够用更精简的方式实现了。这一节中,我们会针对五个设计模式展开讨论,它们分别是:

     策略模式

    策略模式代表了解决一类算法的通用解决方案,你可以在运行时选择使用哪种方案。这一模式应用到更广泛的领域,比如使用不同的标准来验证输入的有效性,使用不同的方式来分析或者格式化输入。

    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");

      lambda

    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");
    

      

     模板方法

      如果你需要采用某个算法的框架,同时又希望有一定的灵活度,能对它的某些部分进行改进,那么采用模板方法设计模式是比较通用的方案。好吧,这样讲听起来有些抽象。换句话说,模板方法模式在你“希望使用这个算法,但是需要对其中的某些行进行改进,才能达到希望的效果”时是非常有用的。
      让我们从一个例子着手,看看这个模式是如何工作的。假设你需要编写一个简单的在线银行应用。通常,用户需要输入一个用户账户,之后应用才能从银行的数据库中得到用户的详细信息,最终完成一些让用户满意的操作。不同分行的在线银行应用让客户满意的方式可能还略有不同,比如给客户的账户发放红利,或者仅仅是少发送一些推广文件。你可能通过下面的抽象类方式来实现在线银行应用:

    abstract class OnlineBanking {
        public void processCustomer(int id) {
            Customer c = Database.getCustomerWithId(id);
            makeCustomerHappy(c);
        }
    
        abstract void makeCustomerHappy(Customer c);
    }
    
        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());


     观察者模式

      观察者模式是一种比较常见的方案,某些事件发生时(比如状态转变),如果一个对象(通常我们称之为主题)需要自动地通知其他多个对象(称为观察者),就会采用该方案。创建图形用户界面(GUI)程序时,你经常会使用该设计模式。这种情况下,你会在图形用户界面组件(比如按钮)上注册一系列的观察者。如果点击按钮,观察者就会收到通知,并随即执行某个特定的行为。 但是观察者模式并不局限于图形用户界面。比如,观察者设计模式也适用于股票交易的情形,多个券商可能都希望对某一支股票价格(主题)的变动做出响应

    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));
        }
    }
    
        @Test
        public void test() {
    
            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);
                }
            });
        }

     责任链模式

        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");
            }
        }
    
        @Test
        public void test12() {
            ProcessingObject<String> p1 = new HeaderTextProcessing();
            ProcessingObject<String> p2 = new SpellCheckerProcessing();
            p1.setSuccessor(p2);
            String result1 = p1.handle("Aren't labdas really sexy?!!");
            System.out.println(result1);
            
            //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 {
        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 createProductlambda(String name){
            Supplier<Product> p = map.get(name);
            if(p != null) {
                return p.get();
            }
            throw new IllegalArgumentException("No such product " + name);
        }
        
        
        
        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);
            }
        }
    }


    public class Product {
    }

    public class Bond extends Product {
    }
    public class Loan extends Product {
    }
    public class Stock extends Product {
    }

    @Test
    public void test22(){
    Product p = ProductFactory.createProduct("loan");

    Supplier<Product> loanSupplier = Loan::new;
    Product product = loanSupplier.get();
    Optional<Product> p1 = Optional.of(p);
    Optional<Product> p2 = Optional.ofNullable(p);

    }

    摘自参考:java 8 in action

  • 相关阅读:
    记录docker 安装sonarqube和安装的一些坑
    Docker安装部署ELK教程 (Elasticsearch+Kibana+Logstash+Filebeat)
    使用docker创建rabbitmq服务
    Centos7下安装Docker
    使用 docker 搭建 MySQL 主从同步/读写分离
    sql优化建议
    记使用docker配置kafka
    6种@Transactional注解的失效场景
    linux定时任务之清理tomcat catalina.out日志
    持久层框架JPA与Mybatis该如何选型
  • 原文地址:https://www.cnblogs.com/chenfx/p/15242039.html
Copyright © 2011-2022 走看看