美文网首页
2019-04-11 软件设计师 设计模式

2019-04-11 软件设计师 设计模式

作者: 米_8d62 | 来源:发表于2019-04-11 16:00 被阅读0次

    结构型模式(外桥组元代饰配)

    一)外观模式(Facade)

    定义:为子系统中一组不同的接口提供统一的接口
    适用:
    1.子系统正逐渐变得复杂,应用模式的过程中演化出许多类。可以使用外观为这些子系统类提供一个较简单的接口。
    2.可以使用外观对子系统分层,每个字系统级别有一个外观做为入口。让她们通过外观进行通信,可以简化他们的依赖关系。
    代码示例:

    // 一个维修汽车厂系统包括,小车维修、公交维修、验收 
    // 小车
    class Car{
        // 维修
        public void repair();
    }
    // 公车
    class Bus{
        // 维修
        public void repair();
    }
    // 维修人员
    class Repairer{
        public void doRepair();
    }
    // 验收人员
    class Checker{
        public void doCheck();
    }
    
    // 老板不可能管的那么细,他需要雇佣一个经理
    class Leader{
        private Repairer repairer;
        private Checker checker;
        public void repair();
        public boolean check();
    }
    public class TestFacade {
        public void test(){
            // 老板接一个单子时,直接叫经理去修车,然后再问经理是否完成,这个经理就是一个外观
            Leader leader = new Leader();
            // 通知经理修车,不管经理下面有多少个维修人员,验收人员
            leader.repair();
            boolean isOk = leader.check();
        }
    }
    

    二)桥接模式(Bridge)

    定义:将抽象部分与它的实现部分分离,使它可以独立的变更。
    适用:
    1.不想在抽象与实现之间形成固定的绑定关系(这样就能在运行时切换实现)。
    2.抽象与实现都应可以通过子类化独立进行扩展。
    3.对抽象的实现进行修改不应影响客户端代码。
    4.如果每个实现需要额外的子类以细化抽象,则说明有必要把它们分成两个部分。
    5.想在带有不同抽象接口的多个对象之间共享一个实现。
    代码示例:

    interface ILeader{
        public void doSomething();
    }
    class LeaderA implements ILeader{
        @Override
        public void doSomething() {}
    }
    class LeaderB implements ILeader{
        @Override
        public void doSomething() {}
    }
    class Boss {
        ILeader leader;
        public void setLeader(ILeader leader) {
            this.leader = leader;
        }
        public void doSomething(){
            this.leader.doSomething();
        }
    }
    public class TestBirdge {
        public void test(){
            Boss boss = new Boss();
            LeaderA leaderA = new LeaderA();
            boss.setLeader(leaderA);
            boss.doSomething();
            // 当某个经理离职的时候,老板可以再找一个有经验的经理来做事,
            LeaderB leaderB = new LeaderB();
            boss.setLeader(leaderB);
            boss.doSomething();
        }
    }
    

    三)组合模式(Composite)

    定义:将对象组合成树形结构以表示’部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
    适用:
    1.想获得对象抽象的树形表示(部分-整体层次结构)。
    2.想让客户端统一处理组合结构中的所有对象。
    代码示例:

    public class Employee {
       private String name;
       private String dept;
       private int salary;
       private List<Employee> subordinates;
    
       //构造函数
       public Employee(String name,String dept, int sal) {
          this.name = name;
          this.dept = dept;
          this.salary = sal;
          subordinates = new ArrayList<Employee>();
       }
    
       public void add(Employee e) {
          subordinates.add(e);
       }
    
       public void remove(Employee e) {
          subordinates.remove(e);
       }
    
       public List<Employee> getSubordinates(){
         return subordinates;
       }
    
       public String toString(){
          return ("Employee :[ Name : "+ name 
          +", dept : "+ dept + ", salary :"
          + salary+" ]");
       }   
    }
    public class CompositePatternDemo {
       public static void main(String[] args) {
          Employee CEO = new Employee("John","CEO", 30000);
    
          Employee headSales = new Employee("Robert","Head Sales", 20000);
    
          Employee headMarketing = new Employee("Michel","Head Marketing", 20000);
    
          Employee clerk1 = new Employee("Laura","Marketing", 10000);
          Employee clerk2 = new Employee("Bob","Marketing", 10000);
    
          Employee salesExecutive1 = new Employee("Richard","Sales", 10000);
          Employee salesExecutive2 = new Employee("Rob","Sales", 10000);
    
          CEO.add(headSales);
          CEO.add(headMarketing);
    
          headSales.add(salesExecutive1);
          headSales.add(salesExecutive2);
    
          headMarketing.add(clerk1);
          headMarketing.add(clerk2);
    
          //打印该组织的所有员工
          System.out.println(CEO); 
          for (Employee headEmployee : CEO.getSubordinates()) {
             System.out.println(headEmployee);
             for (Employee employee : headEmployee.getSubordinates()) {
                System.out.println(employee);
             }
          }     
       }
    }
    

    四)享元模式(Flyweight)

    定义:运用共享技术有效地支持大量细粒度的对象。
    适用:
    1.系统中有大量对象时。
    2.这些对象消耗大量内存时。
    3.这些对象的状态大部分可以外部化时。
    代码示例:

    
    public interface Shape {
       void draw();
    }
    
    public class Circle implements Shape {
       private String color;
       private int x;
       private int y;
       private int radius;
     
       public Circle(String color){
          this.color = color;     
       }
     
       public void setX(int x) {
          this.x = x;
       }
     
       public void setY(int y) {
          this.y = y;
       }
     
       public void setRadius(int radius) {
          this.radius = radius;
       }
     
       @Override
       public void draw() {
          System.out.println("Circle: Draw() [Color : " + color 
             +", x : " + x +", y :" + y +", radius :" + radius);
       }
    }
    public class ShapeFactory {
       private static final HashMap<String, Shape> circleMap = new HashMap<>();
     
       public static Shape getCircle(String color) {
          Circle circle = (Circle)circleMap.get(color);
     
          if(circle == null) {
             circle = new Circle(color);
             circleMap.put(color, circle);
             System.out.println("Creating circle of color : " + color);
          }
          return circle;
       }
    }
    
    public class FlyweightPatternDemo {
       private static final String colors[] = 
          { "Red", "Green", "Blue", "White", "Black" };
       public static void main(String[] args) {
     
          for(int i=0; i < 20; ++i) {
             Circle circle = 
                (Circle)ShapeFactory.getCircle(getRandomColor());
             circle.setX(getRandomX());
             circle.setY(getRandomY());
             circle.setRadius(100);
             circle.draw();
          }
       }
       private static String getRandomColor() {
          return colors[(int)(Math.random()*colors.length)];
       }
       private static int getRandomX() {
          return (int)(Math.random()*100 );
       }
       private static int getRandomY() {
          return (int)(Math.random()*100);
       }
    }
    

    五)代理模式(Proxy)

    定义:为其他对象提供一种代理以控制对这个对象的访问。
    适用:想在访问一个类时做一些控制。
    代码示例:

    public interface Image {
       void display();
    }
    
    public class RealImage implements Image {
     
       private String fileName;
     
       public RealImage(String fileName){
          this.fileName = fileName;
          loadFromDisk(fileName);
       }
     
       @Override
       public void display() {
          System.out.println("Displaying " + fileName);
       }
     
       private void loadFromDisk(String fileName){
          System.out.println("Loading " + fileName);
       }
    }
    
    public class ProxyImage implements Image{
     
       private RealImage realImage;
       private String fileName;
     
       public ProxyImage(String fileName){
          this.fileName = fileName;
       }
     
       @Override
       public void display() {
          if(realImage == null){
             realImage = new RealImage(fileName);
          }
          realImage.display();
       }
    }
    
    public class ProxyPatternDemo {
       
       public static void main(String[] args) {
          Image image = new ProxyImage("test_10mb.jpg");
     
          // 图像将从磁盘加载
          image.display(); 
          System.out.println("");
          // 图像不需要从磁盘加载
          image.display();  
       }
    }
    

    六)装饰器模式(Decorator)

    定义:动态的给对象添加一些额外的责任,就增加功能来说,装饰比生成子类更为灵活。
    适用:在不想增加很多子类的情况下扩展类。
    代码示例:

    public interface Shape {
       void draw();
    }
    
    public class Rectangle implements Shape {
     
       @Override
       public void draw() {
          System.out.println("Shape: Rectangle");
       }
    }
    
    public class Circle implements Shape {
     
       @Override
       public void draw() {
          System.out.println("Shape: Circle");
       }
    }
    
    public abstract class ShapeDecorator implements Shape {
       protected Shape decoratedShape;
     
       public ShapeDecorator(Shape decoratedShape){
          this.decoratedShape = decoratedShape;
       }
     
       public void draw(){
          decoratedShape.draw();
       }  
    }
    
    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");
       }
    }
    
    public class DecoratorPatternDemo {
       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();
       }
    }
    

    七)适配器模式(Adapter)

    定义:将一个类的接口转换成客户希望的另外一个接口,Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
    适用:
    1、系统需要使用现有的类,而此类的接口不符合系统的需要。
    2、想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。
    3、通过接口转换,将一个类插入另一个类系中。
    代码示例:

    public interface MediaPlayer {
       public void play(String audioType, String fileName);
    }
    
    public interface AdvancedMediaPlayer { 
       public void playVlc(String fileName);
       public void playMp4(String fileName);
    }
    
    public class VlcPlayer implements AdvancedMediaPlayer{
       @Override
       public void playVlc(String fileName) {
          System.out.println("Playing vlc file. Name: "+ fileName);      
       }
     
       @Override
       public void playMp4(String fileName) {
          //什么也不做
       }
    }
    
    public class Mp4Player implements AdvancedMediaPlayer{
     
       @Override
       public void playVlc(String fileName) {
          //什么也不做
       }
     
       @Override
       public void playMp4(String fileName) {
          System.out.println("Playing mp4 file. Name: "+ fileName);      
       }
    }
    
    public class MediaAdapter implements MediaPlayer {
     
       AdvancedMediaPlayer advancedMusicPlayer;
     
       public MediaAdapter(String audioType){
          if(audioType.equalsIgnoreCase("vlc") ){
             advancedMusicPlayer = new VlcPlayer();       
          } else if (audioType.equalsIgnoreCase("mp4")){
             advancedMusicPlayer = new Mp4Player();
          }  
       }
     
       @Override
       public void play(String audioType, String fileName) {
          if(audioType.equalsIgnoreCase("vlc")){
             advancedMusicPlayer.playVlc(fileName);
          }else if(audioType.equalsIgnoreCase("mp4")){
             advancedMusicPlayer.playMp4(fileName);
          }
       }
    }
    
    public class AudioPlayer implements MediaPlayer {
       MediaAdapter mediaAdapter; 
     
       @Override
       public void play(String audioType, String fileName) {    
     
          //播放 mp3 音乐文件的内置支持
          if(audioType.equalsIgnoreCase("mp3")){
             System.out.println("Playing mp3 file. Name: "+ fileName);         
          } 
          //mediaAdapter 提供了播放其他文件格式的支持
          else if(audioType.equalsIgnoreCase("vlc") 
             || audioType.equalsIgnoreCase("mp4")){
             mediaAdapter = new MediaAdapter(audioType);
             mediaAdapter.play(audioType, fileName);
          }
          else{
             System.out.println("Invalid media. "+
                audioType + " format not supported");
          }
       }   
    }
    
    public class AdapterPatternDemo {
       public static void main(String[] args) {
          AudioPlayer audioPlayer = new AudioPlayer();
     
          audioPlayer.play("mp3", "beyond the horizon.mp3");
          audioPlayer.play("mp4", "alone.mp4");
          audioPlayer.play("vlc", "far far away.vlc");
          audioPlayer.play("avi", "mind me.avi");
       }
    }
    

    相关文章

      网友评论

          本文标题:2019-04-11 软件设计师 设计模式

          本文链接:https://www.haomeiwen.com/subject/kqgziqtx.html