1. Big talk design model, simple factory

Source: Internet
Author: User

1Through the design pattern, we can find encapsulation changes and loose coupling between objects. In view of the feeling of interface programming, the design is easy to maintain, easy to expand, easy to reuse, and flexible.Program.

2Program coupling is reduced by encapsulation, inheritance, and polymorphism. Separate the business logic from the interface logic to reduce the coupling between them, making it easier for them to maintain and expand.

3, ExampleCode

# Include "iostream" # include "string" # include "vector" // # include "functional" // # include "algorithm" using namespace STD; Class operation {public: operation () {} Operation (double dnumbera1, double dnumberb1) {dnumbera = dnumbera1; dnumberb = dnumberb1; dresult = 0.0;} double getnumbera () {return dnumbera ;} void setnumbera (double dtempa) {dnumbera = dtempa;} double getnumberb () {return dnumbera;} void setnumberb (doub Le dtempb) {dnumberb = dtempb;} virtual double getresult () {return dresult;} protected: Double dnumbera; double dnumberb; double dresult;}; Class operationadd: Public operation {public: operationadd () {} operationadd (double dnumbera, double dnumberb): Operation (dnumbera, dnumberb) {} double getresult () {return dresult = dnumbera + dnumberb;}; Class operationsub: public operation {public: operationsub () {} operationsub (Double dnumbera, double dnumberb): Operation (dnumbera, dnumberb) {} double getresult () {return dresult = dnumbera-dnumberb ;}; class operationmul: Public operation {public: operationmul () {} operationmul (double dnumbera, double dnumberb): Operation (dnumbera, dnumberb) {} double getresult () {return dresult = dnumbera * dnumberb;}; Class operationdiv: public operation {public: operationdiv () {} operationdiv (double d Numbera, double dnumberb): Operation (dnumbera, dnumberb) {} double getresult () {If (dnumberb = 0) Throw runtime_error ("the divisor cannot be "); return dresult = dnumbera/dnumberb; }}; class operationfactory {public: operationfactory (string stroperakind) {If (stroperakind = "+") popera = new operationadd (); if (stroperakind = "-") popera = new operationsub (); If (stroperakind = "*") popera = new operationmul (); If (stropera Kind = "/") popera = new operationdiv ();} operation * getoperation () {return popera ;}~ Operationfactory () {If (popera) {Delete popera; popera = NULL ;}} private: Operation * popera ;}; int main () {cout <"input two num and a operation (a number, opera, a number):" <Endl; double dnumbera1, dnumberb1; string stropera; cin> dnumbera1> dnumberb1> stropera; operationfactory myoperationfac (stropera); operation * myoperation = myoperationfac. getoperation (); myoperation-> setnumbera (dnumbera1); myoperation-> setnumberb (dnumberb1); cout <myoperation-> getresult (); return 1 ;}

4,UMLFigure

A ) Class (Class): The class name displayed in italic is an abstract class, + "Is Public ," - "Is Private ," # "Is Protected . First behavior class name; second behavior feature (field or attribute); Third behavior operation (method or behavior ). Abstract class. The class name is in italic.

B)Interface(Interface 《InterfaceIs different from the category, or is marked as a lollipop.

C) Inheritance:Hollow triangle+The solid line is the inheritance relationship. Birds inherit the characteristics of animals.

D) Implementation interface:Hollow triangle+Dotted Line. For example, implement the flying interface.

E) When a class needs to know another class, it can be associated (Association). Use the solid arrow.For example, penguins are associated with the climate because Penguins need to know the climate.

F) Aggregation relationship (Aggregation)VSSynthesize (Composition):

aggregation weak" owned "Relationship: A objects can contain B object, however, B the object is not A A part of the object. A each individual in the object can be separated from A the object exists independently. Make Hollow Diamond+Solid line arrow. For example, geese are aggregated by geese.

CompositionIs a strong "possess" relationship, that is:BThe object isAObject. Has the same lifecycle.Use solid diamond+Solid line arrow. For example, wings are components of birds.

AggregationAndCompositionIt can have a base.

G) Dependency (Dependency):Use the dotted arrow to represent, Pointing to the dependent object. For example, animals depend on oxygen and water.

5It can be seen from the top. No matter what the relationship is, such as association, combination, and aggregation, it is also implemented through polymorphism, abstraction, and inheritance in object-oriented languages. In terms of association, combination, and aggregation, although they have different meanings, most of the Code is implemented by defining another object (or its reference and pointer) in an object.

Refer:

【1]UMLFigure

Http://my.chinaunix.net/space.php? Uid = 13830775 & Do = Blog & id = 97645

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.