zoukankan      html  css  js  c++  java
  • 接口之工厂方法的设计模式

    1 定义一个用于创建对象的接口,让子类决定实例化哪一个类,FactoryMethod使一个类的实例化延迟到子类

    package p1;
    
    public class TestFactory {
    
        public static void main(String[] args) {
            IWorkFactory i = new TeacherWorkFactory();
            i.getWork().doWork();     //Teacher is working
    
            IWorkFactory i2 = new StudentWorkFactory();
            i2.getWork().doWork();    //Student is working
        }
    
    }
    
    interface IWorkFactory {
        public abstract Work getWork();
    }
    
    class TeacherWorkFactory implements IWorkFactory {
    
        @Override
        public Work getWork() {
            return new TeacherWork();
        }
    }
    
    class StudentWorkFactory implements IWorkFactory {
    
        @Override
        public Work getWork() {
            return new StudentWork();
        }
    }
    
    interface Work {
        public abstract void doWork();
    }
    
    class TeacherWork implements Work {
        public void doWork() {
            System.out.println("Teacher is working");
        }
    }
    
    class StudentWork implements Work {
        public void doWork() {
            System.out.println("Student is working");
        }
    }
  • 相关阅读:
    eclipse 快捷键
    JSTL标签 参考手册
    Oracle错误代码大全
    十大编程算法
    win激活查询及修改
    LINUX安全设置
    Mac OS Ruby安装 使用RVM
    windows 说“我爱你”
    ubuntu安装
    linux下文件压缩与解压操作
  • 原文地址:https://www.cnblogs.com/yjtm53/p/4138917.html
Copyright © 2011-2022 走看看