Java设计模式之简单工厂模式

Java设计模式之简单工厂模式

    • 简单工厂模式

      使用参数或者配置文件等事先定义好的变量,然后利用分支判断初始化具体产品类并返回;
      不符合“开发-封闭”原则,每次增加产品,都需要修改类方法。工厂类单一,不用维护大量的工厂类;
      工厂方法模式抽象工厂模式规避了不符合“开发-封闭”原则,同时支持更灵活的增加产品;

    • 适用性

      1)当一个类不知道它所必须创建的对象的类的时候。
      2)当一个类希望由它的子类来指定它所创建的对象的时候。

    • 举例

      如哈佛汽车加工厂既可以生产跑车又可以生产卡车。

    • UML类图
      Java设计模式之简单工厂模式

    • 代码示例

      • 定义哈佛汽车接口(或抽象类)
        package com.jin.demo.DesignPatterns.Factory.SimpleFactory;
        
        /**
         * 定义哈佛汽车接口(或抽象类)
         * @auther jinsx
         * @date 2019-05-15 08:58
         */
        public interface HFCar {
        	// 什么汽车都可以跑
        	void run();
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
      • 哈佛跑车
        package com.jin.demo.DesignPatterns.Factory.SimpleFactory;
        
        /**
         * 哈佛跑车
         * @auther jinsx
         * @date 2019-05-15 09:00
         */
        public class HFRoadster implements HFCar{
        	@Override
        	public void run() {
        		System.out.println("哈佛小跑车,起步就是快");
        	}
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
      • 哈佛卡车
        package com.jin.demo.DesignPatterns.Factory.SimpleFactory;
        
        /**
         * 哈佛卡车
         * @auther jinsx
         * @date 2019-05-15 09:03
         */
        public class HFTruck implements HFCar {
        	@Override
        	public void run() {
        		System.out.println("哈佛小卡车,运输能力就是强");
        	}
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
      • 声明简单工厂,有一个返回Car对象的静态类
        package com.jin.demo.DesignPatterns.Factory.SimpleFactory;
        
        /**
         * 简单工厂
         * @auther jinsx
         * @date 2019-05-15 09:06
         */
        public class CarSimpleFactory {
        	// 需要使用定义好的参数获取产品对象,没增加产品都需要修改次工厂方法
        	public static HFCar createHFCar(String type){
        		HFCar car = null;
        		switch (type){
        			case "roadster" :
        				car = new HFRoadster();
        				break;
        			case "truck" :
        				car = new HFTruck();
        				break;
        		}
        		return car;
        	}
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
      • 测试类https://dribbble.com/sanaslatvmcb/collections/4201092
        https://dribbble.com/nesmaoxclul/collections/4201094
        https://dribbble.com/sanaslatvmcb/collections/4201098
        https://dribbble.com/nesmaoxclul/collections/4201100
        https://dribbble.com/sanaslatvmcb/collections/4201103
        https://dribbble.com/nesmaoxclul/collections/4201105
        https://dribbble.com/sanaslatvmcb/collections/4201109
        https://dribbble.com/nesmaoxclul/collections/4201110
        https://dribbble.com/nesmaoxclul/collections/4201115
        https://dribbble.com/sanaslatvmcb/collections/4201117
        https://dribbble.com/nesmaoxclul/collections/4201119
        https://dribbble.com/sanaslatvmcb/collections/4201120
        https://dribbble.com/sanaslatvmcb/collections/4201123
        https://dribbble.com/nesmaoxclul/collections/4201124
        https://dribbble.com/sanaslatvmcb/collections/4201128
        https://dribbble.com/nesmaoxclul/collections/4201130
        https://dribbble.com/sanaslatvmcb/collections/4201132
        https://dribbble.com/nesmaoxclul/collections/4201134
        https://dribbble.com/sanaslatvmcb/collections/4201136
        https://dribbble.com/nesmaoxclul/collections/4201138
        https://dribbble.com/sanaslatvmcb/collections/4201141
        https://dribbble.com/nesmaoxclul/collections/4201142
        https://dribbble.com/sanaslatvmcb/collections/4201144
        https://dribbble.com/nesmaoxclul/collections/4201145
        https://dribbble.com/sanaslatvmcb/collections/4201150
        https://dribbble.com/nesmaoxclul/collections/4201151
        https://dribbble.com/sanaslatvmcb/collections/4201154
        https://dribbble.com/nesmaoxclul/collections/4201155
        https://dribbble.com/sanaslatvmcb/collections/4201158
        https://dribbble.com/nesmaoxclul/collections/4201160
        https://dribbble.com/sanaslatvmcb/collections/4201164
        https://dribbble.com/nesmaoxclul/collections/4201165
        https://dribbble.com/sanaslatvmcb/collections/4201167
        https://dribbble.com/nesmaoxclul/collections/4201168
        https://dribbble.com/sanaslatvmcb/collections/4201171
        https://dribbble.com/nesmaoxclul/collections/4201174
        https://dribbble.com/sanaslatvmcb/collections/4201176
        https://dribbble.com/nesmaoxclul/collections/4201179
        https://dribbble.com/sanaslatvmcb/collections/4201183
        https://dribbble.com/nesmaoxclul/collections/4201184
        https://dribbble.com/ratheuapo/collections/4201189
        https://dribbble.com/nanoughhsdfb/collections/4201191
        https://dribbble.com/ratheuapo/collections/4201193
        https://dribbble.com/nanoughhsdfb/collections/4201197
        https://dribbble.com/ratheuapo/collections/4201200
        https://dribbble.com/nanoughhsdfb/collections/4201203
        https://dribbble.com/ratheuapo/collections/4201204
        https://dribbble.com/nanoughhsdfb/collections/4201207
        https://dribbble.com/ratheuapo/collections/4201209
        https://dribbble.com/nanoughhsdfb/collections/4201211
        https://dribbble.com/ratheuapo/collections/4201213
        https://dribbble.com/nanoughhsdfb/collections/4201216
        https://dribbble.com/ratheuapo/collections/4201218
        https://dribbble.com/nanoughhsdfb/collections/4201220
        https://dribbble.com/ratheuapo/collections/4201221
        https://dribbble.com/nanoughhsdfb/collections/4201222
        https://dribbble.com/ratheuapo/collections/4201225
        https://dribbble.com/nanoughhsdfb/collections/4201228
        https://dribbble.com/ratheuapo/collections/4201230
        https://dribbble.com/nanoughhsdfb/collections/4201232
        https://dribbble.com/ratheuapo/collections/4201235
        https://dribbble.com/nanoughhsdfb/collections/4201237
        https://dribbble.com/ratheuapo/collections/4201239
        https://dribbble.com/nanoughhsdfb/collections/4201242
        https://dribbble.com/ratheuapo/collections/4201243
        https://dribbble.com/nanoughhsdfb/collections/4201247
        https://dribbble.com/ratheuapo/collections/4201250
        https://dribbble.com/nanoughhsdfb/collections/4201252
        https://dribbble.com/ratheuapo/collections/4201254
        https://dribbble.com/nanoughhsdfb/collections/4201257
        https://dribbble.com/ratheuapo/collections/4201258
        https://dribbble.com/nanoughhsdfb/collections/4201261
        https://dribbble.com/ratheuapo/collections/4201265
        https://dribbble.com/nanoughhsdfb/collections/4201267
        https://dribbble.com/ratheuapo/collections/4201271
        https://dribbble.com/nanoughhsdfb/collections/4201273
        https://dribbble.com/ratheuapo/collections/4201275
        https://dribbble.com/nanoughhsdfb/collections/4201278
        https://dribbble.com/ratheuapo/collections/4201282
        https://dribbble.com/nanoughhsdfb/collections/4201283
        https://dribbble.com/teteighprcjwh/collections/4201286
        https://dribbble.com/shirebvxpbn/collections/4201287
        https://dribbble.com/teteighprcjwh/collections/4201290
        https://dribbble.com/shirebvxpbn/collections/4201291
        https://dribbble.com/teteighprcjwh/collections/4201294
        https://dribbble.com/shirebvxpbn/collections/4201295
        https://dribbble.com/teteighprcjwh/collections/4201299
        https://dribbble.com/shirebvxpbn/collections/4201301
        https://dribbble.com/teteighprcjwh/collections/4201304
        https://dribbble.com/shirebvxpbn/collections/4201306
        https://dribbble.com/teteighprcjwh/collections/4201310
        https://dribbble.com/shirebvxpbn/collections/4201314
        https://dribbble.com/teteighprcjwh/collections/4201316
        https://dribbble.com/shirebvxpbn/collections/4201318
        https://dribbble.com/teteighprcjwh/collections/4201321
        https://dribbble.com/shirebvxpbn/collections/4201323
        https://dribbble.com/teteighprcjwh/collections/4201324
        https://dribbble.com/shirebvxpbn/collections/4201325
        https://dribbble.com/teteighprcjwh/collections/4201327
        https://dribbble.com/shirebvxpbn/collections/4201330
        https://dribbble.com/teteighprcjwh/collections/4201333
        https://dribbble.com/shirebvxpbn/collections/4201335
        https://dribbble.com/teteighprcjwh/collections/4201339
        https://dribbble.com/shirebvxpbn/collections/4201341
        https://dribbble.com/teteighprcjwh/collections/4201343
        https://dribbble.com/shirebvxpbn/collections/4201344
        https://dribbble.com/teteighprcjwh/collections/4201348
        https://dribbble.com/shirebvxpbn/collections/4201349
        https://dribbble.com/teteighprcjwh/collections/4201352
        https://dribbble.com/shirebvxpbn/collections/4201354
        https://dribbble.com/teteighprcjwh/collections/4201356
        https://dribbble.com/shirebvxpbn/collections/4201357
        https://dribbble.com/teteighprcjwh/collections/4201359
        https://dribbble.com/shirebvxpbn/collections/4201360
        https://dribbble.com/teteighprcjwh/collections/4201364
        https://dribbble.com/shirebvxpbn/collections/4201365
        https://dribbble.com/teteighprcjwh/collections/4201368
        https://dribbble.com/shirebvxpbn/collections/4201371
        https://dribbble.com/teteighprcjwh/collections/4201373
        https://dribbble.com/shirebvxpbn/collections/4201375
        https://dribbble.com/shisatmikv/collections/4201381
        https://dribbble.com/shisatmikv/collections/4201384
        https://dribbble.com/shisatmikv/collections/4201388
        https://dribbble.com/shisatmikv/collections/4201393
        https://dribbble.com/shisatmikv/collections/4201398
        https://dribbble.com/shisatmikv/collections/4201404
        https://dribbble.com/shisatmikv/collections/4201408
        https://dribbble.com/shisatmikv/collections/4201415
        https://dribbble.com/shisatmikv/collections/4201417
        https://dribbble.com/shisatmikv/collections/4201418
        https://dribbble.com/shisatmikv/collections/4201421
        https://dribbble.com/shisatmikv/collections/4201424
        https://dribbble.com/shisatmikv/collections/4201428
        https://dribbble.com/shisatmikv/collections/4201432
        https://dribbble.com/shisatmikv/collections/4201433
        https://dribbble.com/shisatmikv/collections/4201436
        https://dribbble.com/ceeteanbkcxeb/collections/4201438
        https://dribbble.com/shisatmikv/collections/4201439
        https://dribbble.com/ceeteanbkcxeb/collections/4201443
        https://dribbble.com/shisatmikv/collections/4201444
        https://dribbble.com/ceeteanbkcxeb/collections/4201446
        https://dribbble.com/shisatmikv/collections/4201447
        https://dribbble.com/ceeteanbkcxeb/collections/4201448
        https://dribbble.com/ceeteanbkcxeb/collections/4201451
        https://dribbble.com/ceeteanbkcxeb/collections/4201454
        https://dribbble.com/ceeteanbkcxeb/collections/4201457
        package com.jin.demo.DesignPatterns.Factory.SimpleFactory;
        
        /**
         * 测试类
         * @auther jinsx
         * @date 2019-05-15 09:15
         */
        public class TestSimpleFactory {
        	public static void main(String[] args) {
        		// 来一个哈佛小跑
        		HFCar roadster = CarSimpleFactory.createHFCar("roadster");
        		roadster.run();
        		// 来一个哈佛小卡
        		HFCar truck = CarSimpleFactory.createHFCar("truck");
        		truck.run();
        	}
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
上一篇:paddlepaddle 报错:DeprecationWarning: Using or importing the ABCs from ‘collections‘ instead of from ‘


下一篇:c#-特殊的集合-BitArray-bitArray.Not()-简单实验