zoukankan      html  css  js  c++  java
  • 工厂模式


    简单工厂模式/静态工厂模式 >>> 对象创建模式 >>> 创建具有共同父类的类实例对象
    简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类

    public interface Cumputer{
        public void play(String movie);
    }
    public class HuaweiCumputer implements Cumputer{
        @Override
        public void play(String movie){
            System.out.println("正在使用华为电脑播放电影:"+movie);
        }
    }
    1 public class AppleCumputer implements Cumputer{
    2     @Override
    3     public void play(String movie){
    4         System.out.println("正在使用苹果电脑播放电影:"+movie);
    5     }
    6 }
    public class SimpleFactory{
        public static Cumputer factory(String cuputername){
            if(cuputername.equals("Huawei")){
                return new HuaweiCumputer();
            }else if(cuputername.equals("Apple")){
                return new AppleCumputer();
            }else{
                throw new RuntimeException("没有找到登陆类型");
            }
        }
    
        public static void main(String[]args){
            Cumputer a = SimpleFactory.factory("Apple");
            Cumputer h = SimpleFactory.factory("Huawei");
            a.play("流浪地球");
            h.play("三体");
        }
    }


    工厂方法模式/虚拟构造器模式/多态工厂模式 >>> 对象创建模式 >>> 创建具有共同父类的类实例对象

    工厂父类负责定义创建产品对象的公共接口,而工厂子类则负责生成具体的产品对象,

    将产品类的实例化操作延迟到工厂子类中完成,即通过工厂子类来确定究竟应该实例化哪一个具体产品类。

    public interface Cumputer{
        public void play_movie(String movie);
        public void play_music(String music);
    }
    public class Huawei implements Cumputer{
        public void play_movie(String movie){
            System.out.println("正在使用华为电脑播放电影: "+movie);
        }
        public void play_music(String music){
            System.out.println("正在使用华为电脑播放音乐: "+music);
        }
    }
    public class Apple implements Cumputer{
        public void play_movie(String movie){
            System.out.println("正在使用苹果电脑播放电影: "+movie);
        }
        public void play_music(String music){
            System.out.println("正在使用苹果电脑播放音乐: "+music);
        }
    }
    public interface Factory{
        public Cumputer CreateCuputer();
    }
    public class HuaweiFactory implements Factory{
        public Cumputer CreateCuputer(){
            return new Huawei();
        }
    }
    public class AppleFactory implements Factory{
        public Cumputer CreateCuputer(){
            return new Apple();
        }
    }
    public class FactoryMethod{
        public static void main(String[] args) {
            HuaweiFactory hf = new HuaweiFactory();
            Cumputer hc = hf.CreateCuputer();
            AppleFactory af = new AppleFactory();
            Cumputer ac = af.CreateCuputer();
            hc.play_movie("流浪地球");
            hc.play_music("Faded");
            ac.play_movie("熊出没");
            ac.play_music("平凡之路");
        }
    }


    抽象工厂模式 >>> 对象创建模式 >>> 创建具有共同父类的类实例对象
    提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类

    interface Apple{
        public void design();
    }
    interface Huawei{
        public void design();
    }
    
    class Iphone implements Apple{
        public void design(){
            System.out.println("苹果设计手机: Iphone");
        }
    }
    
    class Mac implements Apple{
        public void design(){
            System.out.println("苹果设计电脑: Mac");
        }
    }
    
    class Mate implements Huawei{
        public void design(){
            System.out.println("华为设计手机: Mate");
        }
    }
    
    class MateBook implements Huawei{
        public void design(){
            System.out.println("华为设计电脑: MateBook");
        }
    }
    interface Facotry{
        Apple manufactureAppleProduct();
        Huawei manufactureHuaweiProduct();
    }
    
    class CumputerFacotry implements Facotry{
        public Apple manufactureAppleProduct(){
            return new Mac();
        }
        public Huawei manufactureHuaweiProduct(){
            return new MateBook();
        }
    }
    
    class PhoneFactory implements Facotry{
        public Apple manufactureAppleProduct(){
            return new Iphone();
        }
        public Huawei manufactureHuaweiProduct(){
            return new Mate();
        }
    }
    public class AbstractFactory{
        public static void main(String[] args) {
            // 获取 Mate手机、Iphone手机
            Facotry facotry = new PhoneFactory();   //手机工厂
            Apple apple = facotry.manufactureAppleProduct();
            apple.design();
            Huawei huawei = facotry.manufactureHuaweiProduct();
            huawei.design();
    
            // 获取 MateBook、Mac
            Facotry facotry2 = new CumputerFacotry();
            Apple mac = facotry.manufactureAppleProduct();
            mac.design();
            Huawei matebook = facotry.manufactureHuaweiProduct();
            huawei.design();
        }
    }
  • 相关阅读:
    招聘 微软全球技术支持中心 sql server组
    retrofit2 上传图片
    android 6.0权限判断 音频 拍照 相册
    android studio 修改成自己jks(keystore)签名文件
    android 透明度颜色值
    android studio clone 失败
    fresco Bitmap too large to be uploaded into a texture
    android 虚线
    EventBus 3.0使用
    java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting
  • 原文地址:https://www.cnblogs.com/xiaofeifei-wang/p/10492841.html
Copyright © 2011-2022 走看看