Kiss!
The most look at head first design mode, deeply than the big talk design mode deeper, the original feeling strategy and factory combination particularly uncomfortable, because also to change factory, this direct use interface choice, do not modify, only expand, shout, heard there are many people see this has difficulty, actually I see also have a lot of confused, Perhaps because of the problem with the point of view, this time from the new summary of the relationship between the class diagram, the code, the knowledge of what is not said.
relationship strength from weak to strong:
Dependency relationship (Dependency)---Association Relationship (Association)---aggregation (Aggregation)---combination (composition)---generalization (generalization)
The relationship corresponds to the code:
Inheritance
<span style= "FONT-SIZE:18PX;" >public class father{//father class}public class son extends father{//son class inherit father class}</span>
implementing Interfaces
<span style= "FONT-SIZE:18PX;" >//Buy Mobile Interface public interface buyiphone{//buy mobile phone method public void buy (); public class father{//Father class realization Buy cell phone method public void Buy () {//Buy mobile phone code slightly};} </span>
Dependentdependencies are put in code to declare or use other classes in a method, or to pass in an instance of a class.
<span style= "FONT-SIZE:18PX;" >public class iphone{//Specific method}//1, method declaration or Newpublic class chen{public void Usephone () {iphone chenphone;//statement iphone Chenphone = new IPhone ();//new the object of a cell phone};} 2, as a method of the parameters of public class Chen{public void Usephone (IPhone chenphone) {//implementation slightly};} </span>
Association (with one)
This is quite similar to the association, but the declaration is in a class, not a method.
<span style= "FONT-SIZE:18PX;" >//-dependent claims and new are public class Chen{iphone chenphone;//declared on the outside of the method iphone Chenphone = new iphone ();//new a phone object public void Usephone () {};} </span>
the difference between affinity and dependency:
- From the point of view of whether the properties of a class are increased:
The two classes that have dependencies do not increment the properties. One of these classes acts as either a parameter or a return value for a method of another class, or a variable of a method.
The two classes in which a relationship occurs, one of which becomes a property of another class, and the property is a more tightly coupled, longer-lasting relationship.
- In terms of the life cycle of the relationship:
Dependencies are generated only when the method of a class is called, and it ends with the end of the method.
Association relationships are generated when the class is instantiated, and when the class is destroyed, the relationship ends. Correlation relationships have a longer lifetime than dependency theory.
Combination
B class construction time, in its own class NEW the One A objects, B Object Release, A The object is then freed.
<span style= "FONT-SIZE:18PX;" >publicclass bird{<span style= "White-space:pre" ></span>public Wing wing;<span style= "White-space: Pre "></span>public void Bird () {<span style=" white-space:pre "></span>wing =new Wing;<span Style= "White-space:pre" ></SPAN>};} </span>
A bird cannot live without wings.
Aggregation
Class B constructs an object a,b object is releasedfrom the class, anda object is not released outside.
<span style= "FONT-SIZE:18PX;" >publicclass birdgroup{<span style= "White-space:pre" ></span>public Bird bird;<span style= " White-space:pre "></span>public void Birdgroup (Bird b) {<span style=" White-space:pre "></span> Bird =b;<span style= "White-space:pre" ></SPAN>}; </span>
The birds are gone, and the birds are still alive.
Design Principles
Pages |
Principle |
9 |
To find the changes, to separate them, and not to mix it up without change. |
11 |
Programming for interfaces, not for implementation. |
23 |
Multi-use combination, less inheritance |
53 |
A loose coupling effort between interacting objects |
86 |
Open extensions, close modifications |
139 |
Rely on abstraction, do not rely on specific |
265 |
Least know the principle: talk to a close friend only |
339 |
A class should have only one cause of change |
Summary:The second look at the design mode of the greatest feeling is, practice, the practice of time-base mode are very easy to resonate, and understand and did not write their own (refers not to read the code, examples, or use design mode), did not write their own have a kind of indefinitely, or lack of contact, this summed up the relationship between the code, In fact, they are very simple, usually use, engage in this line, are knocked out.
—————————— not practicing —————————————————— chenchen ————————
The "Head first" class diagram relationship corresponds to the code (JAVA)