设计模式 结构型模式

适配模式

在下图中可以看出,Adaptee类并没有sampleOperation2()方法,而客户端则期待这个方法。为使客户端能够使用Adaptee类,提供一个中间环节,即类Adapter,把Adaptee的API与Target类的API衔接起来。Adapter与Adaptee是继承关系。
在这里插入图片描述

interface Target{
	// Adaptee有这个方法
	void sampleOperation1();
	// Adaptee没有这个方法
	void sampleOperation2();
}

class Adaptee{
	public void sampleOperation1(){}
}

class Adapter extends Adaptee implements Target{
	public void sampleOperation2(){}
}

这好比ADSL,用户想通过电话线上网,但是电话线不能直接接到电脑上,但用户就是想通过电话线上网,所以引入了调制解调器,调制解调器就是做适配器的作用。

组合模式

组合模式就比较简单了,以树型结构组成的层次关系,忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
在这里插入图片描述
以文件和文件夹为例

abstract class Compoent{}

class File extends Compoent{
	private String fileName;
}
class FileFolder extends Compoent{
	private List<Compoment> files;
	private String fileFolderName;
	public void scan(){
        for(component f:files){
            if(f instanceof File){
                System.out.println("File "+((File) f).filename);
            }else if(f instanceof Folder){
                Folder e = (Folder)f ;
                System.out.println("Folder "+e.foldername);
                e.scan();
            }
        }
    }
}

桥接模式

在这里插入图片描述它的主要特点是把抽象(Abstraction)与行为实现(Implementation)分离开来,从而可以保持各部分的独立性以及应对他们的功能扩展。

1.Client 调用端

这是Bridge模式的调用者。

2.抽象类(Abstraction)

抽象类接口(接口这货抽象类)维护队行为实现(implementation)的引用。它的角色就是桥接类。

3.Refined Abstraction

这是Abstraction的子类。

4.Implementor

行为实现类接口(Abstraction接口定义了基于Implementor接口的更高层次的操作)

5.ConcreteImplementor

Implementor的子类

interface Implementor{
	// 定义其实现类必须实现的接口
	public void operation();
}

public class ConcreateImplementorA implements Implementor{
	public void operation(){}
}
public class ConcreateImplementorB implements Implementor{
	public void operation(){}
}

// 桥接类
public abstract class Abstract{
	private Implementor implementor;
	public void setTmplementor(Implementor implementor){
		this.implementor = implementor;
	}
	public void getTmplementor(){
		return this.implementor;
	}
	protected viod operation(){
		this.implementor.operation();
	}
}

public class RefinedAbstraction extends Abstract{
	protected void operation(){
		super.getImplementor.operation();
	}
}

装饰者模式

装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。

interface Source{ void method();}
public class Decorator implements Source{
    // 这是真实的对象
    private Source source ;
    public void decotate1(){
        System.out.println("decorate");
    }
    // 这是包装类提供的包装方法
    @Override
    public void method() {
        decotate1();
        source.method();
    }
}

代理模式

装饰模式应该为所装饰的对象增强功能;代理模式对代理的对象施加控制,并不提供对象本身的增强功能。
客户端通过代理类访问,代理类实现具体的实现细节,客户只需要使用代理类即可实现操作。

interface BuyHouse{ void buyHosue();}

class BuyHouseImpl implements BuyHouse{
    @Override
    public void buyHosue() {
    }
}

class Proxy implements BuyHouse{
    private BuyHouse buyHosue;

    public void setBuyHouse(final BuyHouse buyHouse){
	this.buyHouse = buyHouse;
    }
    @Override
    public void buyHosue() {
        System.out.println("买房前准备");
        buyHosue.buyHosue();
        System.out.println("买房前准备");
    }
}

外观模式

用一个大外部接口统一操作各个子对象


public interface Shape {
   void draw();
}


public class Rectangle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Rectangle::draw()");
   }
}

public class Square implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Square::draw()");
   }
}

public class Circle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Circle::draw()");
   }
}

public class ShapeMaker {
   private Shape circle;
   private Shape rectangle;
   private Shape square;
 
   public ShapeMaker() {
      circle = new Circle();
      rectangle = new Rectangle();
      square = new Square();
   }
 
   public void drawCircle(){
      circle.draw();
   }
   public void drawRectangle(){
      rectangle.draw();
   }
   public void drawSquare(){
      square.draw();
   }
}

public class FacadePatternDemo {
   public static void main(String[] args) {
      ShapeMaker shapeMaker = new ShapeMaker();
 
      shapeMaker.drawCircle();
      shapeMaker.drawRectangle();
      shapeMaker.drawSquare();      
   }
}

享元模式

使用共享对象的方法,用来尽可能减少内存使用量以及分享资讯。通常使用工厂类辅助,例子中使用一个HashMap类进行辅助判断,数据池中是否已经有了目标实例,如果有,则直接返回,不需要多次创建重复实例。

abstract class flywei{ }

public class Flyweight extends flywei{
    Object obj ;
    public Flyweight(Object obj){
        this.obj = obj;
    }
}

class  FlyweightFactory{
    private HashMap<Object,Flyweight> data;

    public FlyweightFactory(){ data = new HashMap<>();}

    public Flyweight getFlyweight(Object object){
        if ( data.containsKey(object)){
            return data.get(object);
        }else {
            Flyweight flyweight = new Flyweight(object);
            data.put(object,flyweight);
            return flyweight;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/tsfx051435adsl/article/details/83444662