the compound
Use composite as much as possible
Use private inheritance when you need to change a virtual function in the private inheritance base class
Another way to override a private inheritance is to change the virtual function by using a compound that defines a nested class that inherits from the public, and the composite class uses that nested object.
class Widget {private: class WidgetTimer:public Timer {
Specify interface inheritance and enforce inheritanceArticle 35: Consider alternatives other than the virtual function。。。。。。。。Article 36: Never redefine inherited non-virtual functionsWhen a non-virtual function in a base class is redefined in a derived class, accessing the Non-virtual function with a pointer to a base class type, which actually points to a derived class object, accesses the Non-virtual function of the base class
(According to the msdn webcast courses)
This is a behavior-oriented design model that is widely used.
Objects may often need different typesAlgorithm, But if the change is frequent, the type will become very fragile.
As shown inCubeTake, take different cross sections, then its area calculation method will be different, need to use different algorithms.
So, how can we
About OOP
Blog address: http://www.cnblogs.com/ronny reprint please indicate the source!
1. inheritance can be a single inheritance or multiple inheritance. Each inheritance connection can be public, protected, or private, or virtual or non-virtual.
2. Each option of the member function is virtual, non-virtual, or pure-virtual.
3. Interaction between member functions and other language features: What are the interactions between default parameter values and virtual functions? How does in
event update, type is delegate EventHandler Public EventEventHandler Update; Private stringAction; Public voidNotify () {Update (); } Public stringSubjectstate {Get{returnAction;} Set{action =value;} } } classBoss:subject {//declares an event update, type is delegate EventHandler Public EventEventHandler Update; Private stringAction; Public voidNotify () {Update (); } Public stringSubjectstate {Get{returnAction;} Set{action =value;} } } //colleagues
-virtual functions specify interface inheritance and enforce inheritance. (as described in clause 33, do not hide non-virtual functions of the base class in a derived class in public inheritance)//Article 38: By compounding the mold out of the has-a or according to something to achieve//clause 39: Use private inheritance wisely and cautiously//1. Private inheritance is highly likely to be an orthodox design strategy when one of the classes needs to ac
Motive (motivation)In software systems, it is often faced with the creation of "some complex objects".These objects often face drastic changes due to changes in demand, but they have a fairly consistent interface. How to deal with this change? How to isolate "these volatile objects" from "Client programs (using these objects)" so that "client programs that rely on these volatile objects" do not change as demand changes.Intentions (Intent)Use the prototype instance to specify the kind of
32. Make sure public inheritance models "is-a". The so-called optimal design depends on what the system wants to do, including the present and future. The problem to be solved: there are some questions about the locations where both assert passes. Summary: "Public inheritance" means is-. Everything that applies to base classes must also apply to derived classes, because every derived class object is also a
32. Make sure your public inherits the is-a relationship from the moldPublic inheritance means the relationship of is-a (the principle of the Richter substitution), and every thing that applies to the base class also applies to inheriting classes.The rectangle inherits the square question:-can be implemented with a rectangular operation cannot be implemented with a square-In the field of programming, a square is a rectangle that is wrong-In the real world, a square is a rectangle that is correct
drops AH with the agent can solve the remote access Problem.Second, the virtual agent, according to the need to create a very expensive object, through it to hold the instantiation of the real object that takes a long time, so as to achieve performance optimization, such as open a large HTML page, there are many text and pictures, those words loaded, but the picture is not loaded, Only see the picture occu
(According to the msdn webcast courses)
Object-oriented solution solves the problem of System Abstraction without compromising the system performance. However, in some special applications, because the number of objects is too large, adopting object-oriented will bring an unbearable internal overhead to the system.
F
(); } }1.3 Client Calls class program { staticvoid Main (string[] args) { new Addfactory (); = operfactory.createoperation (); 1 ; 2 ; double result=oper. GetResult (); Console.WriteLine (result); Console.read (); } }Iv. comparison of simple factory and factory methodsThe client needs to be judged by using a simple factory, such as when the client passes the drop-down list to select
I have found that object-oriented design is too serious to be overlooked. Use the OOP language, but not object-oriented design. It is nothing more than writing C programs with classes,
(According to the msdn webcast courses)
New Problem: Implementing dependencies cannot cope with changes in the specific instantiation type. (When the instance object changes, it cannot meet the requirements)
Example:
When a road is prone to change and may become a mudroad, You need to modify all the new to road locations in the system.
Solution:
Closed change point-where to change and where to close; if the new type is relatively stable, there
;
2. Decoupling customer code from complex object container structures is the core idea of the composite mode, the Customer Code will be dependent on the pure abstract interface, rather than the complex internal implementation structure of the object container, so as to better "respond to changes ".
3. In composite mode, "add, remove, and other methods related to ob
According to the msdn webcast courses)
Adaptation: basically, the original incompatible interfaces are converted into compatible interfaces without changing the original implementations.
Gof: to convert an interface of a class to another interface required by the customer, you have to work together the classes that cannot work together due to incompatibility of the original interface.
Object Adapter:
Use an existing class as a field of the adapter
();}} /// /// Decoration Class C /// Public Class Decoratorc: decorator{ Public Decoratorc (tank): Base (Tank){} Public Override Void Shot (){ // Satellite Positioning Function Extension // Do shot... Base . Shot ();} Public Override Void Run (){ // Satellite Positioning Function Extension // Do run... Base . Run ();}}
Code highlighting produced by Actipro CodeHighlighter (freewar
, there is a problem that cannot be ignored:Explosive growth of subclass!
ProblematicRoot Cause: Excessive use of inheritance to extend the object function, because inheritance is a type-introducedStaticSuch expansion lacks flexibility.
How to solve the problem: static (compile-time) --> dynamic (runtime)
Dynamically add some additional responsibilities to an object. In terms of functions, th
(According to the msdn webcast courses)
Objects in different States often perform different actions.
Usually, we can use the switch... Case statement to solve the problem. However, if the state changes frequently, this must cause tight coupling between the object and the object. Of course, we should not deliberately adopt the design pattern to use the
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.