Elegant Java -- how are elegant manufacturing objects implemented? Factory mode

Posted by Tubby on Mon, 10 Jan 2022 16:50:41 +0100

Column navigation

Elegant Java (zero) -- problem oriented learning

preface

We talked about several example methods of single example before
Let's take a broader perspective. In fact, Java is often designed from top to bottom. First specify the interface, specify the parent class, formulate standards, and then consider how to implement it! So this component is often not just a singleton problem! It may be an interface implementation class tree or a parent subclass tree, as well as a hybrid interface, abstract parent class and concrete subclass structure

So the question is, suppose I have a component that depends on multiple implementations, which one should I choose? Or is there more than one subclass that I should choose?

At this time, there is a problem similar to that we will mention in Article 6 - the caller needs to consider that the coupling degree is too high for the callee. In case of any problem with the callee, is the caller not GG? All change?

For an inappropriate example, the teacher receives the homework and the students hand in the homework. The homework may be in the form of paper, online documents, word documents or markdown documents. The efficient teacher will not care which of your students gives it in what form (subclass Implementation). He will only ask the students to hand in the paper, and others will try to print it by themselves (the students will manage the teacher, but only specify the rules)

The factory mode here is the collection component called by the teacher. How can students switch to which implementation? No matter the teacher sets the standard, the students can solve it by themselves (the factory determines which implementation to use according to the teacher's parameters and then output it)

To sum up, factory mode is required for the following occasions:

  • The client class does not care which implementation should be used, but only formulates the interface function
  • The creation process is complex and should be transparent to customers
  • So that customers don't have to worry about what subclass to choose. It should be you to provide customers with the right implementation, not which one to use
  • Delay implementation initialization

Although the last delay implementation uses the single example delay implementation idea, specifically, for example, there are only 10 of the 100 implementation subclasses that can be used. When the caller calls the factory, the factory decides which implementation should be used, which is equivalent to delaying to the factory

Factory method

This is a factory, which often has a very complex inheritance and composition structure. Here, it is simply set as an interface + implementation. We mainly use ConcreteFactory to build cars

	public interface Factory{
		Car manufactCar(Integer mode);
	}
	
	public class ConcreateFactory implements Factory{
		public Car manufactCar(Integer mode, Integer price) {
			if(mode == 1) return new Benz(price);
			else return new BWM(price); 
		}
	}
	

Obviously, the factory decides how to instantiate here, and the customer only needs to provide the mode and price. The factory will worry about the rest of the instantiation problems!

This is the car interface. There are two ways to realize that every car can drive

	public interface Car {
		Integer price;
		Car drive();
	}
	public class Benz implements Car{
		public Benz (price){
			this.price = price;
		}
		public String drive(){
			return "That's a driving Benz which worth $" + price;
		}
	}
	
	public class BWM implements Car{
		public BWM (price){
			this.price = price;
		}
		public String drive(){
			return "That's a driving BWM which worth $" + price;
		}
	}

Naturally, our client only needs:

	public class Client{
		private Factory factory; //Which factory
		
		public Client(Factory factory){
			this.factory = factory;
		}
		public void getAndDriveMyCar(){
			Car myCar = factory.manufactCar(1, 10000);
		}
		public static void main(String[] args){
			Client me = new Client(new ConcreteFactory()); // Which factory do you still need to choose
			client.getAndDriveMyCar();
		}
	}
	

In fact, the client call is really a word car mycar = factory manufactCar(1, 10000);

Static factory method

In fact, we can find that the concreteFactory does not need to be instantiated at all, except that non production vehicles need singletons. In that case, singletons and factories need singletons to be maintained by IOC containers

If you don't need a singleton, for example, we Integer Of course, the Integer object that valueof actually returns is multi instance

In this case, we can also use the static factory method, such as referring to Integer valueof

	
	public static Integer valueOf(int i) {
		if(i >= -128 && i <= IntegerCache.high)
			return IntegerCache.cache[i + 128];
		else 
			return new Integer(i);
	}
	

This valueOf is actually a simple static factory method. See, this is equivalent to directly setting the static factory method in the Car class. What factory class is needed?

Of course, it is limited to this simple instantiation, and it is not a singleton factory

Postscript

In the next article, we will talk about the creation of singleton objects before proxy. Now it is the general creation method of ordinary objects. What is the proxy mode? Why use agents, especially dynamic agents? The next article will show you

Column navigation
Elegant Java (zero) -- problem oriented learning

Topics: Java Back-end