Guest User

Untitled

a guest
Mar 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. //---------------------Single responsibility principle -----------------------------------------
  2.  
  3. // 1 class - 1 duty, in other words in one class could be one public method
  4. // for example - the screwdriver has something to twist. If you make it massive, then
  5. // it can be used for hammering nails
  6. // (though this is not very convenient),
  7. ---------------------------------
  8. // or another example: human Greg
  9. // he opened courses for something by himself.
  10. // He has 2 responsibilities:
  11. // he has to teach these courses,
  12. // and deal with financial issues.
  13.  
  14. class GreG{
  15. ...
  16. public void Teach(){}
  17. public void doSomeWithMoney(){}
  18. }
  19.  
  20. // according SRP one person has to hire another one person to take Greg out of one tis duty (financial issues or a teaching).
  21.  
  22. class GreG{
  23. ...
  24. public void Teach(){}
  25. }
  26.  
  27. class anotherMan{
  28. public void countMoney(){}
  29. }
  30.  
  31. //---------------------Open/closed principle-----------------------------------------------
  32.  
  33. // software entities (classes, modules, functions, etc.) should be opened for extension, but closed for modification
  34. // for instance, we have a Rectangle class witm 2 parametres and 1 method;
  35.  
  36. class Rectangle {
  37. int width,height;
  38. int Area(){
  39. return width*height;
  40. }
  41. }
  42.  
  43. // we have array of Rectangles and we should count suma of their areas in e new class
  44. class Areas{
  45. int areaCount(Rectangle [] rects){
  46. int sum=0;
  47. for(Rectangle r:rects){
  48. sum+=r.Area();
  49. }
  50. return sum;
  51. }
  52. }
  53.  
  54. // Later we need to extend this class for counting suma of Circles areas
  55.  
  56. class Areas{
  57. int areaCount( shape [] rects){
  58. int sum=0;
  59. if(shape instanceof Rectangle){
  60. for(Rectangle r:rects){
  61. sum+=r.Area();
  62. }
  63. }
  64. else
  65. {
  66. for(Circle c:rects){
  67. sum+=c.Area();
  68. }
  69. }
  70. return sum;
  71. }
  72. }
  73.  
  74. Further, in order not to change the class from time to time.
  75. we can create the abstract class "Shape" witch will have the function to calculate area.
  76. other figures extends frome Shape class and redefine the function if calculating area.
  77.  
  78. abstract class Shape{ double Area();}
  79.  
  80. class Rectangle extends Shape{
  81. double width,height;
  82. double Area(){
  83. return width*height;
  84. }
  85. }
  86. class Circle extends Shape{
  87. double radius;
  88. double Area(){
  89. return 3.14*radius*radius;
  90. }
  91. }
  92.  
  93. class Areas{
  94. int areaCount( Shape [] shapes){
  95. int sum=0;
  96. for(Shape s:shapes){
  97. sum+=s.Area();
  98. }
  99. return sum;
  100. }
  101. }
  102.  
  103. //---------------------Liskov substitution principle-----------------------------------
  104.  
  105. // all successors are able to include to their behavior the ancestor behavior
  106. // for Example: we have bird class and it can fly
  107.  
  108. class bird{
  109. string fly(){
  110. return "i can fly" ;
  111. }
  112. }
  113.  
  114. // and it has successors - duck, sparrow, ostrich and penguin
  115.  
  116. class duck extends bird{
  117. string fly(){
  118. return "i can fly" ;
  119. }
  120. string swim()
  121. {
  122. return "i can swim";
  123. }
  124. }
  125.  
  126. class sparrow extends bird{
  127. string fly(){
  128. return "i can fly" ;
  129. }
  130.  
  131. }
  132.  
  133. class ostrich extends bird{
  134. string fly(){
  135. throw "i can not fly" ;
  136. }
  137. string run()
  138. {
  139. return "i can run";
  140. }
  141. }
  142.  
  143. class penguin extends bird{
  144. string fly(){
  145. throw "i can not fly" ;
  146. }
  147. string swim()
  148. {
  149. return "i can swim";
  150. }
  151. string walk()
  152. {
  153. return "i can walk";
  154. }
  155. }
  156. // as we can see, there are the exceptions in ostrich and penguin classes. they can not fly.
  157. // and we must throw an exception.
  158. // it would be better to do the inheritance with another method or class,
  159. // for example, all birds can walk:
  160.  
  161. class bird{
  162. string Walk(){
  163. return "i can Walk" ;
  164. }
  165. }
  166.  
  167. class duck extends bird{
  168. string Walk(){
  169. return "i can Walk" ;
  170. }
  171. string fly(){
  172. return "i can fly" ;
  173. }
  174. string swim()
  175. {
  176. return "i can swim";
  177. }
  178. }
  179.  
  180. class sparrow extends bird{
  181. string Walk(){
  182. return "i can Walk" ;
  183. }
  184. string fly(){
  185. return "i can fly" ;
  186. }
  187.  
  188. }
  189.  
  190. class ostrich extends bird{
  191. string Walk(){
  192. return "i can Walk" ;
  193. }
  194. string run()
  195. {
  196. return "i can run";
  197. }
  198. }
  199.  
  200. class penguin extends bird{
  201. string Walk(){
  202. return "i can Walk" ;
  203. }
  204. string swim()
  205. {
  206. return "i can swim";
  207. }
  208. }
  209.  
  210.  
  211. //---------------------Interface segregation principle--------------------------------------
  212.  
  213. // for example, there is an interface "spend time" which will be added to the object "day"
  214.  
  215. interface ISpendTime{
  216. string readbook();
  217. string learnEnglish();
  218. string chatWithFriends();
  219. string doStuff();
  220. }
  221. class day implements ISpendTime{
  222. string readbook(){
  223. return "readBook";
  224. }
  225. string learnEnglish(){
  226. return "learnEnglish";
  227. }
  228. string chatWithFriends(){
  229. return "chatWithFriends";
  230. }
  231. string doStuff(){
  232. throw "I wodnt do this";
  233. }
  234. }
  235. // there is not the fact that someone will want to all these activities during one day
  236. // he have to refuse some these actions.
  237. // it is possible to add each of these actions to different interfaces.
  238. // And after that, connect those interfaces that are needed.
  239.  
  240. interface Ireadbook{
  241. string readbook();
  242. }
  243. interface IlearnEnglish{
  244. string learnEnglish();
  245. }
  246. interface IchatWithFriends{
  247. string chatWithFriends();
  248. }
  249. interface IdoStuff{
  250. string doStuff();
  251. }
  252. class day implements Ireadbook , IlearnEnglish{
  253. string readbook(){
  254. return "readBook";
  255. }
  256. string learnEnglish(){
  257. return "learnEnglish";
  258. }
  259. }
  260.  
  261. //---------------------Dependency inversion principle--------------------------------
  262. // high level modules should not depend on low level modules.
  263. // Abstractions should not depend on details.
  264. // Details should depend upon abstractions.
  265.  
  266. // if we have the method witch has as parameter some object,
  267. // but we want to use only one of metod of this object.
  268. // it would be beter to pass not specific object, but any object witch has the method we need.
  269.  
  270. // for example if we need to eat, it is not neccesary to go in
  271. // the certain shop or cafe, as we can just go to the nearest one
Add Comment
Please, Sign In to add comment