lambda重构设计模式

这里选取5种设计模式如何lambda简化


【1】责任链模式

import java.util.function.Function;
import java.util.function.UnaryOperator;

public class wordtest {

	public static void main(String[] args) {
		//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);
		System.out.println(pipeline.apply("Aren't labdas really sexy?!!"));

		//责任链模式
		ProcessingObject<String> p1 = new HeaderTextProcessing();
		ProcessingObject<String> p2 = new SpellCheckerProcessing();
		p2.setSuccessor(p1);
		String result = p2.handle("Aren't labdas really sexy?!!");
		System.out.println(result);

	}

}

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

class HeaderTextProcessing extends ProcessingObject<String> {
	public String handleWork(String text) {
		return "From Raoul, Mario and Alan: " + text;
	}
}

class SpellCheckerProcessing extends ProcessingObject<String> {
	public String handleWork(String text) {
		return text.replaceAll("labda", "lambda");
	}
}


【2】工厂模式

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public class FactoryTest {
	
	final static private 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);
	}

	public static void main(String[] args) {
		Product p1 = ProductFactory.createProduct("loan");//传统工厂模式
		Product p2 = createProduct("loan");//lambda工厂模式,不过要考虑扩展性
	}

}

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

interface Product {
}

class Loan implements Product {
	Loan() {
		System.out.println("----Loan----");
	};
}

class Stock implements Product {
	Stock() {
		System.out.println("----Stock----");
	};
}

class Bond implements Product {
	Bond() {
		System.out.println("----Bond----");
	};
}


【观察者模式】

import java.util.ArrayList;
import java.util.List;

public class ObserverTest {

	public static void main(String[] args) {
		//传统
		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
		Feed f2 = new Feed();
		f2.registerObserver((String tweet) -> {
			if (tweet != null && tweet.contains("money")) {
				System.out.println("Breaking news in NY! " + tweet);
			}
		});
		f2.registerObserver((String tweet) -> {
			if (tweet != null && tweet.contains("queen")) {
				System.out.println("Yet another news in London... " + tweet);
			}
		});
		f2.registerObserver(
				(String tweet) -> {
					if (tweet != null && tweet.contains("wine")) {
						System.out.println("Today cheese, wine and news! " + tweet);
				}
		});
		f2.notifyObservers("The wine said her favourite book is Java 8 in Action!");
	}

}

interface Subject {
	void registerObserver(Observer o);

	void notifyObservers(String tweet);
}

interface Observer {
	void notify(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));
	}
}

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








猜你喜欢

转载自blog.csdn.net/luozhonghua2014/article/details/78933260