美文网首页
知识就是力量

知识就是力量

作者: 呵鲲 | 来源:发表于2018-02-26 21:07 被阅读0次

    静态工厂方法模式

    // 二者共同的接口
    public interface Human {
        public void eat();
        public void sleep();
        public void beat();
    }
    
    // 创建实现类 Male
    public class Male implements Human {
        public void eat() {
            System.out.println("Male can eat."); 
        }
        public void sleep() {
            System.out.println("Male can sleep.");
        }
        public void beat() {
            System.out.println("Male can beat.");
        }
    } 
    
    // 创建实现类 Female
    public class Female implements Human {
        public void eat() {
            System.out.println("Female can eat."); 
        }
        public void sleep() {
            System.out.println("Female can sleep.");
        }
        public void beat() {
            System.out.println("Female can beat.");
        }
    } 
    
    // 多个工厂方法
    public class HumanFactory {
        public static Male createMale() {
            return new Male();
        }
        public static Female createFemale() {
            return new Female();
        }
    }
    
    // 工厂测试类
    public class FactoryTest {
        public static void main(String[] args) {
            Human male = HumanFactory.createMale();
            male.eat();
            male.sleep();
            male.beat();
        }
    }
    

    抽象工厂模式

    // 抽象食物
    interface Food {
        public String getFoodName();
    }
    
    // 抽象餐具
    interface TableWare {
        public String getToolName();
    }
    
    // 抽象工厂
    interface KitchenFactory {
        public Food getFood();
        public TableWare getTableWare();
    }
    
    // 具体食物 Apple 的定义如下
    class Apple implements Food {
        public String getFoodName() {
            return "apple";
        }
    }
    
    // 具体餐具 Knife 的定义如下
    class Knife implements TableWare { 
        public String getToolName() {
            return "knife";
        }
    }
    
    // 以具体工厂 AKitchen 为例
    class AKitchen implements KitchenFactory {
        public Food getFood() {
           return new Apple();
        }
    
        public TableWare getTableWare() {
           return new Knife();
        }
    }
    
    // 吃货要开吃了
    public class Foodaholic {
        public void eat(KitchenFactory k) {
           System.out.println("A foodaholic is eating "+ k.getFood().getFoodName()
                  + " with " + k.getTableWare().getToolName() );
        }
    
        public static void main(String[] args) {
           Foodaholic fh = new Foodaholic();
           KitchenFactory kf = new AKitchen();
           fh.eat(kf);
        }
    }
    

    适配器模式

    // 国标插头
    public interface CnPluginInterface {
        void chargeWith2Pins();
    }
    
    // 实现国标插座的充电方法
    public class CnPlugin implements CnPluginInterface {
        public void chargeWith2Pins() {
            System.out.println("charge with CnPlugin");
        }
    }
    
    // 在国家中内充电
    public class Home {
        private CnPluginInterface cnPlugin;
    
        public Home() { }
    
        public void setPlugin(CnPluginInterface cnPlugin) {
            this.cnPlugin = cnPlugin;
        }
    
        // 充电
        public void charge() {
            // 国标充电
            cnPlugin.chargeWith2Pins();
        }
    }
    
    // 英标插头
    public interface EnPluginInterface {
        void chargeWith3Pins();
    }
    
    // 实现英标插座的充电方法
    public class EnPlugin implements EnPluginInterface {
        public void chargeWith3Pins() {
            System.out.println("charge with EnPlugin");
        }
    }
    
    // 适配器
    public class PluginAdapter implements CnPluginInterface {
        private EnPluginInterface enPlugin;
    
        public PluginAdapter(EnPluginInterface enPlugin) {
             this.enPlugin = enPlugin;
        }
    
        // 这是重点,适配器实现了英标的插头,然后重载国标的充电方法为英标的方法
        @Override
        public void chargeWith2Pins() {
            enPlugin.chargeWith3Pins();
        }
    }
    
    // 适配器测试类
    public class AdapterTest {
        public static void main(String[] args) {
            EnPluginInterface enPlugin = new EnPlugin();
            Home home = new Home();
            PluginAdapter pluginAdapter = new PluginAdapter(enPlugin);
            home.setPlugin(pluginAdapter);
            // 会输出 “charge with EnPlugin”
            home.charge();
        }
    }
    

    装饰者模式

    // 抽象类 Girl
    public abstract class Girl {
        String description = "no particular";
    
        public String getDescription(){
            return description;
        }
    }
    
    // 美国女孩
    public class AmericanGirl extends Girl {
        public AmericanGirl() {
            description = "+AmericanGirl";
        }
    }
    
    // 国产妹子
    public class ChineseGirl extends Girl {
        public ChineseGirl() {
            description = "+ChineseGirl";
        }
    }
    
    // 装饰者
    public abstract class GirlDecorator extends Girl {
        public abstract String getDescription();
    }
    
    // 下面以美国女孩示例
    // 给美国女孩加上金发
    public class GoldenHair extends GirlDecorator {
    
        private Girl girl;
    
        public GoldenHair(Girl g) {
            girl = g;
        }
    
        @Override
        public String getDescription() {
            return girl.getDescription() + "+with golden hair";
        }
    
    }
    
    // 加上身材高大的特性
    public class Tall extends GirlDecorator {
    
        private Girl girl;
    
        public Tall(Girl g) {
            girl = g;
        }
    
        @Override
        public String getDescription() {
            return girl.getDescription() + "+is very tall";
        }
    
    }
    
    
    // 检验一下
    public class Test {
    
        public static void main(String[] args) {
            Girl g1 = new AmericanGirl();
            System.out.println(g1.getDescription());
    
            GoldenHair g2 = new GoldenHair(g1);
            System.out.println(g2.getDescription());
    
            Tall g3 = new Tall(g2);
            System.out.println(g3.getDescription());
    
            // 你也可以一步到位
            // Girl g = new Tall(new GoldenHair(new AmericanGirl())); 
        }
    }
    

    观察者模式

    // Subject 主题接口
    public interface Subject {
        public void registerObserver(Observer o);
        public void removeObserver(Observer o);
        public void notifyAllObservers();
    }
    
    // 观察者接口
    public interface Observer {
        public void update(Subject s);
    }
    
    // 视频网站某狐 实现 Subject 接口
    public class VideoSite implements Subject{
    
        // 观察者列表 以及 更新了的视频列表
        private ArrayList<Observer> userList;
        private ArrayList<String> videos;
    
        public VideoSite(){
            userList = new ArrayList<Observer>();
            videos = new ArrayList<String>();
        }
    
        @Override
        public void registerObserver(Observer o) {
            userList.add(o);
        }
    
        @Override
        public void removeObserver(Observer o) {
            userList.remove(o);
        }
    
        @Override
        public void notifyAllObservers() {
            for (Observer o: userList) {
                o.update(this);
            }
        }
    
        public void addVideos(String video) {
            this.videos.add(video);
            notifyAllObservers();
        }
    
        public ArrayList<String> getVideos() {
            return videos;
        }
    
        public String toString(){
            return videos.toString();
        }
    }
    
    // 实现观察者,即看视频的美剧迷们
    public class VideoFans implements Observer {
    
        private String name;
    
        public VideoFans(String name){
            this.name = name;
        }
        @Override
        public void update(Subject s) {
            System.out.println(this.name + ", new videos are available! ");
            // print video list
            System.out.println(s);
        }
    }
    
    //  测试一下
    public class Main {
    
        public static void main(String[] args) {
            VideoSite vs = new VideoSite();
            vs.registerObserver(new VideoFans("LiLei"));
            vs.registerObserver(new VideoFans("HanMeimei"));
            vs.registerObserver(new VideoFans("XiaoMing"));
    
            // add videos
            vs.addVideos("Video 1");
            //vs.addVideos("Video 2");
        }
    }
    

    单例模式

    // 静态内部类
    public class Wife {
        private static class WifeHolder {
            private static final Wife wife = new Wife();
        }
    
        private Wife() { }
    
        public static Wife getWife() {
            return WifeHolder.wife;
        }
    }
    

    相关文章

      网友评论

          本文标题:知识就是力量

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