设计模式之结构模式

news/2025/2/24 17:04:37

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

设计模式之结构模式

一、概述

    1.1 简述

        告别面向过程: 从汇编到C,由于机器的执行都是通过有顺序的,我们的编程都是面向过程,可以极大的提高系统资源的利用率。当越来越多的项目需要快速实现,硬件设备越来越多的不是我们考虑的重点。项目越来越大,开发人员越来越多的时候,我们需要一种更加工程化的思想来分析项目。

        走向工程化:从项目的可扩展,低耦合、开发周期等方面来思考问题,面向对象编程就是基于这一点提出的一种高度抽象的思维方式。将项目的各个模块从分解、求同、抽象,形成以功能模块为基点来完成项目工程。封装、基础、多态的出现可以让我们大大降低项目的耦合性、 从而提高稳定行和可扩展性。

    1.2  结构模式

        结构模式是功能的封装、基础、多态的主要使用实现。我们平时用的最多,也是最容易忽略的一种实现。常用的有以下几种实现。

二、实例

     2.2 适配器模式

//旧功能已经存在,需要在原来的功能上添加新功能
//基类
public class Source {

	public void method1() {
		System.out.println("this is basic Source");
	}
}

//想添加功能
public interface TargetTable {
	// 旧方法
	// public void method1();
	/**
	 * @see 添加新的方法
	 */
	public void method2();
}

//适配器

public class Adapter extends Source implements TargetTable {

	// Source实现了方法1,所有只需要实现方法2
	@Override
	public void method2() {
		System.out.println("this is other method");
	}

	
	public static void main(String[] args) {
		// 这中方法在集合中大量使用,例如ArrayList和LinkList,如果使用List做引用,则有一些方法没法实现
		// TargetTable tt = new Adapter(); 如果是接口引用,则需要在接口里面实现一下旧接口
		Adapter tt = new Adapter(); //如果直接用adapter类,直接调用就行
		tt.method1();
		tt.method2();
	}

}

    2.3 装饰模式

//共能还没实现,需要将功能用在不同的地方
//功能
public interface SourceInterf {

	public void method();
}

//实现类
public class Source implements SourceInterf {

	@Override
	public void method() {
		System.out.println("the original method!");
	}

}

// 装饰器

public class Decorator implements SourceInterf {

	/**
	 * @see 接口:需要实现的功能
	 * @see 基类:实现功能
	 * @see 装饰类:接受接口实现类,强化
	 */
	private SourceInterf sourceInterf;

	public Decorator(SourceInterf sourceInterf) {
		this.sourceInterf = sourceInterf;
	}

	@Override
	public void method() {
		System.out.println("before decorator!");
		sourceInterf.method();
		System.out.println("before decorator!");
	}

	public static void main(String[] args) {
		SourceInterf source = new Source();
		SourceInterf obj = new Decorator(source);
		obj.method();

	}
}

    2.4 代理模式

//需要在旧的功能上实现新功能
//功能
public interface SourceInterf {

	public void method();
}
//实现类
public class Source implements SourceInterf {

	@Override
	public void method() {
		System.out.println("the original method!");
	}

}
//代理类
public class Proxy implements SourceInterf {
	/**
	 * @see 接口:需要实现的功能
	 * @see 基类:实现功能
	 * @see 装饰类:实例化接口实现类,强化
	 */
	private SourceInterf sourceInterf;

	// 直接实例化
	public Proxy() {
		this.sourceInterf = new Source();
	}

	@Override
	public void method() {
		System.out.println("before decorator!");
		sourceInterf.method();
		System.out.println("before decorator!");
	}

	public static void main(String[] args) {
		SourceInterf obj = new Proxy();
		obj.method();
		//1、修改原有的方法来适应。这样违反了“对扩展开放,对修改关闭”的原则。
		//2、就是采用一个代理类调用原有的方法,且对产生的结果进行控制。这种方法就是代理模式。

	}
}

    2.5  享元模式

public class ConnectionP2 {
	/**
	 * @see 享元模式:类似于池技术
	 */
	private String url = "jdbc:mysql//localhost:33016/test";
	private String username = "root";
	private String password = "root";
	private String driverClassName = "com.mysql.jdbc.Driver";

	// 空闲池:存放空闲连接
	private Vector<Connection> freePool;
	// 空闲个数
	private int freeNumber = 100;
	private int poolSize = 100;
	private static ConnectionP2 instance = null;
	Connection conn = null;

	private ConnectionP2() {
		freePool = new Vector<Connection>(poolSize);
		for (int i = 0; i < poolSize; i++) {
			try {
				Class.forName(driverClassName);
				conn = DriverManager.getConnection(url, username, password);
				freePool.add(conn);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// 空闲数+1
	public void release() {
		synchronized (ConnectionP2.class) {
			freeNumber++;
		}
	}

	// 返回一个、空闲数-1
	public Connection getConnection() {
		synchronized (ConnectionP2.class) {
			if (!freePool.isEmpty()) {
				conn = freePool.get(freeNumber);
				freeNumber--;
			}
			return conn;
		}
	}
}


转载于:https://my.oschina.net/u/2246410/blog/597951


http://www.niftyadmin.cn/n/710091.html

相关文章

WPF's Style BasedOn

原文:WPFs Style BasedOn1 <Style x:Key"BasedStyle" BasedOn"{x:Null}" TargetType"{x:Type Control}"> 2 <Setter Property"FontFamily" Value"Microsoft YaHei" /> 3 <Setter Property"Font…

redis服务器及采集端设置

redis&#xff08;logstash&#xff09;.conf内容 #服务端配置,logstash抓取redis数据&#xff0c;配置名自取例一 #从redis读数据input {redis {host > "127.0.0.1"port > 6379type > "redis-input"dat…

7.配置zabbix报警

1.配置报警媒介&#xff1a;&#xff08;1&#xff09;点击administration下面的media types&#xff08;2&#xff09;点击右上角的create media type邮件报警我们使用centos的sendmail短信报警我们使用云片网的短信通道报警脚本的存放位置我们可以通过查看zabbix_server.conf…

时间服务器

方法1&#xff1a;与一个已知的时间服务器同步ntpdate time.nist.gov其中 time.nist.gov 是一个时间服务器.删除本地时间并设置时区为上海rm -rf /etc/localtimeln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime方法2&#xff1a;linux自动同步时间vi /etc/crontab加上…

【Spring】Spring学习笔记-01-入门级实例

听说当前Spring框架很流行&#xff0c;我也准备好好学学Spring开发&#xff0c;并将学习的过程和大家分享&#xff0c;希望能对志同道合的同学有所帮助。 以下是我学习Spring的第一个样例。1.Spring开发环境的搭建 我用的开发工具是MyEclipse 10&#xff0c;用maven管理jar包。…

使用Swift 字典模型互转 超级简单

写在前面的话 现在很多iOS项目的开发开始转向Swift语言。 相信 Swift语言很快会成为iOS工程师 必备技能。 字典转模型&#xff0c; 模型转转字典在开发过程中扮演非常重要的角色。 今天就和大家分享一下使用Swift&#xff0c;如何进行字典模型互转。 ** Demo在这里 为了让工作做…

laravel 权限管理 常用命令

use Spatie\Permission\Models\Role;use Spatie\Permission\Models\Permission;$role Role::create([name > writer]);$permission Permission::create([name > edit articles]);$permissions $user->permissions; //分配给用户的所有权限的列表$permissions $us…

HTTP消息推送的原理

2019独角兽企业重金招聘Python工程师标准>>> 消息推送的原理&#xff1a; 使用HTTP的 【异步】 和 【非阻塞】的长连接的原理。 1. 客户端发出一个http长连接请求&#xff0c;然后等待服务器的响应。这个请求是异步的&#xff0c;所以客户端可以继续工作&#xff0c…