2010-10-
In September 24, I started to look at the big talk design pattern and felt very good. At that time, I decided to read 23 design patterns during the 11 S and use C ++ to implement the code again, I spent three days in structure 11 and then worked overtime. I finally read it again. I only wrote a few chapters of code, and then I wrote it until now. I will write it well from today, one design pattern per day.
The factory mode starts with a simple calculator. At first, a main function is used to solve all the problems. Then, the business and logic are separated and the implementation of the function is written into a function, in the end, each operation method is written as a class and the factory method is used. Let me really understand the encapsulation, inheritance and Polymorphism of object-oriented programming. Below is the code I wrote.
Main Function
// Calculator. cpp: defines the entry point of the console application. <Br/> // </P> <p> # include "stdafx. H "<br/> # include <iostream> <br/> # include" operationfactory. H "<br/> # include" operation. H "</P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> double strnumbera, strnumberb, result; <br/> char stroperation; <br/> operation * operator; <br/> operationfactory factory; <br/> operator = factory. createoperate ('*'); <br/> values-> setnumbera (2); <br/> values-> setnumberb (4 ); <br/> result = response-> getresult (); <br/> return 0; <br/>}</P> <p>
Operation class implementation
# Pragma once <br/> # include "stdafx. H "<br/> # include <iostream> <br/> using namespace STD; </P> <p> class operation <br/>{< br/> public: <br/> operation (void) <br/>{< br/>}< br/> Public: <br/> ~ Operation (void) <br/>{< br/>}< br/> Public: <br/> double numbera; <br/> double numberb; <br/> char operate; <br/> Public: <br/> void setnumbera (double number) <br/>{< br/> numbera = number; <br/>}< br/> double getnumbera () <br/>{< br/> return numbera; <br/>}</P> <p> void setnumberb (double number) <br/>{< br/> numberb = number; <br/>}< br/> double getnumberb () <br/>{< br/> return numberb; <br/>}</P> <p> public: <br/> virtual double getresult () <br/> {<br/> double result = 0; <br/> return result; <br/>}< br/>}; <br/>
Operationadd, operationsub, operationmul, operationdiv class implementation
Class operationadd: <br/> Public operation <br/>{< br/> Public: <br/> operationadd (void) <br/>{< br/>}< br/> Public: <br/> ~ Operationadd (void) <br/>{< br/>}< br/> Public: <br/> double getresult () <br/> {<br/> double result = 0; <br/> result = numbera + numberb; <br/> return result; <br/>}< br/>}; </P> <p> class operationsub: <br/> Public operation <br/>{< br/> public: <br/> operationsub (void) <br/>{< br/>}< br/> Public: <br/> ~ Operationsub (void) <br/>{< br/>}< br/> Public: <br/> double getresult () <br/> {<br/> double result = 0; <br/> result = numbera-numberb; <br/> return result; <br/>}</P> <p >}; </P> <p> class operationmul: <br/> Public operation <br/>{< br/> Public: <br/> operationmul (void) <br/>{< br/>}< br/> Public: <br/> ~ Operationmul (void) <br/>{< br/>}< br/> Public: <br/> double getresult () <br/> {<br/> double result = 0; <br/> result = numbera * numberb; <br/> return result; <br/>}< br/>}; </P> <p> class operationdiv: <br/> Public operation <br/>{< br/> public: <br/> operationdiv (void) <br/>{< br/>}< br/> Public: <br/> ~ Operationdiv (void) <br/>{< br/>}< br/> Public: <br/> double getresult () <br/> {<br/> double result = 0; <br/> result = numbera/numberb; <br/> return result; <br/>}< br/>}; <br/>
Implementation of the computing Factory
Class operationfactory <br/>{< br/> Public: <br/> operationfactory (void) <br/>{< br/>}< br/> public: <br/> ~ Operationfactory (void) <br/>{< br/>}< br/> Public: <br/> operation * operation; <br/> public: <br/> operation * createoperate (char operate) <br/>{< br/> // operation operator = NULL; <br/> switch (operate) <br/>{< br/> case '+': <br/>{< br/> condition = new operationadd (); <br/> break; <br/>}< br/> case '-': <br/>{< br/> condition = new operationsub (); <br/> break; <br/>}< br/> case '*': <br/>{< br/> condition = new operationmul (); <br/> break; <br/>}< br/> case '/': <br/>{< br/> condition = new operationdiv (); <br/> break; <br/>}</P> <p> return response; <br/>}< br/>
In fact, I am not only familiar with the simple factory design model. What's more, I am familiar with some precautions in the C ++ code writing process, such:
1. When new is used, there is a pointer variable in front;
2. When adding a class, you must write the constructor. Even if the constructor does not do anything, you 'd better write it.