Design pattern 2 Object-oriented design principles

Source: Internet
Author: User


Object-oriented design principle principle objective object-oriented design original table single Responsibility principle case Closed-closing principle case dependence reversal principle case

Object-Oriented design principles


For the design of object-oriented software system, it is a very important problem to improve the reusability of the system while supporting maintainability, and how to improve the maintainability and reusability of a software system is one of the core problems in object-oriented design. In object-oriented design, maintainability reuse is based on design principles. Each principle contains some ideas of object-oriented design, which can promote the design level of a software structure from different angles.


The principle of object-oriented design was born to support maintainability reuse, which is embodied in many design patterns, which are the guiding principles summarized in many design scenarios. Object-oriented design principles are also one of the important indicators that we use to evaluate the effectiveness of a design pattern.


purpose of the principle: cohesion, low coupling


Object-oriented Design original table

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/8A/D8/wKioL1g9S6WgngolAAOuZbIXL0U218.png "title=" Object-oriented Design original table. png "alt=" Wkiol1g9s6wgngolaaouzbixl0u218.png "/>



1, single duty principle:

If you separate the methods of each class, you can guarantee that each class has a single pointer.

Improved security.

Improved serviceability

Improved readability.



Single principle, model 1,

[Email protected]:~/design$ cat main.cpp #include <iostream>using namespace Std;class clothes{public:void working () {cout << "wear formal Clothes" <<ENDL;} void Shoping () {cout << "wear casual clothes" <<endl;}}; int main () {clothes C; c.working (); c.shoping (); return 0;} [Email protected]:~/design$ g++ main.cpp &&/a.out wear formal clothes for casual clothes [email protected]:~/design$


But this project, the old programmer went away, the new programmer took over and asked to wear casual clothes for work

The code then becomes this:

[Email protected]:~/design$ g++ main.cpp &&./a.out wear formal clothes in official clothes [email protected]:~/design$ cat Main.cpp # Include <iostream>using namespace Std;class clothes{public:void working () {cout << "wear formal Clothes" <<ENDL;} void Shoping () {cout << "wear formal clothes" <<endl;}}; int main () {clothes C; c.working (); c.shoping (); return 0;} [Email protected]:~/design$ g++ main.cpp &&./a.out wear formal clothes in formal clothes [email protected]:~/design$


Then a new programmer looked at the two lines with the same function, and changed

[Email protected]:~/design$ cat main.cpp #include <iostream>using namespace Std;class clothes{public:void working () {cout << "wear formal Clothes" <<ENDL;} void Shoping () {cout << "wear formal clothes" <<endl;}}; int main () {clothes C; c.working (); c.working (); return 0;} [Email protected]:~/design$ g++ main.cpp &&./a.out wear formal clothes in formal clothes [email protected]:~/design$


Then the novice to the project will be completely dizzy .....

As a single principle, so you don't have to do it. , do not modify the code that has already been written.

[Email protected]:~/design$ cat main.cpp #include <iostream>using namespace Std;class clothesworing{public:void Style () {cout << "dress up" << Endl;}}; Class Clothesshoping{public:void style () {cout << "wear casual" << Endl;}}; int main () {clothesshoping cs; Clothesworing Cw;cs.style (); Cw.style (); return 0;} [Email protected]:~/design$ g++ main.cpp &&/a.out wear casual dress up [email protected]:~/design$


single principle of responsibility :

If you separate the methods of each class, you can guarantee that each class has a single pointer.

Improved security.

Improved serviceability

Improved readability.



2, opening and closing principle case:

The change of class is to add code instead of modifying source code

Original code: [Email protected]:~$ cat main.cpp//design mode opening/closing principle//Definition: Class changes are made by adding code instead of modifying source code #include<iostream>using namespace Std;class banker{public:void Save () {cout << deposit << Endl;} void Pay () {cout << "payment" << Endl;} void Transfer () {cout << "transfer" <<endl;}}; int main () {Banker B; B.save (); B.pay (); B.transfer (); return 0;} [Email protected]:~$ g++ main.cpp &&./a.out Deposit Payment Transfer [email protected]:~$

Span style= "Font-size:24px;color:rgb (255,0,0); Background-color:rgb (255,255,0);" > code optimization,

[email protected]:~$ cat main.cpp //design mode   Open/close principle//Definition: Class changes are made by adding code instead of modifying source code #include< iostream>using namespace std;////////////////////////////////////////////////////////////////// class abstractbanker //abstract class {Public://abstractbanker ()  = 0;virtual ~abstractbanker () {}; Virtual void work ()  = 0;//interface};// // // // // // // //  // // // // // // // // // // // // //  class savebanker:public abstractbanker //deposit, inheriting abstract class {Public:virtual void work () {cout  <<  "Deposit Method"  << endl;}}; class paybanker:public abstractbanker //payment, inheriting abstract class {Public:virtual void work () {cout  <<  "Payment Method"  << endl;}}; class transferbanker:public abstractbanker //transfer, inheriting abstract class {Public:virtual void work () {cout  <<  "Transfer method" << endl;}};/ Int main () {savebanker* sb = new  savebanker;sb->work ();d elete sb; Paybanker  *pb = new paybanker;pb->work ();d elete sb; Transferbanker *tb = new transferbanker;tb->work ();d elete tb;return 0;} [email protected]:~$ g++ main.cpp -wall && ./a.out  method of payment method of deposit method [ email protected]:~$


New fund processing requirements, no need to modify the source code

[email protected]:~$ cat main.cpp //design mode   Open/close principle//Definition: Class changes are made by adding code instead of modifying source code #include< iostream>using namespace std;////////////////////////////////////////////////////////////////// class abstractbanker //abstract class {Public://abstractbanker ()  = 0;virtual ~abstractbanker () {}; Virtual void work ()  = 0;//interface};// // // // // // // //  // // // // // // // // // // // // //  class savebanker:public abstractbanker //deposit, inheriting abstract class {Public:virtual void work () {cout  <<  "Deposit Method"  << endl;}}; class paybanker:public abstractbanker //payment, inheriting abstract class {Public:virtual void work () {cout  <<  "Payment Method"  << endl;}}; class transferbanker:public abstractbanker //transfer, inheriting abstract class {Public:virtual void work () {cout  <<  "Transfer method" << endl;}}; class fundbanker:public abstractbanker //new fund, inherit abstract class, do not need to modify the source code of other classes {public:virtual void  Work () {cout <<  "Fund processing"  << endl;}};/ Int main () {savebanker* sb = new  savebanker;sb->work ();d elete sb; Paybanker  *pb = new paybanker;pb->work ();d elete sb; Transferbanker *tb = new transferbanker;tb->work ();d elete tb; Fundbanker *fb = new fundbanker;fb->work ();d elete fb;return 0;} [email protected]:~$ g++ main.cpp -wall && ./a.out  Deposit Method Payment Method transfer Method Fund processing [email protected]:~$


Summarize:

Open and close principle: safe and stable, can be maintained.



3, dependence reversal principle case:

Zhang San to work and drive a Mercedes

[Email protected]:~$ cat main.cpp//Design mode: Dependency reversal #include<iostream>using namespace Std;class benz{public:void run () { cout << "Mercedes-Benz launched" << Endl;}; Class Zhangsan{public:void Drivebenz (Benz *car) {cout << "Zhang San Drive to work" << endl;car->run ();}}; int main () {Benz *benz = new Benz; Zhangsan *z3 = new Zhangsan;z3->drivebenz (benz); return 0;} [Email protected]:~$ g++ main.cpp-wall &&/a.out Zhang San drove to work Mercedes-Benz started [email protected]:~$


Zhang San may also drive a BMW to work.

[email protected]:~$ cat main.cpp //design mode: Dependency reversal # include <iostream>using namespace std;class benz{public:void run () {cout <<  " The Mercedes-Benz started the  << endl;}}; Class bmw{public:void run () {cout <<  "BMW launched"  << endl;}}; Class zhangsan{public:void drivebenz (Benz *car) {cout <<  "Zhang San   drive to work" <<  endl;car->run ();} VOID&NBSP;DRIVEBMW (Bmw *car) {cout <<  "Zhang San   drive to Work" << endl;car->run ();}; Int main () {benz *benz = new benz; Zhangsan *z3 = new zhangsan;z3->drivebenz (Benz); BMW&NBSP;*BMW&NBSP;=&NBSP;NEW&NBSP;BMW;Z3->DRIVEBMW (BMW); return 0;} [email protected]:~$ g++ main.cpp -wall && ./a.out  Zhang San   Drove to work Mercedes started Zhang San   drove to work BMW started the [email protected]:~$ 

-------------------------------

The business is becoming more and more complex, what about the public?

More and more chaotic .....

------------------------------------------


Isolate the business layer and the implementation layer through the abstraction layer and decouple

[email protected]:~$ cat main.cpp //design mode: Dependency reversal//business layer and implementation layer   through abstraction layer   isolation, decoupling  # include<iostream>using namespace std;//////////Abstraction Layer   car    man  ////////////////// Class car{public:virtual void run ()  = 0;virtual ~car () {}};class  Driver{public:virtual void drive (Car *car)  = 0;virtual ~driver () {}};////////      Implementation Layer         /////////////////////////class  zhangsan:public driver{public:virtual void drive (Car *car) {cout <<  " Zhang San drove to work " << endl;car->run ();}}; Class lisi:public driver{public:virtual void drive (Car *car) {cout <<  "John Doe   drove to work"  << endl;car->run ();}; Class benz:public car{public:virtual void run () {cout <<  "benz  started up"  << endl;};}; ClAss bmw:public car{public:virtual void run () {cout <<  "bmw  started"   << endl;};};/   Main function  ////////////////////////////////////int main () {//Jeng Changsan Benz Car * benz  = new benz;driver *zhangsan = new zhangsan;zhangsan->drive (Benz);// Get John Doe to work   drive BMW car *bmw = new bmw;driver *lisi = new lisi;lisi-> Drive (BMW);   return 0;} [email protected]:~$ g++ main.cpp -wall && ./a.out  Zhang San drove to work Benz   started John Doe   drove to work bmw  started [email protected]:~$




Dependency reversal principle, computer assembly case:

Abstraction Layer: CPU, graphics card, memory

Frame layer: Combined CPU, video card, memory

[email protected]:~$ cat main.cpp //design mode: Dependency reversal, computer assembly case #include<iostream>using  namespace std;//abstract class Class cpu{public:virtual void caculate ()  = 0;virtual ~ CPU () {}};class card{public:        virtual void display ()  = 0;virtual ~card () {}};class memmory{public:virtual void storage ()  =  0;virtual ~memmory () {}};//schema class Class computer{public:computer (Cpu* cpu,card* card, MEMMORY*&NBSP;MEM) {This->cpu = cpu;this->card = card;this->mem = mem;} Virtual ~computer ()  {};void work () {cpu->caculate (); Card->display (); Mem->storage ();} private:cpu *cpu; card *card; memmory* mem;};/ Implementation Layer//////////////////////////////////class intelcpu:public cpu{public:virtual void  Caculate () {cout <<  "intel cpu working" &NBSP;&LT;&LT;&NBsp;endl;}}; Class nvidiacard:public card{public:virtual void display () {cout <<  "nvidia  card  working " << endl;}}; Class kingston:public memmory{public:virtual void storage () {cout <<  " Kingston mem  working " << endl;}};/    main function  ///////////////////int main () {cpu* cpu = new intelcpu; card* card = new nvidiacard; memmory* mem= new kingston; Computer* computer = new computer (CPU,CARD,MEM); Computer->work (); return 0;} [Email protected]:~$ g++ main.cpp -wall && ./a.out intel cpu  workingnvidia card  workingkingston mem  working[email protected]:~$


Principle of substitution on the Richter scale: slightly

Interface Isolation principle:

Synthetic multiplexing principles: inheritance, composition, dependency

can use combination without inheriting

Dimitri Law:





This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1877855

Design pattern 2 Object-oriented design principles

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.