Some thoughts on learning design patterns for the first time
At present, I am reading the book "Dahua design pattern", which is cheap and easy to understand.
The steps are in place
That is to find that you can really improve the logic of your code, feel the convenience brought by design patterns, and understand why and what are the benefits of doing so.
What is the simple factory model
It uses three object-oriented features
1. Encapsulation: the separation of business logic and interface logic can make it easy to expand and maintain. This will reduce the coupling between them
2. Inheritance and polymorphism: it can achieve flexible expansion and modification, as well as code reuse
The simple factory pattern refers to who needs to be instantiated and whether a function will be added in the future. Adding an instantiated object requires a class to create the instance separately
Make a simple calculator console
I I just came out
Of course, I'm no exception. I always like to write code in a pile, just like the computer mode.
public static void main(String[] args) { // Simulate two double double A = 1.05; double B = 2.00; // Analog operator String operation = "+"; double result = 0; if (operation.equals("+")) { result = strNumA + strNumB; } if (operation.equals("-")) { result = strNumA - strNumB; } if (operation.equals("*")) { result = strNumA * strNumB; } if (operation.equals("/")) { result = strNumA / strNumB; } System.out.println(result + ""); }
emmm, a simple and bug gy calculator is done
II I've been working for a while
After working for a period of time, I found my code and its nonstandard
So I have the following better code
public static void main(String[] args) { // Simulate two double double strNumA = 1.05; double strNumB = 0; // Analog operator String operation = "/"; double result = 0; switch (operation) { case "+": result = strNumA + strNumB; break; case "-": result = strNumA - strNumB; break; case "*": result = strNumA * strNumB; break; case "/": if (strNumB == 0) { try { throw new Exception("Denominator cannot be 0!"); } catch (Exception e) { e.printStackTrace(); } } result = strNumA / strNumB; break; } System.out.println(result + ""); }
III After being beaten for a while
After being caught many times, I know to separate the code to facilitate maintenance and expansion. Make the code reusable instead of copying
It is also an object-oriented idea.
therefore:
//Business class public class Operation { public double result(double strNumA, double strNumB, String operat) { double result = 0; switch (operat) { case "+": result = strNumA + strNumB; break; case "-": result = strNumA - strNumB; break; case "*": result = strNumA * strNumB; break; case "/": if (strNumB == 0) { try { throw new Exception("Denominator cannot be 0!"); } catch (Exception e) { e.printStackTrace(); } } result = strNumA / strNumB; break; } return result; } } //Interface class public static void main(String[] args) { // Simulate two double double strNumA = 1.05; double strNumB = 0.05; // Analog operator String operat = "/"; double result = new Operation().result(strNumA, strNumB, operat); System.out.println(result + ""); }
IV After N times to modify their own code
Whether it's the change in requirements or the logic of your business, you start coding without being clear. They found that the love code they wrote was difficult to maintain and a little like a rebellious child.
Moreover, the above love code can not be flexibly expanded and modified.
When you want to modify or add an operation. It will be easy to modify other operation rules.
It's too risky
Try to get polymorphism and inheritance into it. Make the code loose coupling.
So we can separate various operation rules in the business class
//Operation class is a large concept class public class Computing { private double strNumA; private double strNumB; public double getStrNumA() { return strNumA; } public void setStrNumA(double strNumA) { this.strNumA = strNumA; } public double getStrNumB() { return strNumB; } public void setStrNumB(double strNumB) { this.strNumB = strNumB; } public double getResult() { double result = 0; return result ; } } //The addition, subtraction, multiplication and division class needs to inherit the operation class and override the getResult virtual method of the operation class //Additive class public class OperationADD extends Computing { public double getResult() { double result = 0; result = getStrNumA() + getStrNumB(); return result; } } //Subtraction class public class OperationSUB extends Computing { public double getResult() { double result = 0; result = getStrNumA() - getStrNumB(); return result; } } //multiplicative public class OperationMUL extends Computing { public double getResult() { double result = 0; result = getStrNumA() * getStrNumB(); return result; } } //Division class public class OperationDIV extends Computing { public double getResult() { if (getStrNumB() == 0) { try { throw new Exception("Denominator cannot be zero!"); } catch (Exception e) { e.printStackTrace(); } } double result = 0; result = getStrNumA() / getStrNumB(); return result; } }
V Simple factory mode
Inherit the operation class, rewrite the operation method inside, and then modify an operation rule, it will not move to other codes, but how to let the computer know which operation rule I need?
That is, how to instantiate objects
The design pattern of this content can help you create an instance. You only need to pass in the operator, and it will automatically give you the object you want, which is the factory
//Computing factory public class OperationFactory { public Computing createComputing(String operat) { Computing com = null; switch (operat) { case "+": com = new OperationADD(); break; case "-": com = new OperationSUB(); break; case "*": com = new OperationMUL(); break; case "/": com = new OperationDIV(); break; } return com; } }
Through polymorphism, the result of the calculator is realized by returning the parent class
//Interface code public static void main(String[] args) { OperationFactory operationFactory = new OperationFactory(); //Ask the factory for the operation object you want Computing computing = operationFactory.createComputing("+"); computing.setStrNumA(4.50); computing.setStrNumB(5.65); double retuResult = computing.getResult(); System.out.println(retuResult + ""); }
Later, if you want to add an operation, you need to create a class to inherit the operation class. And inject the creation rules of this operation in the factory, and it can be used
It was not until recently that I realized that there was no end to learning programming.