选择适合的设计模式是写出一套优秀代码的根本,本篇文章小编就将带大家详细了解java中各种常见的设计模式。
单例模式
这是最常见的一种,单例设计是指一个类只允许产生一个实例化对象。
它也是最好理解的一种设计模式,分为懒汉式和饿汉式。
饿汉式:构造方法私有化,外部无法产生新的实例化对象,只能通过static方法取得实例化对象
class Singleton { /** * 在类的内部可以访问私有结构,所以可以在类的内部产生实例化对象 */ private static Singleton instance = new Singleton(); /** * private 声明构造 */ private Singleton() {} /** * 返回对象实例 */ public static Singleton getInstance() { return instance; } public void print() { System.out.println("Hello Singleton..."); } }
懒汉式:当第一次去使用Singleton对象的时候才会为其产生实例化对象的操作
class Singleton { /** * 声明变量 */ private static volatile Singleton singleton = null; /** * 私有构造方法 */ private Singleton() {} /** * 提供对外方法 * @return */ public static Singleton getInstance() { // 还未实例化 if (singleton == null) { synchronized(Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; } public void print() { System.out.println("Hello World"); } }
工厂模式
interface Sender { void Send(); } class MailSender implements Sender { @Override public void Send() { System.out.println("This is mail sender..."); } } class SmsSender implements Sender { @Override public void Send() { System.out.println("This is sms sender..."); } } public class FactoryPattern { public static void main(String[] args) { Sender sender = produce("mail"); sender.Send(); } public static Sender produce(String str) { if ("mail".equals(str)) { return new MailSender(); } else if ("sms".equals(str)) { return new SmsSender(); } else { System.out.println("输入错误..."); return null; } } }
装饰模式
interface Shape { void draw(); } /** * 实现接口的实体类 */ class Rectangle implements Shape { @Override public void draw() { System.out.println("Shape: Rectangle..."); } } class Circle implements Shape { @Override public void draw() { System.out.println("Shape: Circle..."); } } /** * 创建实现了 Shape 接口的抽象装饰类。 */ abstract class ShapeDecorator implements Shape { protected Shape decoratedShape; public ShapeDecorator(Shape decoratedShape) { this.decoratedShape = decoratedShape; } @Override public void draw() { decoratedShape.draw(); } } /** * 创建扩展自 ShapeDecorator 类的实体装饰类。 */ class RedShapeDecorator extends ShapeDecorator { public RedShapeDecorator(Shape decoratedShape) { super(decoratedShape); } @Override public void draw() { decoratedShape.draw(); setRedBorder(decoratedShape); } private void setRedBorder(Shape decoratedShape) { System.out.println("Border Color: Red"); } } /** * 使用 RedShapeDecorator 来装饰 Shape 对象。 */ public class DecoratorPattern { public static void main(String[] args) { Shape circle = new Circle(); Shape redCircle = new RedShapeDecorator(new Circle()); Shape redRectangle = new RedShapeDecorator(new Rectangle()); System.out.println("Circle with normal border"); circle.draw(); System.out.println("\nCircle of red border"); redCircle.draw(); System.out.println("\nRectangle of red border"); redRectangle.draw(); } }
策略模式
策略模式中定义了一系列算法,并把每个算法封装起来,使它们可以相互替换,且算法的变化不会影响到使用算法的客户。
/** * 抽象算法的策略类,定义所有支持的算法的公共接口 */ abstract class Strategy { /** * 算法方法 */ public abstract void AlgorithmInterface(); } /** * 具体算法A */ class ConcreteStrategyA extends Strategy { //算法A实现方法 @Override public void AlgorithmInterface() { System.out.println("算法A的实现"); } } /** * 具体算法B */ class ConcreteStrategyB extends Strategy { /** * 算法B实现方法 */ @Override public void AlgorithmInterface() { System.out.println("算法B的实现"); } } /** * 具体算法C */ class ConcreteStrategyC extends Strategy { @Override public void AlgorithmInterface() { System.out.println("算法C的实现"); } } /** * 上下文,维护一个对策略类对象的引用 */ class Context { Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } public void contextInterface() { strategy.AlgorithmInterface(); } } /** * 客户端代码:实现不同的策略 */ public class StrategyPattern { public static void main(String[] args) { Context context; context = new Context(new ConcreteStrategyA()); context.contextInterface(); context = new Context(new ConcreteStrategyB()); context.contextInterface(); context = new Context(new ConcreteStrategyC()); context.contextInterface(); } }
桥接模式
interface DrawAPI { public void drawCircle(int radius, int x, int y); } class RedCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y) { System.out.println("Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", " + y + "]"); } } class GreenCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y) { System.out.println("Drawing Circle[ color: green, radius: " + radius + ", x: " + x + ", " + y + "]"); } } abstract class Shape { protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI) { this.drawAPI = drawAPI; } public abstract void draw(); } class Circle extends Shape { private int x, y, radius; public Circle(int x, int y, int radius, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.radius = radius; } public void draw() { drawAPI.drawCircle(radius, x, y); } } //客户端使用代码 Shape redCircle = new Circle(100, 100, 10, new RedCircle()); Shape greenCircle = new Circle(100, 100, 10, new GreenCircle()); redCircle.draw(); greenCircle.draw();
以上就是本篇文章的所有内容,更多详细java入门知识敬请关注本站了解详情。
推荐阅读: