23种经度的设计模式:
#设计模式:设计模式是在大量的实践中总结和理论化之后优选的代码结构、编程风格、以及解决问题的思考方式
创建型模式,共5种:工厂方法模式,抽象工厂模式,单例模式,建造者模式,原型模式
结构型模式,共7种:适配器模式,装饰器模式,代理模式,外观模式,桥接模式,组合模式,享元模式
行为型模式,共11种:策略模式,模仿方法模式,观察者模式,迭代器模式,责任链模式,命令模式,备忘录模式,
状态模式,访问者模式,中介者模式,解释器模式
单例模式 - Singleton
#概述:采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例
#优点:减少了系统性能的开销
#区别:
#饿汉式: 坏处:对象加载时间过长 好处:线程安全的
#懒汉式: 坏处:线程不安全的(多线程时需要修改) 好处:延迟对象的创建
//饿汗式
class Singleton1 {
//声明当前类对象
private static Singleton1 instance = new Singleton1();
//私有化类的构造器
private Singleton1(){
}
public static Singleton1 getInstance(){
return instance;
}
}
//懒汉式
class Singleton2 {
//声明当前类对象
private static Singleton2 instance = null;
//私有化类的构造器
private Singleton2(){
}
//返回类对象
public static Singleton2 getInstance(){
if(instance == null){
instance = new Singleton2();
}
return instance;
}
}
//懒汉式(线程安全)
class Singleton2 {
//声明当前类对象
private static Singleton2 instance = null;
//私有化类的构造器
private Singleton2(){
}
//返回类对象
public static Singleton2 getInstance(){
if(instance == null){
synchronized(){
if(instance == null){
instance = new Singleton2();
}
}
}
return instance;
}
}
模板方法设计模式 - TemplateMethod
#概述:抽象类作为多个子类的通用模板,子类在抽象类的基础上进行扩展,改造,但子类总体上会保留抽象类的行为方式
#解决的问题:当功能内部一部分实现是确定的,一部分实现是不确定的,这时可以把不确定的部分暴露出去,让子类去实现
//测试
public class TemplateMethodTest{
public static void main(String[] args) {
Template t = new Template();
t.countTime();
}
}
abstract class TemplateMethod {
//计算时间
public void countTime(){
long start = System.currentTimeMillis();
//计算代码运行时间,属于不确定部分,抽离
this.code();
long end = System.currentTimeMillis();
System.out.println("运行时间是" + (end - start));
}
public abstract void code();
}
class Template extends TemplateMethod{
@Override
public void code() {
for (int i = 2; i <= 1000; i++) {
boolean isFlag = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if(i % j == 0){
isFlag = false;
break;
}
}
if(isFlag){
System.out.println(i + "是质数");
}
}
}
}
代理模式(静态代理) - proxy
#概述:为其他对象提供一种代理以控制对这个对象的访问
//测试
public class ProxyTest {
public static void main(String[] args) {
Proxy proxy = new Proxy(new Server());
proxy.browse();
}
}
//接口
interface Network{
abstract void browse();
}
//被代理类
class Server implements Network{
@Override
public void browse() {
System.out.println("真实的服务器Server");
}
}
//代理类
class Proxy implements Network{
private Network work;
public Proxy(Network works){
this.work = works;
}
public void checkServer(){
System.out.println("检查服务checkServer");
}
@Override
public void browse() {
this.checkServer();
this.work.browse();
}
}
代理模式(动态代理) - proxy
package com.powernode.java;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Human{
String getBelief();
void eat(String food);
}
//被代理类
class Man implements Human{
@Override
public String getBelief() {
return "我相信我可以飞";
}
@Override
public void eat(String food) {
System.out.println("我喜欢吃" + food);
}
}
class ProxyFactory{
//动态返回代理类对象
public static Object getProxyFactory(Object obj){
MyInvocationHanlder hanlder = new MyInvocationHanlder();
hanlder.bind(obj);
//参数一:哪个类的加载器加载的
//参数二:被代理类实现的接口
//参数三:动态代理方法调用
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(), hanlder);
}
}
class MyInvocationHanlder implements InvocationHandler {
//被代理类对象
private Object obj;
public void bind(Object obj) {
this.obj = obj;
}
//被代理类调用方法时,会自动调用此方法
//参数一:代理类对象,参数二:代理类调用的方法,参数三:方法需要的参数
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(this.obj,args);
}
}
public class ProxyTest {
public static void main(String[] args) {
Man man = new Man();
Human proxyFactory = (Human) ProxyFactory.getProxyFactory(man);
proxyFactory.eat("荔枝");
System.out.println(proxyFactory.getBelief());
}
}
工厂模式 - factory
(1)无工厂模式
#概述:实现了创建者与调用者的分离,即将创建对象的具体过程屏蔽提高起来,达到提高灵活性的目的
public class FactoryTest {
public static void main(String[] args) {
//创建者与调用者的没有分离
Audi audi = new Audi();
audi.run();
Byd byd = new Byd();
byd.run();
}
}
interface Car{
void run();
}
class Audi implements Car{
@Override
public void run() {
System.out.println("奥迪");
}
}
class Byd implements Car{
@Override
public void run() {
System.out.println("比亚迪");
}
}
(2)简单工厂模式
#概述:实现了创建者与调用者的分离,即将创建对象的具体过程屏蔽提高起来,达到提高灵活性的目的
#缺点:对于增加新产品,不修改代码的话,是无法扩展的,违反了开闭原则(对扩展开发,对修改封闭)
public class FactoryTest {
public static void main(String[] args) {
CarFactory car = new CarFactory();
Car audi1 = car.getCar("奥迪");
audi1.run();
Car byd1 = car.getCar("比亚迪");
byd1.run();
Car audi2 = CarFactory.getAudi();
audi2.run();
Car byd2 = CarFactory.getByd();
byd2.run();
}
}
interface Car{
void run();
}
class Audi implements Car{
@Override
public void run() {
System.out.println("奥迪");
}
}
class Byd implements Car{
@Override
public void run() {
System.out.println("比亚迪");
}
}
//工厂类
class CarFactory{
//方式一
public static Car getCar(String type){
if("奥迪".equals(type)){
return new Audi();
}
if("比亚迪".equals(type)){
return new Byd();
}
return null;
}
//方式二
public static Car getAudi(){
return new Audi();
}
public static Car getByd(){
return new Byd();
}
}
(3)工厂方法模式
#概述:实现了创建者与调用者的分离,即将创建对象的具体过程屏蔽提高起来,达到提高灵活性的目的
public class FactoryTest {
public static void main(String[] args) {
Car audi = new AudiFactory().getCar();
audi.run();
Car byd = new BydFactory().getCar();
byd.run();
}
}
interface Car{
void run();
}
class Audi implements Car{
@Override
public void run() {
System.out.println("奥迪");
}
}
class Byd implements Car{
@Override
public void run() {
System.out.println("比亚迪");
}
}
//工厂接口
interface Factory{
Car getCar();
}
//工厂类
class AudiFactory implements Factory{
@Override
public Car getCar() {
return new Audi();
}
}
class BydFactory implements Factory{
@Override
public Car getCar() {
return new Byd();
}
}
(4)抽象工厂模式(反射)
#概述:实现了创建者与调用者的分离,即将创建对象的具体过程屏蔽提高起来,达到提高灵活性的目的