Simple factory

A simple factory is really simple. If we have N classes that need to be managed by the factory, we assign a unique identity to each of the N classes. We call the factory method and pass the identity. The downside of this approach is obvious, poor scalability! Serious violation of open and close principle. If M new classes need to be managed by the factory, then the factory class code needs to be changed.

package simplefactory_k; /* * @auther @ @ @mail [email protected] * @date 2020-01-15 10:45 * @notify * @version 1.0 */ public class Airplane { }Copy the code

View Code

package simplefactory_k; /* * @auther @ @ @mail [email protected] * @date 2020-01-15 10:46 * @notify * @version 1.0 */ public class Rocket { }Copy the code

View Code

package simplefactory_k; /* * @auther @ @ @mail [email protected] * @date 2020-01-15 10:46 * @notify * @version 1.0 */ public class Screw { }Copy the code

View Code

package simplefactory_k; /* * @auther @ @ @mail [email protected] * @date 2020-01-15 10:46 * @notify * @version 1.0 */ public class SimpleFactoryK {public Static Object getObject(String obj) {if (obj. Equals (" Airplane ")) {return new Airplane(); } else if (obj. Equals (" Rocket ")) {return new Rocket(); } else if (obj. Equals (" Screw ")) {return new Screw(); } else { return null; }}}Copy the code

View Code

package simplefactory_k; /* * @auther @ @ @mail [email protected] * @date 2020-01-15 10:53 * @notify * @version 1.0 */ public class Main {public static void Main (String[] args) {Object airplane = Simplefactoryk.getobject (" airplane "); System.out.println(airplane instanceof Airplane); Object rocket = Simplefactoryk. getObject(" rocket "); System.out.println(rocket instanceof Rocket); Object screw = Simplefactoryk. getObject(" screw "); System.out.println(screw instanceof Screw); }}Copy the code

View Code

The factory method

The factory approach compensates for the lack of extensibility of simple factories by defining an interface for a product family and defining specific product implementations below the product family. (vertical product family, interface and implementation classes, or abstract classes and subclasses) create product family factory interfaces, different product factories create different products. Apples and bananas are both fruits. Apple factories produce apples, banana factories produce bananas, and they belong to fruit factories. When we need to add another fruit, like an orange, we need to create an orange class, and an orange factory, which sounds like a lot more trouble. But let’s assume that creating a class takes a lot of arguments, and creating the whole thing takes a lot of code. Then creating a factory class is absolutely necessary.

package factorymethod_k; /* * @auther @@mail [email protected] * @date 2020-01-15 14:22 * @notify * @version 1.0 */ public interface  Fruiter { void product(); }Copy the code

View Code

package factorymethod_k; /* * @auther @ @ @mail [email protected] * @date 2020-01-15 14:24 * @notify * @version 1.0 */ public class Apple implements Fruiter {@override public void product() {system.out.println (" implements Fruiter "); }}Copy the code

View Code

package factorymethod_k; /* * @auther @ @ @mail [email protected] * @date 2020-01-15 14:24 * @notify * @version 1.0 */ public class Banana implements Fruiter {@override public void product() {system.out.println (" Banana implements Fruiter "); }}Copy the code

View Code

package factorymethod_k; /* * @auther @@mail [email protected] * @date 2020-01-15 14:25 * @notify * @version 1.0 */ public interface  Farm { Fruiter getBean(); }Copy the code

View Code

package factorymethod_k; /* * @auther * @mail [email protected] * @date 2020-01-15 14:26 * @notify * @version 1.0 */ import factoryMethod.Fruit; public class AppleFarm implements Farm{ @Override public Fruiter getBean() { return new Apple(); }}Copy the code

View Code

package factorymethod_k; /* * @auther @ @ @mail [email protected] * @date 2020-01-15 14:26 * @notify * @version 1.0 */ public class BananaFarm implements Farm{ @Override public Fruiter getBean() { return new Banana(); }}Copy the code

View Code

package factorymethod_k; /* * @auther @ @ @mail [email protected] * @date 2020-01-15 14:29 * @notify * @version 1.0 */ public class Client { public static void main(String[] args) { Farm farm = new AppleFarm(); Fruiter fruiter = farm.getBean(); Farm farm2 = new BananaFarm(); Fruiter fruiter2 = farm2.getBean(); System.out.println(fruiter instanceof Apple); System.out.println(fruiter2 instanceof Banana); }}Copy the code

View Code

The abstract factory

The factory method is fine for a single product family, but not for a productive family. Think of a scenario where you have a fruit tribe and a vegetable tribe. Fruits and vegetables are grown in tropical environments and at room temperature. What do we do if we use the factory method? Create a fruit interface, create a fruit factory interface, create a vegetable interface, create a vegetable factory interface. But this is clearly against practical logic, tropical fruit and warm fruit certainly can not be created in the same factory. So are tropical and ambient vegetables. So we need to do horizontal extraction. Tropical fruits, room temperature fruits are fruits, tropical vegetables, room temperature vegetables are vegetables. We do extraction at the gardener interface, the gardeners who manage normal temperature crops and the gardeners who manage tropical crops. It is now possible to create two groups of products from a single farm.

package abstractFactory; /* * @auther junior * @mail [email protected] * @date 2019-07-24 10:55 * @notify Vegetable interface * @version 1.0 */ public interface Veggie { }Copy the code

View Code

package abstractFactory; /* * @auther junior * @mail [email protected] * @date 2019-07-24 10:58 * @notify Tropical vegetables * @version 1.0 */ import com.sun.org.apache.regexp.internal.RE; public class TropicalVeggie implements Veggie { private String name; public TropicalVeggie(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}Copy the code

View Code

package abstractFactory; /* * @auther young * @mail [email protected] * @date 2019-07-24 10:56 * @notify Northern vegetables * @version 1.0 */ public class NorthernVeggie implements Veggie { private String name; public NorthernVeggie(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}Copy the code

View Code

package abstractFactory; /* * @auther junior * @mail [email protected] * @date 2019-07-24 11:00 * @notify fruit interface * @version 1.0 */ public interface Fruit { }Copy the code

View Code

package abstractFactory; /* * @auther junior * @mail [email protected] * @date 2019-07-24 11:00 * @notify Northern fruit * @version 1.0 */ public class NorthernFruit implements Fruit { private String name; public NorthernFruit(String name) { this.name = name; } public String getName(){ return name; } public void setName(String name){ this.name = name; }}Copy the code

View Code

package abstractFactory; /* * @auther young * @mail [email protected] * @date 2019-07-24 11:02 * @notify Tropical fruit * @version 1.0 */ public class TropicalFruit implements Fruit { private String name; public TropicalFruit(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}Copy the code

View Code

package abstractFactory; /* * @auther youth * @mail [email protected] * @date 2019-07-24 10:50 * @notify Gardeners abstract class * @version 1.0 */ public interface Gardener { }Copy the code

View Code

package abstractFactory; /* * @auther youth * @mail [email protected] * @date 2019-07-24 10:50 * @notify Management of northern farm gardener * @version 1.0 */ public Class NorthernGardener Gardener {public Fruit createFruit(String name) {return new NorthernFruit(name); } // Public Veggie createVeggie(String name) {return new Veggie(name); }}Copy the code

View Code

package abstractFactory; /* * @auther youth * @mail [email protected] * @date 2019-07-24 10:53 * @notify Gardener managing tropical farms * @version 1.0 */ public Class TropicalGardener implements Gardener{public Fruit createFruit(String name){return new TropicalFruit(name); } // Public Veggie createVeggie(String name){return new TropicalVeggie(name); }}Copy the code

View Code

package abstractFactory; /* * @auther (@mail) [email protected] * @date 2019-07-24 12:28 * @notify * @version 1.0 */ import junit.framework.TestCase; public class AbstractFactoryTest extends TestCase { public void testCreate(){ TropicalGardener tropicalGardener = new TropicalGardener(); Fruit fruit1 = tropicalGardener. CreateFruit (" tropical Fruit "); Veggie veggie1 = tropicalGardener. CreateVeggie (" tropical vegetables "); assertTrue(fruit1 instanceof TropicalFruit); assertTrue(veggie1 instanceof TropicalVeggie); NorthernGardener northernGardener = new NorthernGardener(); Fruit fruit2 = northernGardener. CreateFruit (" northern Fruit "); Veggie veggie2 = northernGardener. CreateVeggie (" northern vegetables "); assertTrue(fruit2 instanceof NorthernFruit); assertTrue(veggie2 instanceof NorthernVeggie); }}Copy the code

View Code