软件设计模式的学习

一.单例模式


1.定义
单例模式的定义:指一个类只有一个实例,且该类能自行创建这个实例的一种模式。

例如:Windows 中只能打开一个任务管理器,这样可以避免因打开多个任务管理器窗口而造成内存资源的浪费,或出现各个窗口显示内容的不一致等错误。

2.实现方式
常见的实现方式有:懒汉模式、饥汉模式、双重校验锁、静态内部类、枚举等方式实现
懒汉模式:

/**
 * @author hz
 * @version 1.0
 */
public class Singleton {
    private static Singleton instance = null;
    private Singleton(){}
    public static Singleton getInstance(){
        //如果还没有被实例化过,就实例化一个,然后返回
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

通过关键字synchronized用于确保getInstance方法线程安全,但是这种方式弊端特别大,因为所有线程到达该方法以后需要进行排队等候,所以对性能的损耗非常大,该处理解决了线程安全安全问题,并没有解决效率问题,如果要既要解决线程安全问题又要解决效率问题则需要我们后面的双重校验锁方式。

懒汉模式将实例化的时机放到了需要使用的时候(饿汉是类加载了就有实例),也就是“延迟加载”,相比饿汉,能避免了在加载的时候实例化有可能用不到的实例,但是问题也很明显,我们要花精力去解决线程安全的问题。

二.抽象工厂模式


1.定义:抽象工厂(AbstractFactory)模式的定义:是一种为访问类提供一个创建一组相关或相互依赖对象的接口,且访问类无须指定所要产品的具体类就能得到同族的不同等级的产品的模式结构,这里便出现了两个概念:同族和同等级。
2.举例:
比如我们现在有一个需求:有两个农场,第一个农场用于养牛和种菜,第二个农场用于养马和种水果。经过分析我们生产的产品为不同的类型,工厂方法模式只能完成同一系列产品的生产,而如果需要生产不同系列的产品,则需要我们使用抽象工厂模式

/**
 * @author  hz
 * @version 1.0
 */
public class FarmTest {
    public static void main(String[] args) {
        try {
            Farm f;
            Animal a;
            Plant p;
            //读取相应的配置信息,用于生产工厂
            f = (Farm) ReadXML.getObject();
            a = f.newAnimal();
            p = f.newPlant();
            a.show();
            p.show();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
//抽象产品:动物类
interface Animal {
    public void show();
}
//具体产品:马类
class Horse implements Animal {
    public Horse() {
        System.out.println("具体马类的生成");
    }
    public void show() {
        System.out.println("执行马类的相应操作");
    }
}
//具体产品:牛类
class Cattle implements Animal {

    public Cattle() {
        //具体牛类的生成
        System.out.println("具体牛类的生成");
    }
    public void show() {
        System.out.println("执行马类的相应操作");
    }
}
//抽象产品:植物类
interface Plant {
    public void show();
}
//具体产品:水果类
class Fruitage implements Plant {

    public Fruitage() {
        System.out.println("具体水果类生成");
    }
    public void show() {
        System.out.println("执行水果类的相应操作");
    }
}
//具体产品:蔬菜类
class Vegetables implements Plant {
    public Vegetables() {
        System.out.println("具体蔬菜类生成");
    }
    public void show() {
        System.out.println("执行蔬菜类的相应操作");
    }
}
//抽象工厂:农场类
interface Farm {
    public Animal newAnimal();
    public Plant newPlant();
}
//具体工厂:农场类1
class SGfarm implements Farm {
    public Animal newAnimal() {
        System.out.println("新牛出生!");
        return new Cattle();
    }
    public Plant newPlant() {
        System.out.println("蔬菜长成!");
        return new Vegetables();
    }
}
//具体工厂:农场类2
class SRfarm implements Farm {
    public Animal newAnimal() {
        System.out.println("新马出生!");
        return new Horse();
    }
    public Plant newPlant() {
        System.out.println("水果长成!");
        return new Fruitage();
    }
}

三.工厂方法模式


1.定义:我们把被创建的对象称为“产品”,把创建产品的对象称为“工厂”。如果要创建的产品不多,只要一个工厂类就可以完成,这种模式叫“简单工厂模式”。
2.举例:

/**
 * @author hz
 * @version 1.0
 */
public class Client {
    //抽象产品
    public interface Product {
        void show();
    }
    //具体产品:ProductA
    static class ConcreteProduct1 implements Product {
        public void show() {
            System.out.println("具体产品1显示...");
        }
    }
    //具体产品:ProductB
    static class ConcreteProduct2 implements Product {
        public void show() {
            System.out.println("具体产品2显示...");
        }
    }
    final class Const {
        static final int PRODUCT_A = 0;
        static final int PRODUCT_B = 1;
        static final int PRODUCT_C = 2;
    }
    static class SimpleFactory {
        public static Product makeProduct(int kind) {
            switch (kind) {
                case Const.PRODUCT_A:
                    return new ConcreteProduct1();
                case Const.PRODUCT_B:
                    return new ConcreteProduct2();
            }
            return null;
        }
    }
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值