Dahua Design Pattern--Factory Pattern

 First, let's talk about the simple factory pattern: a parent class has one or more subclasses. A parent class can instantiate multiple subclasses through polymorphism.

code show as below

package effective.company;

public class LeiFeng {
	public void Sweep(){
		System.out.println("扫地。。。");
	}

	public void Wash(){
		System.out.println("Laundry...");
	}
	public void BuyRice(){
		System.out.println("Buy rice...");
	}
	public static void main(String[] args) {
		 IFactory factory = new VolunteerFactory();
		 LeiFeng student = factory.CreateLeiFeng();
		 student.BuyRice();
		 student.Sweep();
		 student.Wash();
	}
}
package effective.company;

public class Volunteer extends LeiFeng {
	public void Sweep(){
		System.out.println("Volunteers sweep the floor...");
	}

	public void Wash(){
		System.out.println("Volunteer laundry..");
	}
	public void BuyRice(){
		System.out.println("Volunteers buy rice...");
	}
}
package effective.company;

public class Undergraduate extends LeiFeng {
	public void Sweep(){
		System.out.println("Students sweep the floor...");
	}

	public void Wash(){
		System.out.println("Students do laundry...");
	}
	public void BuyRice(){
		System.out.println("Students buy rice...");
	}
	
	
}

When the client calls, the code is as follows:

package effective.company;

public class SimpleFactory {
	public static LeiFeng CreateLeiFeng(String type) {
		LeiFeng lf = null;
		switch (type) {
		case "college student":
			lf = new Undergraduate();
			break;

		case "Volunteer":
			lf = new Volunteer();
			break;
		}
		return lf;
	}

	public static void main(String[] args) {
                LeiFeng s1 = SimpleFactory.CreateLeiFeng("大学生");
		s1.BuyRice();
		LeiFeng s2 = SimpleFactory.CreateLeiFeng("大学生");
		s2.Wash();
		LeiFeng s3 = SimpleFactory.CreateLeiFeng("大学生");
		s3.BuyRice();
}}

It can be seen from the above code that the simple factory pattern generates too much repetitive code when the corresponding method is instantiated and called. If you modify college students to be volunteers, modify three places;

Here is the factory pattern:

package effective.company;

public interface IFactory {
	LeiFeng CreateLeiFeng();

}
package effective.company;

public class UndergraduateFactory implements IFactory{

	@Override
	public LeiFeng CreateLeiFeng() {
		// TODO Auto-generated method stub
		return new Undergraduate();
	}

}
package effective.company;

public class VolunteerFactory implements IFactory {

	@Override
	public LeiFeng CreateLeiFeng() {
		// TODO Auto-generated method stub
		return new Volunteer();
	}

}
                    IFactory factory = new VolunteerFactory();//Just modify here
		 LeiFeng student = factory.CreateLeiFeng();
		 student.BuyRice();
		 student.Sweep();
		 student.Wash();

From the above code, it can be found that to replace Undergraduate with Volunteer, you only need to change the implementation of new VolunteerFactory(), which reduces the amount of modified code.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325658324&siteId=291194637