设计模式学习
程序员文章站
2022-06-24 18:47:07
策略模式使用场景一种算法不同的解决方案java 8之前实现方式定义策略接口定义多个类实现策略接口java8 使用Lambda表达式实现策略模式定义行为类,类中有策略的引用。使用Lambda表达式不需要实现接口,只需要把策略的具体实现使用Lambda表达式传进执行的类中即可public interface Strategy { boolean execute(String s);}public class Action{ private Strategy str...
策略模式
使用场景
一种算法不同的解决方案
非Lambda 实现
- 定义策略接口
- 定义多个类实现策略接口
java8 使用Lambda表达式实现策略模式
- 定义行为类,类中有策略的引用。
- 使用Lambda表达式不需要实现接口,只需要把策略的具体实现使用Lambda表达式传进执行的类中即可
public interface Strategy {
boolean execute(String s);
}
public class Action{
private Strategy strategy;
public Action(Strategy strategy) {
this.strategy = strategy;
}
public boolean execute(String s) {
return strategy.execute(s);
}
}
public class Main {
public static void main(String[] args) {
Action valiator = new Action(s -> s.matches("[a-z]+"));
boolean match = valiator.execute("aaa");
System.out.println(match);
Action valiator1 = new Action (s -> s.matches("[0-9]+"));
boolean match1 = valiator1.execute("111");
System.out.println(match1);
}
}
模板方法
使用场景
某个算法的部分子流程希望可以*的修改
非Lambda 实现
public abstract class Template {
public void compute() {
method1();
method2();
}
private void method1() {
System.out.println("do method1");
}
protected abstract void method2();
}
public class TemplateImpl1 extends Template {
@Override
public void method2() {
System.out.println("TemplateImpl1 do method2");
}
}
public class TemplateImpl2 extends Template {
@Override
protected void method2() {
System.out.println("TemplateImpl2 do method2");
}
}
public class Main {
public static void main(String[] args) {
Template template1 = new TemplateImpl1();
template1.compute();
Template template2 = new TemplateImpl2();
template2.compute();
}
}
do method1
TemplateImpl1 do method2
do method1
TemplateImpl2 do method2
使用Lambda表达式实现模板方法
public class Template {
public void compute(Consumer<Object> method2, Object o) {
method1();
method2.accept(o);
}
private void method1() {
System.out.println("do method1");
}
}
public class Main {
public static void main(String[] args) {
Template template1 = new Template();
template1.compute(o -> {
System.out.println(o);
}, "templateimpl1 do method2");
Template template2 = new Template();
template1.compute(o -> {
System.out.println(o);
}, "templateimpl2 do method2");
}
}
do method1
templateimpl1 do method2
do method1
templateimpl2 do method2
观察者模式
使用场景
某些事件发生时(比如状态转变),如果一个对象(通 常我们称之为主题)需要自动地通知其他多个对象(称为观察者),就会采用该方案
非Lambda 实现
public interface Observer {
void notify(String tweet);
}
public interface Subject {
void registerObserver(Observer o);
void notifyObservers(String tweet);
}
public class Observer1 implements Observer {
@Override
public void notify(String tweet) {
System.out.println(tweet + "1");
}
}
public class Observer2 implements Observer {
@Override
public void notify(String tweet) {
System.out.println(tweet + "2");
}
}
public class SubjectImpl implements Subject {
private List<Observer> observerList = new ArrayList<>();
@Override
public void registerObserver(Observer o) {
observerList.add(o);
}
@Override
public void notifyObservers(String tweet) {
observerList.stream().forEach(e -> {
e.notify(tweet);
});
}
}
public class Main {
public static void main(String[] args) {
Subject f = new SubjectImpl();
f.registerObserver(new Observer1());
f.registerObserver(new Observer2());
f.notifyObservers("notify observer");
}
}
notify observer1
notify observer2
java8 使用Lambda表达式实现观察者模式
public class Observer1 implements Observer {
private Consumer<Object> consumer;
public Observer1(Consumer<Object> consumer) {
this.consumer = consumer;
}
@Override
public void notify(Object tweet) {
consumer.accept(tweet);
}
}
public class Observer2 implements Observer {
private Consumer<Object> consumer;
public Observer2(Consumer<Object> consumer) {
this.consumer = consumer;
}
@Override
public void notify(Object tweet) {
consumer.accept(tweet);
}
}
public static void main(String[] args) {
Subject f = new SubjectImpl();
f.registerObserver(new Observer1(e -> {
System.out.println(e + "1");
}));
f.registerObserver(new Observer2(e -> {
System.out.println(e + "2");
}));
f.notifyObservers("notify observer");
}
}
责任链模式
使用场景
责任链模式是一种创建处理对象序列(比如操作序列)的通用方案。一个处理对象可能需要 在完成一些工作之后,将结果传递给另一个对象,这个对象接着做一些工作,再转交给下一个处 理对象,以此类推。
非Lambda 实现
public abstract class ProcessingObject<T> {
private ProcessingObject<T> successor;
public void setSuccessor(ProcessingObject<T> successor) {
this.successor = successor;
}
public T handle(T t) {
T t1 = handleWork(t);
if (successor != null) {
return successor.handle(t1);
}
return t1;
}
protected abstract T handleWork(T t);
}
public class ProcessingChain1 extends ProcessingObject<String> {
@Override
protected String handleWork(String text) {
return "hello, " + text;
}
}
public class ProcessingChain2 extends ProcessingObject<String> {
@Override
protected String handleWork(String text) {
return text.replace("chai","chain");
}
}
public class Main {
public static void main(String[] args) {
ProcessingObject<String> processingObject1 = new ProcessingChain1();
ProcessingObject<String> processingObject2 = new ProcessingChain2();
processingObject1.setSuccessor(processingObject2);
String res = processingObject1.handle("I am chai of responsibility pattern !");
System.out.println(res);
}
}
hello, I am chain of responsibility pattern !
java8 Lambda实现责任链
public class Main {
public static void main(String[] args) {
UnaryOperator<String> processingObject1 = (String text) ->text ;
UnaryOperator<String> processingObject2 = (String text) -> text.replace("chai", "chain");
Function<String, String> pipeline = processingObject1.andThen(processingObject2);
String res = pipeline.apply("Hello,I am chai of reponsibility pattern !");
System.out.println(res);
}
Hello,I am chain of reponsibility pattern !
}
工厂模式
使用场景
使用工厂模式,不同条件下创建不同实例时,你无需向客户暴露实例化的逻辑就能完成对象的创建。
java8 之前实现方法
public interface Product {
void product();
}
public class Product1 implements Product {
@Override
public void product() {
System.out.println("I am product1");
}
}
public class Product2 implements Product {
@Override
public void product() {
System.out.println("I am product2");
}
}
public class ProductFactory {
public static Product createProduct(String name) {
switch (name) {
case "product1":
return new Product1();
case "product2":
return new Product2();
default:
throw new RuntimeException("no such product");
}
}
}
public class Main {
public static void main(String[] args) {
Product product1 = ProductFactory.createProduct("product1");
product1.product();
Product product2 = ProductFactory.createProduct("product2");
product2.product();
}
}
I am product1
I am product2
Java8 新特性实现
public class ProductFactory {
private static Map<String, Supplier<Product>> factory = null;
static {
factory.put("product1", Product1::new);
factory.put("product2", Product2::new);
}
public static Product createProduct(String name) {
Supplier<Product> productSupplier = factory.get(name);
if (productSupplier != null) {
return productSupplier.get();
}
throw new RuntimeException("no such product ");
}
}
本文地址:https://blog.csdn.net/weixin_39175589/article/details/107584333