NetEase Cloud Classroom _c++ Development Introduction to Mastery _ Chapter 8: Design Patterns

Source: Internet
Author: User
Tags setf

Lesson 44 Introduction to design Patterns

Introduction to Design Patterns

The first principle of object-oriented design: Programming for interfaces, not for implementation

interfaces, pointers

Implementation-CASE

If a class A is already present and you want to reuse Class A, there are two ways to do it.

Reuse through inheritance

Multiplexing by combination

The second principle of object-oriented design: Prioritize object combinations rather than class inheritance

Lesson 45 Observer Mode

If two objects are loosely coupled, they can interact with each other, but their dependence on each other is small.

#include <iostream> #include <assert.h> #include <list>class observer{public:virtual ~observer () = 0{ }virtual void update (float, float, float) = 0;}; Class Subject{public:virtual ~subject () = 0{}virtual void Registerobserver (Observer *) = 0;virtual void Removeobserver (Ob Server *) = 0;virtual void Notifyobserver () const = 0;}; Class Currentcondition:p ublic observer{public:currentcondition (): m_temperature (0.0), m_humidity (0.0) {}~ Currentcondition () {}void update (float temperature, float humitity, float pressure) {m_temperature = Temperature;m_ Humidity = Humitity;m_pressure = Pressure;display ();} void display () {STD::COUT.SETF (std::ios::showpoint); Std::cout.precision (3) std::cout << "Current Condition" << m_temperature;std::cout << "C, and" << m_humidity << "% humidity" << std::endl;} Private:float m_temperature;float m_humidity;float m_pressure;}; Class Forecastcondition:p ublic observer{public:forecastcondition (): m_currentpressure (0.0), M_lastpressure (0.0) {}~forecastcondition () {}void update (float temperature, float humitity, float pressure) {m_ Lastpressure = M_currentpressure;m_currentpressure = Pressure;display ();} void display () {STD::COUT.SETF (std::ios::showpoint); Std::cout.precision (3) std::cout << "Current Condition" << m_temperature;std::cout << "C, and" << m_humidity << "% humidity" << std::endl;} Private:float m_temperature;float m_humidity;float m_pressure;float m_lastpressure;float m_currentPressure;}; Class Weatherdata:p ublic subject{public:weatherdata (): m_temperature (0.0), m_humidity (0.0), m_pressure (0.0) {}~ Weatherdata () {}void registerobserver (Observer *o) {assert (O); M_observers.push_back (o);} void Removeobserver (Observer *o) {m_observers.remove (o);} void Notifyobserver () const{for (std::list<observer *>::const_iterator it = M_observers.begin (); it! = M_ Observers.end (); ++it) {Observer *obj = *it;obj->update (M_temperature, m_humidity, m_pressure);}} void setmeasurements(float temperature, float humitity, float pressure) {m_temperature = Temperature;m_humidity = Humitity;m_pressure = pressure;} Private:float m_temperature;float m_humidity;float m_pressure;std::list<observer*> m_observers;private: Weatherdata (const weatherdata&);}; int main () {currentcondition current; Forecastcondition forecast; Weatherdata Weatherdata;weatherdata.registerobserver (&current); Weatherdata.registerobserver (&forecast); Weatherdata.setmeasurements (+, 30.4f); Weatherdata.setmeasurements (+, 29.2f); Weatherdata.setmeasurements ( 29.2f); system ("pause"); return 0;}

Lesson 46 Strategy Mode

#include <iostream> #include <assert.h> #include <memory>class flybehavior//-Fly abstract class {Public:virtual ~ Flybehavior () = 0{}virtual void Fly () const = 0;}; Class Flynoway:p ublic flybehavior//will not fly {public:void fly () const{std::cout << "I can ' t fly" << Std::endl;}; Class flyrocketpowered:p ublic flybehavior//rocket Fly {public:void fly () const{std::cout << "I ' m flying with a rocket" < < Std::endl;}}; Class flywithwings:p ublic flybehavior//wings Fly {public:void fly () const{std::cout << "I m flying" << Std::endl;} Class quackbehavior//called abstract class {public:virtual ~quackbehavior () = 0{}virtual void Quack () const = 0;}; Class Fakequack:p Ublic quackbehavior//false Call {public:void quack () const{std::cout << "Qwak" << Std::endl;}; Class Mutequack:p ublic quackbehavior//not called {public:void quack () const{std::cout << "Silence" << Std::endl;}; Class Quack:p ublic quackbehavior//ga {public:void quack () const{std::cout << "Quack" << Std::endl;}}; Class SqueaK:p ublic quackbehavior//squeak {public:void quack () const{std::cout << "Squeak" << Std::endl;}}; Class duck//Duck abstract class {public:void Setflybehavior (Flybehavior *fb) {assert (FB); _flybehavior = std::auto_ptr< Flybehavior> (FB);} void Setquackbehavior (Quackbehavior *qb) {assert (QB); _quackbehavior = std::auto_ptr<quackbehavior> (QB);} void Performfly () const{_flybehavior->fly ();} void Performquack () const{_quackbehavior->quack ();} void swim () const{std::cout << "All ducks float, even decoys!" << Std::endl;} virtual void display () const = 0;protected:duck (Flybehavior *flybehavior, Quackbehavior *quackbehavior): _flybehavior ( Flybehavior), _quackbehavior (Quackbehavior) {assert (Flybehavior); assert (Quackbehavior);} Virtual ~duck () {}private:std::auto_ptr<flybehavior>_flybehavior;//smart pointer std::auto_ptr<quackbehavior>_ Quackbehavior;duck (const duck&);//disable copy constructorvoid operator= (const duck&);//disable Assignment Operator};class Decoyduck:p ublic duck//Decoy Duck {public:decoyduck ():D uck (New Flynoway (), New Mutequack ()) {}void display () const{std::cout << "I ' m a Duck decoy" << Std::endl;}}; Class Mallardduck:p ublic duck//Mallard {public:mallardduck ():D uck (New Flywithwings (), New Quack ()) {}void display () const{ Std::cout << "I ' m a real mallard duck" << Std::endl;}}; Class Modeldduck:p ublic duck//false Duck {public:modeldduck ():D uck (New Flynoway (), New Fakequack ()) {}void display () CONST{STD: : cout << "I ' m a model duck" << Std::endl;}}; Class redhead:p ublic duck//Red-headed Duck {public:redhead ():D uck (New Flywithwings (), New Quack ()) {}void display () const{std::cout << "I ' m a real Red Head duck" << Std::endl;}}; Class Rubberduck:p ublic duck//Rubber Duck {public:rubberduck ():D uck (New Flynoway (), New Squeak ()) {}void display () CONST{STD:: cout << "I ' m a rubber duck" << Std::endl;}}; int main () {Std::auto_ptr<mallardduck>mallard (New Mallardduck ()); Std::auto_ptr<rubberduck>rubberduck ( New Rubberduck ()); STD::AUTO_PTR&LT;decoyduck>decoy (New Decoyduck ()); Std::auto_ptr<modeldduck>model (new Modeldduck ());mallard-> Performfly (); Mallard->performquack (); Rubberduck->performfly (); Rubberduck->performquack ();d ecoy-> Performfly ();d ecoy->performquack (); Model->performfly (); Model->performquack (); Model->setflybehavior (New flyrocketpowered ()); Model->performfly (); System ("pause"); return 0;}

NetEase Cloud Classroom _c++ Development Introduction to Mastery _ Chapter 8: Design Patterns

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.