WeChat PK10 program, WeChat airship program, whole station packaging without deletion

WeChat PK10 program, WeChat airship program, whole station packaging without deletion Download Address   Q2952777280

One hot noon in May, I chatted with my little brother about the design model used by the company.

I said jokingly: "Our company does not use many design models, that is, strategy, facade, simple factory, factory, single case, command, combination, template, etc."

Xiaodang: "What is a simple factory? Is it different from the factory mode?"

As soon as I listened, I crossed my hands over my chest and said with a slight meaning, "What factory model do you know?"

Xiao Dang said fluently, "Take the object methods you create into a class and create the corresponding objects according to their type."

I exclaimed: "Xiaodang, you know the wrong factory model. This is a simple factory."

Xiao Dang was full of doubts and said, "What is factory mode and what is a simple factory?"

I drank a drink of water and said, "

Simple factories are not design patterns, but programming habits.

The method of creating the object is extracted into a common class and the corresponding object is created according to the type.

It is not one of the 23 GOF design modes.

For example, PL3, DLT and others have their own prize calculators.Simple factories are implemented as follows:

package factory;
public class SimpleCalculatorFactoryDemo {
    public static abstract class Calculator{
        private String type;
        public Calculator(String type) {
            this.type = type;
        }
        public void calculate(){
            System.out.println(" This "+type+" calculate is done");
        }
    }
    public static class PL3Calculator extends Calculator{
        public PL3Calculator() {
            super("PL3");
        }
    }
    public static class DLTCalculator extends Calculator{
        public DLTCalculator() {
            super("DLT");
        }
    }

    public static class SimpleCalculatorFactory{
        public  Calculator createCalculator(String type){
            if ("PL3".equals(type)) { return new PL3Calculator(); }
            else if ("DLT".equals(type)){ return new DLTCalculator(); }
            return null;
        }
    }

    public static class PrizeTicketHandler{
        SimpleCalculatorFactory factory;
        public PrizeTicketHandler(SimpleCalculatorFactory factory){
            this.factory=factory;
        }
        public Calculator getCalculator(String type){
            return factory.createCalculator(type);
        }
    }
    public static void main(String[] args) {
        SimpleCalculatorFactoryDemo. SimpleCalculatorFactory factory=new SimpleCalculatorFactory();
        PrizeTicketHandler prizeTicketHandler=new PrizeTicketHandler(factory);
        prizeTicketHandler.getCalculator("DLT").calculate();
    }
}

Its class diagram is as follows:

When I was about to go down and say something about the shortcomings of the simple factory, Xiao Dang was impatient when he chewed the apple and said, "You don't have to go into details about this. I'm already familiar with it.""

Sweeping his lips, he said with a smile, "What's wrong with this?"

Xiao Dang stopped chewing apples and thought for more than 10 seconds. "Every new class is added, the if judgment statements in the factory increase accordingly, which is very detrimental to the maintenance and expansion of the system."

I nodded in approval.

"Indeed, as specific product classes increase, the logic of simple factories needs to be modified, making it increasingly difficult to expand and maintain, closing modifications that have not been made, and developing extensions."

After snacking on a large portion of the apple, he said thoughtfully, "That's not what we write in our system. The winners are digital colors and competitive colors."

After I drank some water, I tried to guide Xiaodang and asked, "Do you still remember how our system was implemented?"

Xiao Dang threw away the apple core, wiped his hand and said, "Of course, this is achieved roughly as follows."

(Note: Implementation of Factory Mode)

package factory;
public class FactoryCalculatorDemo {
    public static abstract class Calculator{
        private String type;
        public Calculator(String type) {
            this.type = type;
        }
        public void calculate(){
            System.out.println(" This "+type+" calculate is done");
        }
    }
    public static class PL3Calculator extends Calculator {
        public PL3Calculator() {
            super("PL3");
        }
    }
    public static class DLTCalculator extends Calculator {
        public DLTCalculator() { super("DLT"); }
    }
    public static class BSKCalculator extends Calculator {
        public BSKCalculator() {
            super("BSK");
        }
    }
    public static class FTCalculator extends Calculator {
        public FTCalculator() {
            super("FT");
        }
    }

    public static abstract class CalculatorFactory{
        public abstract Calculator createCalculator(String type);
    }

    public static class NumberLotteryCalculatorFactory extends CalculatorFactory {
        @Override
            public Calculator createCalculator(String type){
                if ("PL3".equals(type))
                { return new PL3Calculator(); }
                else if ("DLT".equals(type))
                { return new DLTCalculator(); }
                return null;
            }
    }

    public static class JCCalculatorFactory extends CalculatorFactory {
        @Override
        public Calculator createCalculator(String type){
            if ("BSK".equals(type))
            { return new BSKCalculator(); }
            else if ("FT".equals(type))
            { return new FTCalculator(); }
            return null;
        }
    }

    public static void main(String[] args) {
        //Caller, aware of subclasses used
        CalculatorFactory calculatorFactory=new NumberLotteryCalculatorFactory();
        calculatorFactory.createCalculator("PL3").calculate();
        calculatorFactory=new JCCalculatorFactory();
        calculatorFactory.createCalculator("FT").calculate();
    }
}

Its class diagram is as follows:

I am glad that he has a good command and I can safely go.Then, instead of waiting for a little talk, force him to conclude:

"This is the factory mode, which defines an interface for creating objects, but which of the classes the subclasses feel they want to instantiate is determined by NumberLotteryCalculatorFactory and JCCalculatorFactory."

Xiaodang said, "NumberLotteryCalculatorFactory and JCCalculatorFactory also achieve parallel hierarchy relationship. If there are new classes added, it is better to add a corresponding factory subclass without modifying the original code."

"Yes," I nodded.

Xiao Dang touched his chin and asked, "The factory mode understands, so why do simple factories still work in the system?"

I said with a smile: "Our checker part is a simple factory. There are only three checkers: note number, format, amount, basically unchanged.So just use a simple factory, remember that the simpler the function, the better, Keep it Simple is best"

Summary:

Factory mode: Defines an interface for creating objects, but the subclass thinks which class to instantiate.

As for whether to choose a simple factory or a factory mode, depending on the business needs, the simpler the function, the better.

Keep it Simple is best.

Keywords: calculator Programming

Added by Klojum on Wed, 15 May 2019 05:02:17 +0300