设计模式示例(Java 版)

设计模式

单例模式

保证一个类仅有一个实例,并提供一个访问它的全局访问点

  1. 构造方法私有化
  2. 声明一个本类对象
  3. 给外部提供一个静态方法获取对象实例

两种实例方式:

  1. 饿汉式
  2. 懒汉式

在项目中为什么要使用单例,单例有什么好处?

  1. 在设计一些工具类的时候(通常工具类只有功能方法,没有属性)
  2. 工具类可能会被频繁调用

目的是为了节省重复创建对象所带来的内存消耗,从而来提高效率

能不能用构造方法+静态方法来替代单例

public class Test()
{
    public static void main(String[] args)
    {
        SginleTon1 s1 = SingleTon1.getInstance();
        s1.print();
        SginleTon1 s11 = SingleTon1.getInstance();
        s11.print();
        
        System.out.println(s1==s11);  // true
        
        SginleTon2 s2 = SingleTon2.getInstance();
        s2.print();
        SginleTon2 s22 = SingleTon2.getInstance();
        s22.print();
        
		System.out.println(s2==s22);  // true
    }
}

// 饿汉式
class SingleTon1
{
    private SingleTon1(){}
    // 在类被加载后对象被创建,到程序结束后释放
    // 占用内存的时间长,提高效率
    private static SingleTon1 s = new SingleTon1();
    public static SingleTon1 getInstance()
    {
        return s;
    }
    public void print()
    {
        System.out.println("测试方法");
    }
}

// 懒汉式 v1:在多线程访问时会有安全问题
class SingleTon2()
{
    private SingleTon2(){}
    private static SingleTon2 s;

    // 在第一次调用 getInstance 方法时,对象被创建,到程序结束后释放
    // 占用内存的时间短,效率低(懒加载/延迟加载)
    public static SingleTon2 getInstance()
    {
        if (s == null) {
            s = new SingleTon2();
        }
        return s;
    }
    public void print()
    {
        System.out.println("测试方法");
    }
}

// 用构造方法+静态方法来实现工具类,比如 Math
// 在内存静态区一直存在
class Tools
{
    private Tools(){}
    public static void print1(){}
    public static void print2(){}
}

模板方法模式(抽象类应用)

模板方法模式(Template Method):定义一个操作中的算法的骨架,而将一些可变部分的实例延迟到子类中。模板方法模式使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定的步骤。

public class Test{
    public static voicd main(String[] args)
    {
        UserManager um = new UserManager();
        um.action("admin", "add");  // 执行了添加操作
        um.action("guest", "add");  // 你没有操作权限,请联系管理员
    }
}

abstract class BaseManager
{
    public static void action(String name, String method) 
    {
        if ("admin".equals(name)) {
            execute(method);
        } else {
            System.out.println("你没有操作权限,请联系管理员");
        }
    }
    
    public abstract void execute(String method);
}

class UserManager extends BaseManager
{
    public void execute(String method)
    {
        // 用户是否登录的验证
        // 验证成功后才可以执行以下操作
        if ("add".equals(method)) {
            System.out.println("执行了添加操作");
        } else if ("del".equals(method)) {
            System.out.println("执行了删除操作");
        }
    }
}

class ClassManager extends BaseManager{
    // ...
}

策略模式(接口应用)

策略模式(Strategy Pattern),定义了一系列的算法,将每一种算法封装起来并可以相互替换使用,策略模式让算法独立于使用它的客户应用而独立变化。

OO 设计原则:

  1. 面向接口编程(面向抽象编程)
  2. 封装变化
  3. 多用组合,少用继承
public class Test
{
    public static void main(String[] args)
    {
        BaseService usr = new UserService();
        usr.setISave(new FileSave());
        usr.add("2222");
    }
}

// 把可变的行为抽象出来,定义一系列的算法
// 这样的好处是这些行为可以在真正时相互替换
interface ISave
{
    public void save(String data);
}

class FileSave implements ISave
{
    public void save(String data)
    {
        System.out.println("把数据保存到文件中。。。");
    }
}

class NetSave implements ISave
{
    public void save(String data)
    {
        System.out.println("把数据保存到网络上");
    }
}


abstract class BaseService
{
    private ISave isave;
    public void setISave(ISave isave)
    {
        this.isave = isave;
    }
    public void add(String data)
    {
        System.out.println("检查数据合法性");
        isave.save(data);
        System.out.println("数据保存完毕");
    }
}

class UserService extends BaseService
{
}

简单工厂模式

是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家庭中最简单实用的模式。

public class Test
{
    public static void main(String[] args)
    {
        // 使用者和被使用者两者之间耦合,产生了依赖,当被使用者改变时,会影响到使用者
        Product phone = new Phone();
        phone.work();
        // 使用工厂模式来降低两者之间的依赖
        Product phone = ProductFactory.getProduct("phone");
    }
}

interface Product
{
    public void work();
}

class Phone implements Product
{
    public void work()
    {
        System.out.println("手机开始工作。。。");
    }
}

class Computer implements Product 
{
    public void work()
    {
        System.out.println("电脑开始工作。。。");
    }
}

// 工厂类
class ProductFactory
{
    public static Product getProduct(String name)
    {
        if ("phone".equals(name)) {
            return new Phone();
        } else if ("computer".equals(name)) {
            return new Computer();
        } else {
            return null;
        }
    }
}

静态代理模式

代理模式(Proxy):为其它对象提供一种代理以控制对这个对象的访问。

代理模式说白了就是“真实对象”的代表,在访问对象时引入一定程度的间接性,因为这种间接性可以附加多种用途。

public class Test
{
    public static void main(String[] args)
    {
        Action userAction = new UserAction();
        ActionProxy proxy = new ActionProxy(userAction);
        proxy.doAction();
    }
}

class ActionProxy implements Action
{
    private Action target;  // 被代理的对象
    
    public ActionProxy(Action target)
    {
        this.target = target;
    }
    
    // 执行操作
    public void doAction()
    {
        long startTime = System.out.currentTimeMillis();
        target.doAction();  // 执行真正的业务
        long endTime = System.out.currentTimeMillis();
        Sytem.out.println("共耗时:" + (endTime - startTime) + "ms");
    }
} 

interface Action
{
    public void doAction();
}

class UserAction implements Action
{
    public void doAction()
    {
        for (int i = 0; i < 100; i++) {
           System.out.println("用户开始工作了");
        }
    }
}

适配器模式

适配器模式(Adapter),将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

OO 设计原则:

  1. 面向接口编程(面向抽象编程)
  2. 封装变化
  3. 多用组合,少用继承
  4. 对修改关闭,对扩展开放

例 1:

public class Test
{
    public static void main(String[] args) 
    {
        PowerA powerA = new PowerAImpl();
        work(powerA);
        
        PowerB powerB = new PowerBImpl();
        // work(powerB); 不行
        Adapter adapter = new Adapter(powerB);
        work(adapter);
    }
    
    public static void work(PowerA a)
    {
        System.out.println("正在连接。。。");
        a.insert();
        System.out.println("工作结束。。。");
    }
}

interface PowerA
{
    public void insert();
}

class PowerAImpl implements PowerA
{
    public void insert()
    {
        System.out.println("电源 A 开始工作");
    }
}

interface PowerB
{
    public void connect();
}

class PowerBImpl implements PowerB
{
    public void connect()
    {
        System.out.println("电源 B 开始工作");
    }
}

// 适配器
class Adapter implements PowerA
{
    private PowerB powerB;
    
    public Adapter(PowerB powerB)
    {
        this.powerB = powerB;
    }
    public void insert()
    {
        powerB.connect();
    }
}

例 2:

interface Animal
{
    public void sing();
    public void cry();
    public void run();
    public void swim();
}

// 适配器类
abstract class AnimalBehaviour 
{
    public void sing(){}
    public void cry(){}
    public void run(){}
    public void swim(){}
}

class Dog implements AnimalBehaviour
{
    public void run()
    {
        System.out.println("我在跑。。。");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1) 优秀的程序应该是这样的:阅读时,感觉很优雅;新增功能时,感觉很轻松;运行时,感觉很快速,这就需要设计模式支撑。2) 设计模式包含了大量的编程思想,讲授和真正掌握并不容易,网上的设计模式课程不少,大多讲解的比较晦涩,没有真实的应用场景和框架源码支撑,学习后,只知其形,不知其神。就会造成这样结果: 知道各种设计模式,但是不知道怎么使用到真实项目。本课程针对上述问题,有针对性的进行了升级 (1) 授课方式采用 图解+框架源码分析的方式,让课程生动有趣好理解 (2) 系统全面的讲解了设计模式,包括 设计模式七大原则、UML类图-类的六大关系、23种设计模式及其分类,比如 单例模式的8种实现方式、工厂模式的3种实现方式、适配器模式的3种实现、代理模式的3种方式、深拷贝等3) 如果你想写出规范、漂亮的程序,就花时间来学习下设计模式吧课程内容和目标本课程是使用Java来讲解设计模式,考虑到设计模式比较抽象,授课采用 图解+框架源码分析的方式1) 内容包括: 设计模式七大原则(单一职责、接口隔离、依赖倒转、里氏替换、开闭原则、迪米特法则、合成复用)、UML类图(类的依赖、泛化和实现、类的关联、聚合和组合) 23种设计模式包括:创建型模式:单例模式(8种实现)、抽象工厂模式、原型模式、建造者模式、工厂模式。结构型模式:适配器模式(3种实现)、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式(3种实现)。行为型模式:模方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter模式)、状态模式、策略模式、职责链模式(责任链模式)2) 学习目标:通过学习,学员能掌握主流设计模式,规范编程风格,提高优化程序结构和效率的能力。
好的,下面是几个 Java 设计模式示例: 1. 单例模式(Singleton Pattern): ```java public class Singleton { private static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } ``` 2. 工厂模式(Factory Pattern): ```java public interface Shape { void draw(); } public class Circle implements Shape { @Override public void draw() { System.out.println("Drawing Circle"); } } public class Rectangle implements Shape { @Override public void draw() { System.out.println("Drawing Rectangle"); } } public class ShapeFactory { public Shape getShape(String shapeType) { if (shapeType == null) { return null; } else if (shapeType.equalsIgnoreCase("circle")) { return new Circle(); } else if (shapeType.equalsIgnoreCase("rectangle")) { return new Rectangle(); } return null; } } ``` 3. 观察者模式(Observer Pattern): ```java public interface Observer { void update(float temperature, float humidity, float pressure); } public interface Subject { void registerObserver(Observer o); void removeObserver(Observer o); void notifyObservers(); } public class WeatherData implements Subject { private ArrayList<Observer> observers; private float temperature; private float humidity; private float pressure; public WeatherData() { observers = new ArrayList<>(); } @Override public void registerObserver(Observer o) { observers.add(o); } @Override public void removeObserver(Observer o) { int i = observers.indexOf(o); if (i >= 0) { observers.remove(i); } } @Override public void notifyObservers() { for (Observer observer : observers) { observer.update(temperature, humidity, pressure); } } public void measurementsChanged() { notifyObservers(); } public void setMeasurements(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); } } public class CurrentConditionsDisplay implements Observer { private float temperature; private float humidity; private Subject weatherData; public CurrentConditionsDisplay(Subject weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); } @Override public void update(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; display(); } public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); } } ``` 以上是三个 Java 设计模式示例,分别是单例模式、工厂模式和观察者模式。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈挨踢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值