Object-Oriented Design

Report 7 Downloads 118 Views
PAGE 1

iOS App Development

Object-Oriented Design

   _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 2

Learning Outcomes By the end of this topic, students should be able to 1. Explain the concept of Object-Oriented Design. 2. Explain tightly-coupled functions and their risks. 3. Explain loosely-coupled functions and their benefits. 4. Describe the relationship between a class and the objects created from it. 5. Describe how methods and properties are defined and are inherited in Object-Oriented Design. Vocabulary Object-Oriented Design Functions Tightly-coupled Loosely-coupled Class Objects Methods Properties Data Variable Instances Initialization

   _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 3

There are many different ways in which you can structure your code when programming; you can build a program to meet a needed goal in a variety of ways. However, if every developer approached their programming differently, it would be difficult to share code and best practices. The programming approach called Object Oriented Design is a set of loose guidelines to help keep the lines of communication open between programs, so it’s easier for developers to read each other’s code. Many languages such as c are procedural languages, where we simply have data and functions that manipulate that data. It allows us to build data and then set directions for what to do to that data. The data can either be used by multiple functions or passed back and forth from one function to another. Any function that wants to do something simply needs to gain visibility to the data declarations and then start setting them. Consequently, the program can become very difficult to maintain and debug as the program gets larger and larger. without a clear, consistent structure, it becomes difficult to fix problems that arise. Additionally, if multiple people are working on the same program, without a set of guidelines, there will be chaos in the code and the program is at risk for having many problems. The goal of Object Oriented Design is to increase the consistency and structure of code within a program so as to meet expectations of other developers and to reduce risk to the program. Coupling of Functions During the development of a program, our functions are likely to be connected to each other somehow. For example, function 1 and function 2 might use the same data, and the developers who are working on these functions need to agree on how the data used is going to be represented and what it means, and define a system of rules on when the functions can read and manipulate the data.

   _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 4

When this is the case, we refer to these two functions as “​ tightly coupled”​ . Tightly coupled systems can be risky because if you make a change in one area, it is highly likely to affect the other and break its functionality. Our goal is to keep our program’s designs as “​ loosely coupled​ ” as possible, meaning pieces of code have little or no knowledge of the other piece of code. A change in one is unlikely to affect the other.

Another possible problem in programming is that data is exposed for all functions to see and manipulate, even though the data may be of no interest to other functions. There is no protection of data items so that only the function(s) that actually need to see and set the data have access. Since everything is exposed, data can be manipulated by other functions to get a job done and the real “owner” of the data doesn’t even know this is happening. Later on, the “owner” changes their data to add some new capability and inadvertently breaks another functions capabilities because they were using data that they shouldn’t have. We can create a structure where data and functions are put together in a way where the data is kept private to those functions, and only a specific set of data is shared publicly to other functions in the code. This moves us from a “tightly coupled” system to a more    _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 5

“loosely coupled” system. This container - that puts together data and functions in private bundles - is called an Object. Take a look at the diagram below. Objects function just like a bank: there is a limited amount of function and data that is accessible by those outside the bank. However, within the walls of the bank, there are additional data and functions, but they’re protected by the walls of the bank.

   _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 6

Objects create strict control over what other functions and other objects can see and do with its innards. When you create an object, you decide what other code can read or set on data and what functions they can call. So for, example, in the diagram below, we see a function accessing Object A can only use the public functions and data, and cannot get access to the private data.

Example Let’s look at a couple of analogies of some real life objects. Lets take the pocket watch as an example. A pocket watch is a intricate time piece that has a rather complex implementation. However, to the user it is quite simple. In the diagram below, we see what the user of a pocket watch sees.    _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 7

If you were the person that makes the watch, here is what they would see. Clearly we wouldn’t want the user of the watch to be able to muck around in the inner workings. They look very delicate and it looks like it would be very easy to break the watch. This is exactly why we create objects in software. Many objects you implement will have potentially intricate designs and you want to ensure nobody else gets access to it.

   _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 8

Another good example is a car. A car has some obvious interfaces that allow someone to use the automobile, such as the steering wheel, the gas and brake pedal and ignition key. What would be hidden in the implementation?

   _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 9

Before you can declare an object, we first need to build it. Let’s use the car’s analogy. An individual car is an object; however, it didn’t magically appear. It was built based on a template, with specifications for most of its elements. Although the color may be specific to the individual car, the car doesn’t deviate from its model. So, we need to build a model that we can use to build individual objects. This model is called a ​ class​ . Think about what classification means in science: categorizing something based on specific characteristics and behaviors. The same is true in programming. Those characteristics and behaviors are the template used to build an object. A great analogy between class and object is to think about a cookie cutter and the cookie. A cookie cutter is analogous to the ​ class​ . The individual cookie is the ​ object. L​ ooking at the image below we can see that the cookie cutter will stamp out cookies that look like stars everytime.

When we a create a class in Swift, it defines all the properties (characteristics) and all the methods (behaviors) a new object of that class will have.    _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 10

Swift Classes To create a class in Swift we start with the keyword class followed by its name and then its properties and methods which is defined between left and right brackets { }. class StopWatch  {    // Properties and Methods will go here.  } 

Declaring an object Once we have a class defined we can create objects of that class - like punching out cookies. In Swift we create an object from a class in a very similar way that we create Arrays and Dictionaries. Rather than using a type specifier after your variable declaration, use the class name instead. var dadsStopWatch = StopWatch() 

Objects can have Properties. Properties are essentially attributes of an object, characteristics of the object. An attribute might be something like the type of car (where car is the object). A car might be traveling a certain speed. Speed is another attribute of the car. The car would have a color. Again, color is an attribute of car. We express attributes using properties. In our pocket watch example, the current time is an attribute. We could express the time on the stopwatch using three properties, a Float for the hour hand, minute hand and second hand. We express properties as follows:    _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 11

class StopWatch  {      ​ var hours = 0.0      var minutes = 0.0      var seconds = 0.0  }  Methods and Objects 

Methods are ways to send directions to an object. For example, in our pocket watch, one action we need is the watch to be wound. To do this we can create a method call ​ windWatch​ which will do just that: wind the watch. Additionally, another method we might want to act on our pocket watch is to set the time. We could do this with a method t​ urnMinuteHandClockwise​ , with a parameter of the numbers of minutes to advance the minute hand. We need to add a parameter to the turnMinuteHandClockwise method because we need to tell it how many minutes to turn. Here’s how we would declare these methods: class StopWatch  {      var hours = 0.0      var minutes = 0.0      var seconds = 0.0      func windWatch() {}      func turnMinuteHandClockwise(#numberOfMinutes:Int) {}  }     _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 12

Using the Object Now that we have an object with properties and methods, we can access the objects properties by using what is called ​ dot notation​ . For example if I want to get the value of dadsStopWatch (object) hour hand I would do it in the following way. var dadsStopWatch= StopWatch()  let currentHourHand = dadsStopWatch​ .hours 

To wind up our watch using our method ​ turnMinuteHandClockwise​ we can call it in the following way. dadsStopWatch​ .turnMinuteHandClockwise​ (numberOfMinutes: 5) 

Terminology An important note is that many of these concepts are shared across many languages. ​ Functions​ and d​ ata​ are called functions and methods​ in Swift, and ​ public data​ is called ​ properties​ and ​ instance variables​ in Swift.

   _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC 

PAGE 13

Initialization Now that we know how to create and use an object, let’s talk about Initializers. Initializers are methods that are used to provide information or data you want the object to have when it gets initialized. For instance in the above example, we would use an initializer to create and new instance of Stopwatch() and then if we wanted to change the properties to have certain values when this object gets created we use initializers. So we use initializers to set any information that is needed on that instance object before we start actually using the object. For example. If we wanted to change the values on the Stopwatch class every time we created a new instance of Stopwatch then we could do this. class StopWatch  {     var hours = 0.0      var minutes = 0.0      var seconds = 0.0    init() {    hours = 24    }     } 

Now if I were to create a new instance of StopWatch. var s = StopWatch()   

The hours property of this instance s.hours would be 24.

   _____________________________________________________________________________________________________________  MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co

© 2015 Mobile Makers Academy, LLC