设计模式之结构型模式

1. 适配器模式【读卡器连接内存卡和笔记本】

  1. 适配器模式作为两个不兼容的接口之间的桥梁【读卡器连接内存卡和笔记本】
  1. 适配器模式实现流程
//1. 为媒体播放器和更高级的媒体播放器创建接口
public interface MediaPlayer{}
public interface AdvancedMediaPlayer{
	//内部有两个方法
}

//2. 创建实现了AdvancedMediaPlayer接口的实体类
public class VlcPlayer implements AdvancedMediaPlayer{
	//实现第一个方法,第二个什么也不做
}
public class Mp4Player implements AdvancedMediaPlayer{
	//实现第二个方法,第一个什么也不做
}

//3. 创建实现了MediaPlayer接口的适配器类
public class MediaAdapter implements MediaPlayer{
	AdvancedMediaPlayer advancedMediaPlayer;
	//MediaPlayer播放器支持的话就用它自己的,不支持的话就用advancedMediaPlayer中的方法
}

//4. 创建MediaPlayer接口的实体类
public class AudioPlayer implements MediaPlayer{
	MediaAdapter mediaAdapter;
	//mediaAdapter来接收应该用哪种播放器
}

2. 桥接模式【一个接口当作另一个接口中的一个对象】

  1. 用于把抽象化与实现化解耦,使得二者可以独立变化。通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。【一个接口当作另一个接口中的一个对象】
  1. 桥接模式实现流程
//1. 创建桥实现接口
public interface DrawAPI{public void drawCircle(int radius, int x, int y);}

//2. 创建DrawAPI接口的实体桥接实现类
public class RedCircle implements DrawAPI{//实现drowCircle方法}
public class GreenCircle implements DrawAPI{//实现drowCircle方法}

//3. 使用DrawAPI接口创建抽象类Shape【核心】
public abstract class Shape {
   protected DrawAPI drawAPI;
   protected Shape(DrawAPI drawAPI){
      this.drawAPI = drawAPI;
   }
   public abstract void draw();  
}

//4. 创建实现了Shape抽象类的实体类
public 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);
   }
}

//5. 调用Shape和DrawAPI类画出不同颜色的圆
public class BridgePatternDemo {
   public static void main(String[] args) {
      Shape redCircle = new Circle(100,100, 10, new RedCircle());
      Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
 
      redCircle.draw();
      greenCircle.draw();
   }
}

3. 过滤器模式【筛选出满足要求的值】

  1. 使用不同的标准来过滤掉一组对象,通过逻辑运算以解耦的方式把它们连接起来。【筛选出满足要求的值】
  1. 过滤器模式的实现流程
//1. 创建一个类,在该类上应用标准
public class Person{name, gender, maritalStatus, 还有有参构造和getxxx方法}

//2. 为标准创建一个接口
public interface Criteria{
	public List<Person> meetCriteria(List<Person> persons)
}

//3. 创建实现了Criteria接口的实体类
public class CriteriaMale implements Criteria {
	//只接收male
}
public class CriteriaFemale implements Criteria {
	//只接收female
}
public class CriteriaSingle implements Criteria {
	//只接收single
}

public class AndCriteria implements Criteria{
   private Criteria criteria;
   private Criteria otherCriteria;
 
   public AndCriteria(Criteria criteria, Criteria otherCriteria) {
      this.criteria = criteria;
      this.otherCriteria = otherCriteria; 
   }
 	
   @Override
   public List<Person> meetCriteria(List<Person> persons) {
      List<Person> firstCriteriaPersons = criteria.meetCriteria(persons);     
      return otherCriteria.meetCriteria(firstCriteriaPersons);
   }
}

public class OrCriteria implements Criteria {
 
   private Criteria criteria;
   private Criteria otherCriteria;
 
   public OrCriteria(Criteria criteria, Criteria otherCriteria) {
      this.criteria = criteria;
      this.otherCriteria = otherCriteria; 
   }
 
   @Override
   public List<Person> meetCriteria(List<Person> persons) {
      List<Person> firstCriteriaItems = criteria.meetCriteria(persons);
      List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons);
 
      for (Person person : otherCriteriaItems) {
         if(!firstCriteriaItems.contains(person)){
           firstCriteriaItems.add(person);
         }
      }  
      return firstCriteriaItems;
   }
}

//4. 调用上边的方法来过滤

4. 组合模式【套娃模式】

  1. 把一组相似的对象当作一个单一的对象。【套娃模式】
  1. 组合模式实现流程
//1. 创建Employee类,该类带有Employee对象的列表
public class Employee{
	private List<Employee> subordinates;
	public void add(Employee e) {subordinates.add(e);}
}

//2. 使用Employee类来创建和打印员工的层次结构
public class Demo{
	Employee CED = xxx;
	Employee headSales = xxx;
	Employee headMarketing = xxx;
	Employee cleark1 = xxx;
	Employee cleark2 = xxx;
	Employee salesExecutive1 = xxx;
    Employee salesExecutive2 = xxx;
	
	//开始套娃模式
	CEO.add(headSales);
	CEO.add(headMarketing);
	headSales.add(salesExecutive1);
    headSales.add(salesExecutive2);
	headMarketing.add(clerk1);
	headMarketing.add(clerk2);
}

5. 装饰器模式【类似于子类继承】

  1. 允许向一个现有的对象添加新的功能,同时又不改变其结构。【类似于子类继承】
  1. 装饰器模式的实现流程
//1. 创建接口
public interface Shape{}

//2. 创建接口的实现类
public class Rectangle implements Shape{}
public class Circle implements Shape{}

//3. 创建实现了Shape接口的抽象装饰类
public abstract class ShapeDecorator implements Shape{
   protected Shape decoratedShape;
 	
   public ShapeDecorator(Shape decoratedShape){
      this.decoratedShape = decoratedShape;
   }
 
   public void draw(){
      decoratedShape.draw();
   }  
}

//4. 创建扩展了ShapeDecorator类的实体装饰类
public 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");
   }
}

//5. 调用就行了
Shape circle = new Circle();
      ShapeDecorator redCircle = new RedShapeDecorator(new Circle());
      ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangle());
      circle.draw();
      redCircle.draw();
      redRectangle.draw();

6. 外观模式【外观展现,实现隐藏】

  1. 隐藏系统的复杂性,并向客户端提供一个客户端可以访问系统的接口。【外观展现,实现隐藏】
  1. 外观模式实现流程
//1. 创建接口
public interface Shape{}

//2. 创建接口的实体类
public class Rectangle implements Shape{}
public class Square implements Shape{}
public class Circle implements Shape{}

//3. 创建外观类
public class ShapeMaker{}

//4. 利用外观类来调用实体类的方法
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.xxxCircle();
shapeMaker.xxxRectangle();
shapeMaker.xxxSquare();

7. 享元模式【重复利用模式】

  1. 主要用于减少创建对象的数量【重复利用模式】
  1. 享元模式的实现流程
//1. 创建接口
public inteface Shape{}

//2. 创建接口的实体类
public class Circle implements Shape{}

//3. 创建工厂,生成基于给定信息的实体类的对象
public class ShapeFactory{
	//就是靠HashMap来存储的【核心】
	private static final HashMap<String, Shape> circleMap = new HashMap<>();
}

8. 代理模式【生产商和经销商】

  1. 一个类代表另一个类的功能。【生产商和经销商】
  1. 代理模式实现流程
//1. 创建一个接口
public interface Image{}

//2. 创建接口的实体类
public class RealImage implements Image{}
public class ProxyImage implements Image{
	//【核心】
	@Override
	public void display(){
		if(realImage == null){
			realImage = new RealImage(fileImage);
		}
		realImage.display();
	}
}

//3. 当被请求时,使用ProxyImage来获取RealImage类的对象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值