Add Simple Factory Pattern implementation
Java source code demonstrate simple factory design pattern
This commit is contained in:
		
							
								
								
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							| @@ -195,6 +195,7 @@ | ||||
|         <module>arrange-act-assert</module> | ||||
|         <module>transaction-script</module> | ||||
|         <module>filterer</module> | ||||
|         <module>simple-factory</module> | ||||
|     </modules> | ||||
|  | ||||
|     <repositories> | ||||
|   | ||||
| @@ -0,0 +1,16 @@ | ||||
| package com.iluwatar.simplefactory; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| public class App { | ||||
| 	 | ||||
| 	private static final Logger LOGGER = LoggerFactory.getLogger(App.class); | ||||
|  | ||||
| 	public static void main(String[] args) { | ||||
| 		Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.carTypes.FORD); | ||||
| 		Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.carTypes.FERRARI); | ||||
| 		LOGGER.info(car1.getDescription()); | ||||
| 		LOGGER.info(car2.getDescription()); | ||||
| 	} | ||||
| } | ||||
| @@ -0,0 +1,10 @@ | ||||
| package com.iluwatar.simplefactory; | ||||
|  | ||||
| /** | ||||
|  * Car interface | ||||
|  */ | ||||
| public interface Car { | ||||
|  | ||||
| 	public String getDescription(); | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,29 @@ | ||||
| package com.iluwatar.simplefactory; | ||||
|  | ||||
|  | ||||
| /** | ||||
|  * Factory of cars | ||||
|  */ | ||||
| public class CarSimpleFactory { | ||||
| 	 | ||||
| 	/* | ||||
| 	 * Enumeration for different types of cars | ||||
| 	 */ | ||||
| 	static enum carTypes { | ||||
| 		FORD, FERRARI | ||||
| 	}; | ||||
| 	 | ||||
| 	/* | ||||
| 	 * Factory method takes as parameter a car type and initiate the appropriate class | ||||
| 	 */ | ||||
| 	public static Car getCar(carTypes type) { | ||||
| 		switch (type) { | ||||
| 		case FORD: | ||||
| 			return new Ford(); | ||||
| 		case FERRARI: | ||||
| 			return new Ferrari(); | ||||
| 		default: | ||||
| 			throw new IllegalArgumentException("Model not supported."); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| package com.iluwatar.simplefactory; | ||||
|  | ||||
| /** | ||||
|  * Ferrari implementation | ||||
|  */ | ||||
| public class Ferrari implements Car { | ||||
| 	 | ||||
| 	static final String DESCRIPTION = "This is Ferrari."; | ||||
| 	 | ||||
| 	@Override | ||||
| 	public String getDescription() { | ||||
| 		return DESCRIPTION; | ||||
| 	} | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| package com.iluwatar.simplefactory; | ||||
|  | ||||
| /** | ||||
|  * Ford implementation | ||||
|  */ | ||||
| public class Ford implements Car { | ||||
|  | ||||
| 	static final String DESCRIPTION = "This is Ford."; | ||||
|  | ||||
| 	@Override | ||||
| 	public String getDescription() { | ||||
| 		return DESCRIPTION; | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user