目录
设计模式的六大原则
1.开闭原则
对扩展开放,对修改关闭。在程序需要进行拓展的时候,不能区修改原有的代码,实现一个热插拔的效果。
2.里氏代换原则
里氏代换原则中说,任何基类出现的地方,子类一定可以出现。里氏代换原则是继承复用的基石,只有当派生类可以替换掉基类,且软件单位的功能不受影响时,基类才能被真正复用,而派生类也能在基类的基础上增加新的行为。
3.依赖倒转原则
这个原则是开闭原则的基础,具体内容:针对接口编程,依赖于抽象而不依赖于具体。
4.接口隔离原则
这个原则的意思:只用多个隔离的接口,比使用单个接口要好,还可以降低类的耦合度。由此可见,其实设计模式就是从大型软件构件出发便于升级维护的软件设计思想,强调降低依赖。降低耦合。
5.迪米特法则(最少知道原则)
一个实体应当尽量少地与其他实体之间发生相互作用。使得系统功能模块相对独立
6.合成复用原则
尽量使用合成/聚合的方法,而不是使用继承
1.单例模式
是Java中最简单的设计模式之一,它提供了一种创建对象的最佳方式。这种模式涉及到宇哥单一的类,该类负责创建自己的对象,同时保证只有单个对象被创建,这个类提供了一种访问唯一的对象的发生,可以直接访问,不需要实例化该类的对象。
注意:1.单例类只有一个实例。
2.单例类必须自己创建自己的唯一实例。
3.单例类必须给所有其他对象提供这一实例。
1.1饿汉式单例
//饿汉式单例
public class Singleton {
private static Singleton singleton = new Singleton();
private Singleton(){};
public static Singleton getSingleton(){
return singleton;
}
}
饿汉式单例在类加载的时候就已经创建了唯一的对象
1.2懒汉式单例
//懒汉式单例
public class Singleton {
private static Singleton singleton ;
private Singleton(){};
public static Singleton getSingleton(){
if(singleton == null){
singleton = new Singleton();
}
return singleton;
}
}
懒汉式单例:在第一次调用getSingleton()方法表示外部需要获取这个单例对象时时才会生成对象。
但是饿汉式单例在多线程的模式下不能保证只有一个对象;当线程进入getSingleton()方法时,可能看到的都是null,每个线程都会创建一个该类的对象。
使用双重加锁保证线程安全。
//懒汉式单例,使用双重加锁,volatile关键字保证单例对象初始化不会被打断来保证线程安全
public class Singleton {
private static volatile Singleton singleton ;
private Singleton(){};
public static Singleton getSingleton(){
if(singleton == null){
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
2.工厂模式
在工厂模式中,我们在创建对象不会对客户端暴露创建逻辑,并且是通过一个共同的接口来指向新创建的对象。定义一个创建的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。
实现:我们创建一个Shape的接口和实现接口的实体类。下一步是定义工厂类ShapeFactory。使用ShapeFactory的getShape来获取Shape对象。
创建接口:
public interface Shape {
void draw();
}
创建实体类:
import factory.Shape;
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle的draw方法");
}
}
class Square implements Shape {
@Override
public void draw() {
System.out.println("Square的draw方法");
}
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle的draw方法");
}
}
创建工厂,定义getShape方法:
import factory.impl.Circle;
import factory.impl.Rectangle;
import factory.impl.Square;
public class ShapeFactory {
public Shape getShape(String str){
if(str == null){
return null;
}
if(str.equals("Square")){
return new Square();
}
if(str.equals("Circle")){
return new Circle();
}
if(str.equals("Rectangle")){
return new Rectangle();
}
return null;
}
}
使用该工厂,通过传递类型信息来获取实体类的对象:
public class Main {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
Shape shape = shapeFactory.getShape("Square");
shape.draw();
Shape circle = shapeFactory.getShape("Circle");
circle.draw();
Shape rectangle = shapeFactory.getShape("Rectangle");
rectangle.draw();
}
}
运行结果:
Square的draw方法
Circle的draw方法
Rectangle的draw方法
3.抽象工厂模式
抽象工厂模式是围绕一个超级工厂来创建其他工厂。该超级工厂又称为其他工厂的工厂;在抽象模式中,接口是负责创建一个相关对象的工厂,不需要显示的指定他们的类,每个生成的工厂都能按照工厂模式提供对象。
实现:我们将创建Shape和Color接口和实现这些接口的实体类,下一步创建抽象工厂类AbsFactory。接着定义工厂类ShapeFactory和ColorFactory,这两个工厂类都是扩展了AbsFactory,然后创建一个工厂创造类FactoryProducer
创建一个Shape接口:
public interface Shape {
void draw();
}
创建Shape接口的实体类:
import factory.Shape;
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle的draw方法");
}
}
class Square implements Shape {
@Override
public void draw() {
System.out.println("Square的draw方法");
}
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle的draw方法");
}
}
创建Color接口:
public interface Color {
void fill();
}
创建Color接口的实体类
public class Black implements Color {
@Override
public void fill() {
System.out.println("Black的fill方法");
}
}
class Green implements Color {
@Override
public void fill() {
System.out.println("Green的fill方法");
}
}
class Red implements Color {
@Override
public void fill() {
System.out.println("Red的fill方法");
}
}
为Color和Shape对象创建抽象类来获取工厂
public abstract class AbsFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape);
}
创建扩展了AbsFactory的工厂类,基于给定的信息生成实体类的对象
public class ShapeFactory extends AbsFactory {
@Override
public Color getColor(String color) {
return null;
}
public Shape getShape(String str){
if(str == null){
return null;
}
if(str.equals("Square")){
return new Square();
}
if(str.equals("Circle")){
return new Circle();
}
if(str.equals("Rectangle")){
return new Rectangle();
}
return null;
}
}
public class ColorFactory extends AbsFactory{
@Override
public Color getColor(String str) {
if(str == null){
return null;
}
if(str.equals("Red")){
return new Red();
}
if(str.equals("Black")){
return new Black();
}
if(str.equals("Green")){
return new Green();
}
return null;
}
@Override
public Shape getShape(String shape) {
return null;
}
}
生成一个工厂创造器,通过性状获取颜色信息来获取工厂。
public class FactoryProducer {
public static AbsFactory getFactory(String str){
if(str == null){
return null;
}
if(str.equals("Shape")){
return new ShapeFactory();
}
if(str.equals("Color")){
return new ColorFactory();
}
return null;
}
}
使用FactoryProducer来获取AbsFactory,通过传递类型信息来获取实体类的对象
public class Main {
public static void main(String[] args) {
AbsFactory shapeFactory = FactoryProducer.getFactory("Shape");
Shape shape = shapeFactory.getShape("Square");
shape.draw();
Shape circle = shapeFactory.getShape("Circle");
circle.draw();
Shape rectangle = shapeFactory.getShape("Rectangle");
rectangle.draw();
AbsFactory colorfactory = FactoryProducer.getFactory("Color");
Color red = colorfactory.getColor("Red");
red.fill();
Color black = colorfactory.getColor("Black");
black.fill();
Color green = colorfactory.getColor("Green");
green.fill();
}
}
输出结果:
Square的draw方法
Circle的draw方法
Rectangle的draw方法
Red的fill方法
Black的fill方法
Green的fill方法