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

    介绍

    工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

    在工厂模式中,我们在创建对象时无需对客户端暴露创建的逻辑,只需一个公共的接口便可指向创建的对象

    意图

    定义一个创建对象的接口,让其子类决定实例化哪一个工厂类

    何时使用

    明确计划在不同的条件创建不同的实例

    如何解决

    让子类实现工厂接口,返回也是一个抽象的产品

    关键代码

    创建过程在其子类中执行

    实例

    需要一辆车,直接从工厂里去取货,不许关这些车辆是怎么做出来的

    设计一个连接服务器的框架,采用三种协议,POP3,IMAP,HTTP,可以把三个座位产品类,共同实现一个借口

    使用场景

    作为创建类的方式,在任何需要申城复杂对象的地方都可以使用工厂类。

    注,在实现复杂类的时候适合使用工厂类,而对于简单的类,new即可,无需用工厂类。如果使用工厂类,就会引入一个工厂类,增加了系统的复杂性。

    1. 简单工厂模式

    Shape.java

    public interface Shape {
        void draw();
    }

    Circle.java

    public class Circle implements Shape{
        public void draw(){
            System.out.println("Inside Circle draw()");
        }
    }

    Rectangle.java

    public class Rectangle implements Shape{
        public void draw() {
            System.out.println("Inside Rectangle draw");
        }
    }

    Square.java

    public class Square implements Shape{
        public void draw() {
            System.out.println("Inside Square draw");
        }
    }

    Shapefactory.java

    public class Shapefactory {
        public Shape getShape(String shapename){
            if(shapename == null){
                return null;
            }
            if(shapename.equals("CIRCLE")){
                return new Circle();
            }
            else if(shapename.equals("RECTANGLE")){
                return new Rectangle();
            }
            else if(shapename.equals("SQUARE")){
                return new Square();
            }
            return null;
        }
    }

    FactoryPatternDemo

    public class FactoryPatternDemo {
        public static void main(String[] args){
            Shapefactory shapefactory = new Shapefactory();
    
            Shape shape_1 = shapefactory.getShape("RECTANGLE");
            shape_1.draw();
    
            Shape shape_2 = shapefactory.getShape("CIRCLE");
            shape_2.draw();
    
            Shape shape_3 = shapefactory.getShape("SQUARE");
            shape_3.draw();
    
            Shape shape_4 = shapefactory.getShape("");
            System.out.println(shape_4);
        }
    }

    结果

    Inside Rectangle draw
    Inside Circle draw()
    Inside Square draw
    null
    

    2. 抽象方法

    public interface Noodles {
        void taste();
    }
    public class LanzhouNoodles implements Noodles {
        @Override
        public void taste() {
            System.out.println("兰州拉面劲道、有味");
    
        }
    }
    public class ChaoxianNoodles implements Noodles {
        @Override
        public void taste() {
            System.out.println("朝鲜面酸爽,好吃");
        }
    }
    public interface NoodlesFactory {
        Noodles createNoodles();
    }
    public class LanzhouFactory implements NoodlesFactory{
    
        @Override
        public Noodles createNoodles() {
            return new LanzhouNoodles();
        }
    }
    public class ChaoxianFatory implements NoodlesFactory{
        @Override
        public Noodles createNoodles() {
            return new ChaoxianNoodles();
        }
    }

    测试

    public class FactoryTest {
        public static void main(String[] args) {
            NoodlesFactory factory = new ChaoxianFatory();
            Noodles noodles = factory.createNoodles();
            noodles.taste();
    
            System.out.println("-------------");
    
            NoodlesFactory factory1 = new LanzhouFactory();
            Noodles noodles1 = factory1.createNoodles();
            noodles1.taste();
        }
    }

    结果

    朝鲜面酸爽,好吃
    -------------
    兰州拉面劲道、有味
    

    3. 多对象

    public interface Drink {
        void feel();
    }
    public class CokeDrink implements Drink{
        @Override
        public void feel() {
            System.out.println("可乐真可口");
        }
    }
    public class Walter implements Drink{
        @Override
        public void feel() {
            System.out.println("白开水贼解渴");
        }
    }
    public interface FoodFactory {
        Noodles createNoodles();
        Drink createDrink();
    }
    public class LanzhouFactory implements FoodFactory {
    
        @Override
        public Noodles createNoodles() {
            return new LanzhouNoodles();
        }
    
        @Override
        public Drink createDrink() {
            return new Walter();
        }
    }
    public class ChaoxianFatory implements FoodFactory {
        @Override
        public Noodles createNoodles() {
            return new ChaoxianNoodles();
        }
    
        @Override
        public Drink createDrink() {
            return new CokeDrink();
        }
    }

    测试

    public class FactoryTest {
        public static void main(String[] args) {
            FoodFactory factory = new ChaoxianFatory();
            Noodles noodles = factory.createNoodles();
            noodles.taste();
            Drink drink = factory.createDrink();
            drink.feel();
    
            System.out.println("-------------");
    
            FoodFactory factory1 = new LanzhouFactory();
            Noodles noodles1 = factory1.createNoodles();
            noodles1.taste();
            Drink drink1 = factory1.createDrink();
            drink1.feel();
        }
    }

    结果

    朝鲜面酸爽,好吃
    可乐真可口
    -------------
    兰州拉面劲道、有味
    白开水贼解渴

      

  • 相关阅读:
    REDELK的安装和使用
    Palo Alto GlobalProtect上的PreAuth RCE
    渗透 Facebook 的思路与发现
    抓取腾讯视频MP4文件
    JS中整数的取整、取余、向上取整
    centos7安装docker
    业界难题-“跨库分页”的四种方案(转)
    centos7设置时间
    简单实现Shiro单点登录(自定义Token令牌)
    nginx 反向代理时丢失端口的解决方案(转)
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/6483913.html
Copyright © 2011-2022 走看看