java设计模式(二)

原文网址:https://blog.csdn.net/doymm2008/article/details/13288067

适配器模式:将某个类的接口转换为客户端期望的另一个接口表示,目的是消除由于接口不匹配而造成的兼容问题,主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式

核心思想就是:有一个Source类,拥有一个方法,待适配,而目标接口是Targetable,通过Adapter类将Source类的功能拓展到Targetable上

类适配器模式 实例:

public class Source {
    public void method1() {
        System.out.println("this is original method!");
    }
}
public interface Targetable {
    public void method1();
    public void method2();
}

public class Adapter extends Source implements Targetable{

    @Override
    public void method2() {
        System.out.println("this is targetable method!!");
    }
    //测试
    public static void main(String[] args) {
        Targetable targetable = new Adapter();
        targetable.method1();
        targetable.method2();
    }
}

运行结果:

this is original method!
this is targetable method!!

这样Targetable接口的实现类就具有了Source类的功能。

对象适配器模式 实例

只需要修改Adapter类

class Adapter1 implements Targetable{
    private Source soucre;
    public Adapter1() {
        this.soucre = new Source();
    }
    @Override
    public void method1() {
        soucre.method1();
    }

    @Override
    public void method2() {
        System.out.println("this is targetable method!!");
    }
}

接口适配器:

public interface SourceInterface {
    public void method1();
    public void method2();
}
abstract class AbstractSource implements SourceInterface {
    @Override
    public void method1() {
    }
    @Override
    public void method2() {
    }
}

public class Wrapper1 extends AbstractSource{
    @Override
    public void method1() {
        System.out.println("this is method1!!");
    }
}

public class Wrapper2 extends AbstractSource{
    @Override
    public void method2() {
        System.out.println("this is method2!!");
    }
    public static void main(String[] args) {
        SourceInterface sourceInterface1 = new Wrapper1();
        SourceInterface sourceInterface2 = new Wrapper2();
        sourceInterface1.method1();
        sourceInterface2.method2();
    }
}

运行结果:

this is method1!!
this is method2!!
 

装饰模式:

装饰模式就是给一个对象增加一个新的功能、而且是动态的,要求被装饰对象和装饰对象实现同一接口、装饰对象持有被装饰对象的实例

应用场景:需要扩展一个类的功能、动态的为一个对象添加功能,还能够动态撤销

实例:

public interface SourceInterface {
    public void method();
}

public class Source implements SourceInterface{
    public void method() {
        System.out.println("this is original method");
    }
}

public class Decorator implements SourceInterface {
    private SourceInterface source;
    public Decorator(SourceInterface source) {
        super();
        this.source = source;
    }
    @Override
    public void method() {
        System.out.println("before decorator");
        source.method();
        System.out.println("after decorator");
    }
    public static void main(String[] args) {
        SourceInterface sourceInterface = new Source();
        SourceInterface sourceInterface2 = new Decorator(sourceInterface);
        sourceInterface2.method();
    }

}

运行结果:

before decorator
this is original method
after decorator
 

代理模式:多出一个代理类,为原对象进行一些操作

应用场景:如果已有的方法在使用的过程中需要改进,如果修改原来的方法,就会违反开闭原则,所以采用代理的方式

实例:

public interface Source {
    public void method();
}

public class SourceImpl implements Source {
    @Override
    public void method() {
        System.out.println("this is original method");
    }
}

public class ProxySource implements Source{
    private Source source;
    public ProxySource() {
        this.source = new SourceImpl();
    }
    public void after() {
        System.out.println("proxy after");
    }
    public void before() {
        System.out.println("proxy before");
    }
    @Override
    public void method() {
        before();
        source.method();
        after();
    }
    public static void main(String[] args) {
        ProxySource proxySource = new ProxySource();
        proxySource.method();
    }
}

外观模式:是为了解决类与类之间的依赖关系,其中不涉及接口,主要是为了统一调配

实例

public class CPU {
    public void start() {
        System.out.println("CPU start");
    }
    public void showdown() {
        System.out.println("CPU showdown");
    }
}

public class Memory {
    public void start() {
        System.out.println("Memory start");
    }
    public void showdown() {
        System.out.println("Memory showdown");
    }
}

public class Computer {
    private CPU cpu;
    private Memory memory;
    public Computer() {
        cpu = new CPU();
        memory = new Memory();
    }
    public void start() {
        System.out.println("computer start ");
        cpu.start();
        memory.start();
        System.out.println("computer start finished");
    }
    public void showdown() {
        System.out.println("computer showdown ");
        cpu.showdown();
        memory.showdown();
        System.out.println("computer showdown finished");
    }
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.start();
        computer.showdown();
    }
}

运行结果:

computer start 
CPU start
Memory start
computer start finished
computer showdown 
CPU showdown
Memory showdown
computer showdown finished
如果我们没有Computer类,那么,CPU、Memory、Disk他们之间将会相互持有实例,产生关系,这样会造成严重的依赖,修改一个类,可能会带来其他类的修改,这不是我们想要看到的,有了Computer类,他们之间的关系被放在了Computer类里,这样就起到了解耦的作用,这,就是外观模式!

 

桥接模式:就是把事物和具体实现分开、使他们自己可以各自独立的变化,可搭配JDBC的数据库驱动进行理解

实例

public interface Source {
    public void method();
}

public class SourceImpl1 implements Source{
    @Override
    public void method() {
        System.out.println("this is SourceImpl1");
    }
}

public class SourceImpl2 implements Source{
    @Override
    public void method() {
        System.out.println("this is SourceImpl2");
    }
}

public abstract class Bridge {
    private Source source;
    public void setSource(Source source) {
        this.source = source;
    }
    public Source getSource() {
        return source;
    }
    public void method() {
        source.method();
    }
}

public class MyBridge extends Bridge{
    @Override
    public void method() {
        getSource().method();
    }
    public static void main(String[] args) {
        Bridge bridge = new MyBridge();
        
        Source source1 = new SourceImpl1();
        bridge.setSource(source1);
        bridge.method();
        
        Source source2 = new SourceImpl2();
        bridge.setSource(source2);
        bridge.method();
    }
}

运行结果:

this is SourceImpl1
this is SourceImpl2

这样,就通过对Bridge类的调用,实现了对接口Source的实现类SourceImpl1和SourceImpl2的调用

组合模式:又叫做整体-部分模式

使用场景:将多个对象组合在一起进行操作

实例:(数据连接池的实现)

package grouppattern;

import java.util.Enumeration;
import java.util.Vector;

public class TreeNode {
    private String name;
    private TreeNode parent;
    private Vector<TreeNode> children = new Vector<>();
    public TreeNode(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public TreeNode getParent() {
        return parent;
    }
    public void setParent(TreeNode parent) {
        this.parent = parent;
    }
    public void add(TreeNode node) {
        children.add(node);
    }
    public void remove(TreeNode node) {
        children.remove(node);
    }
    public Enumeration<TreeNode> getChildren() {
        return children.elements();
    }
}

享元模式:实现对象的共享,即共享池,当系统中对象多的时候可以减少内存的开销,通常与工厂模式以前使用

实例:

public class ConnectionPool {
    private Vector<Connection> pool;
    
    private String url = "jdbc:mysql://localhost:3306/test";
    private String username = "root";
    private String password = "root";
    private String driverClassName = "com.mysql.jdbc.Driver";
    
    private int poolSize = 10;
    private static ConnectionPool instance = null;
    Connection connection = null;
    
    private ConnectionPool() {
        pool = new Vector<>();
        for(int i = 0;i<poolSize;i++) {
            try {
                Class.forName(driverClassName);
                connection = DriverManager.getConnection(url, username, password);
                pool.add(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            
        }
    }
    
    public synchronized void release(Connection connection) {
        pool.add(connection);
    }
    
    public synchronized Connection getConnection() {
        if(pool.size()>0) {
            Connection connection = pool.get(0);
            pool.remove(connection);
            return connection;
        }else {
            return null;
        }
    }
}

下一节:https://blog.csdn.net/q1937915896/article/details/86570106

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值