1、观察者模式
//被观察者
public interface observer{
public void update(String s);
}
//观察著
public interface observable{
public void registerObserver(observer o);
public void notifyUpdate();
}
//微信公众号
public static class Wechat implements observable{
public observer observer;
@Override
public void registerObserver(observer o) {
this.observer = o;
}
@Override
public void notifyUpdate() {
observer.update("有新的内容推送啦!");
}
}
//用户
public static class User implements observer{
@Override
public void update(String s) {
System.out.println(s);
}
}
//测试代码
Wechat wechat = new Wechat();
User user = new User();
wechat.registerObserver(user);
wechat.notifyUpdate();
https://www.cnblogs.com/luohanguo/p/7825656.html
2、适配器模式、
//目标接口
public interface Target {
//这是源类Adapteee没有的方法
public void Request();
}
//源接口
public class Adaptee {
public void SpecificRequest(){
}
}
//适配器
class Adapter implements Target{
// 直接关联被适配类
private Adaptee adaptee;
// 可以通过构造函数传入具体需要适配的被适配类对象
public Adapter (Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void Request() {
// 这里是使用委托的方式完成特殊功能
this.adaptee.SpecificRequest();
}
}
//测试类
public class AdapterPattern {
public static void main(String[] args){
//需要先创建一个被适配类的对象作为参数
Target mAdapter = new Adapter(new Adaptee());
mAdapter.Request();
}
}
https://www.jianshu.com/p/9d0575311214
3、代理模式
interface Subject{
void request();
}
class RealSubject implements Subject{
public void request(){
System.out.println("request");
}
}
class Proxy implements Subject{
private Subject subject;
public Proxy(){
//编译时确定
this.subject = new RealSubject() ;
}
public void request(){
//访问控制
if(){
subject.request();
}else{
....
}
}
}
//测试类
public class ProxyDemo {
public static void main(String args[]){
Proxy p = new Proxy();
p.request();
}
}
4、装饰者模式
//抽象构件角色
public interface Component {
public void sampleOperation();
}
//具体构件角色
public class ConcreteComponent implements Component {
@Override
public void sampleOperation() {
// 写相关的业务代码
}
}
//装饰角色
public class Decorator implements Component{
private Component component;
public Decorator(Component component){
this.component = component;
}
@Override
public void sampleOperation() {
// 委派给构件
component.sampleOperation();
}
}
//具体装饰角色
public class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void sampleOperation() {
// 写相关的业务代码
super.sampleOperation();
// 写相关的业务代码
}
}
https://www.jianshu.com/p/d7f20ae63186
5、适配器模式、代理模式、装饰者模式的区别
- 代理模式和适配器模式最大的区别,代理模式是与原对象实现同一个接口,而适配器类则是匹配新接口,说白了,实现一个新的接口。
- 代理模式与装饰者模式的区别:代理模式主要是控制对某个特定对象访问,而装饰模式主要是为了给对象添加行为。
https://blog.csdn.net/chenchaofuck1/article/details/51030524
https://blog.csdn.net/lulei9876/article/details/39994825
6、建造者模式(builder)
- 用于构建复杂对象
public class MyDialog {
private String title;
private String content;
private String icon;
static class MyBuilder {
MyDialog dialog;
public MyBuilder() {
dialog = new MyDialog();
}
public MyBuilder setTile(String title) {
dialog.title = title;
return this;
}
public MyBuilder setContent(String content) {
dialog.content = content;
return this;
}
public MyBuilder setIcon(String icon) {
dialog.icon = icon;
return this;
}
public MyDialog create() {
return dialog;
}
}
@Override
public String toString() {
return "Dialog [title=" + title + ", content=" + content + ", icon=" + icon + "]";
}
}
MyDialog.MyBuilder builder = new MyDialog.MyBuilder();
MyDialog dialog = builder.setContent("内容").setTile("标题").setIcon("图标").create();
System.out.println(dialog.toString());
https://www.jianshu.com/p/4dcc723b676e
https://blog.csdn.net/carson_ho/article/details/54910597
7、责任链模式
//抽象处理者
public abstract class Handler{
//下一节点的处理者
protected Handler nextHandler;
//满足条件则处理,不满足条件交给下一节点进行处理
public abstract void handRequest(int i) ;
}
public class Handler1 extends Handler{
@Override
public void handRequest(int i) {
if(i < 1) {
System.out.println(i);
}else {
nextHandler.handRequest(i);
}
}
}
public class Handler2 extends Handler{
@Override
public void handRequest(int i) {
if(i < 2) {
System.out.println(i);
}else {
nextHandler.handRequest(i);
}
}
}
//测试代码
Handler1 handler1 = new Handler1();
Handler1 handler2 = new Handler2();
handler1.nextHandler = handler2;
handler1.handRequest(3);
网友评论