What is the use of creating type properties in Swift? - swift

What is the use of type method in swift can anyone give the realtime usage example? because I see it has not been used in any instance of the class
class SomeClass {
static var name=0
SomeClass.someTypeMethod()
}

Let's use the analogy of a car factory. We'll create a Car class. You can think of the Car class as the factory that builds Cars. You use an initializer to build new cars.
The class itself does work that affects all cars: sets prices, orders supplies, schedules payments, etc. An individual car provides transportation.
Say cars have FM radios, and they come with a default set of station presets. You might have a type method that changes the set of default stations that are programmed into a new car. Once you've changed the default stations, new cars built by the factory would have different default stations.

Related

How to decide whether a method should belong to which class?

I have 3 classes:
product
product_list
and customer
The product_list class has an array list, which stores product objects.
If I have a viewProduct method, it should belong to customer class or product_list class?
With my own concept, a viewProduct method should belong to customer class, because customer views products. But in code-wise, how a method in customer class gets data from product_list class?
A class is a data structure, bound together with the algorithms working on it.
Thus, if you decide where to put a method, the rule of thumb is this: select the class, on whose data structures it works the most. If you do it well, it should not even access the data members of other classes directly, only their methods.
Also from the name of the method is it visible, that you are right, that viewProduct should belong to customer: if it would belong to a product, then the viewProduct name would be redundant. Then a view name would be better.
You example does not say anything, how customers relate to specific products. For example, if you would develop a webshop, then the customers would likely choose a product from a list which your controllers generated for them.
Your specification lacks the mechanism, how customers relate to products. Think on it, and you will know the answer.

How to set attribute value to instance of class at runtime

I'm on Enterprise Architect 14. I have a component diagram containing an interface User and two classes Employee and Customer, which both realize interface User.
Furthermore I created two instances, one of each class and specified the values of the attributes via Features & Properties > Set Run State....
Next I created a component with 2 attributes, one of type Employee and one of type Customer. Then I created an instance of the component.
Now I would like to set run state of the component instance by assigning ArbitraryUser to the Employee attribute and ArbitraryCustomer to the Customer attribute of the component instance. According documentation this should be possible (see here).
At run-time, an Object instance can have specific values for its attributes, or exist in a particular state. To model the varying behavior of Objects at run-time, use instance values selected from the 'Select ' dialog and run-time states or run-states.
However I could not figure out how to do so. Can someone help me?
AFAIK that is not possible.
I'm not sure what the quote from the help really means, but I've only ever been able to type a value for the run state.
An partial alternative would be to use associations rather than attributes to model such relations. Then you can create a link as an instance of the association to relate instances of Employee or Customer with instances of a ArbitraryComponent.
This solution doesn't work for datatypes, but it seems a bit far fetched to start modelling instances of datatypes.

MVVM viewmodel and model questions

I'm trying to learn MVVM, and i'm struggling a little on differentiating between a model and viewmodel.
If someone could answer these 2 questions it would help clear a lot up for me:
Say I have an Objects class, which is a viewmodel that contains multiple ObservableCollections of Object.
The Object class contains an ObservableCollection of strings that are displayed on the GUI.
Is the Object class a model or viewmodel?
What if the Object class contains just a string and a integer (name and value), is it a model or viewmodel?
The Model is the class that holds your data. The data can be strings /integers or whatever.
The Model can also be a list / collection of those objects. For example a List of Person objects can still be your Model.
The ViewModel is the tier between your Model and the View. It should be used to perform whatever tasks you need on the data (for instance, if your Model is a list of Person objects but you only want to show in your view people that are aged older then 18, this logic is done in the ViewModel)
So to answer your question:
If you have an object which contains the data (in your example a list of strings) it is the Model.
Even if the object is a little more complex (with relation to the number of properties it holds) it's probably still the Model.
Business Logic should be kept separate from the model. On the other hand Validation can be added to the Model (for instance to make sure the Age property of a person is non-negative) since this is still rules on how your data should behave

what is difference between a Model and an Entity

I am confused to understand what is the meaning of this words:
Entity, Model, DataModel, ViewModel
Can any body help me to understanding them please? Thank you all.
The definition of these terms is quite ambiguous. You will find different definitions at different places.
Entity: An entity represents a single instance of your domain object saved into the database as a record. It has some attributes that we represent as columns in our tables.
Model: A model typically represents a real world object that is related to the problem or domain space. In programming, we create classes to represent objects. These classes, known as models, have some properties and methods (defining objects behavior).
ViewModel: The term ViewModel originates from the MVVM (Model View ViewModel) design pattern. There are instances in which the data to be rendered by the view comes from two different objects. In such scenarios, we create a model class which consists of all properties required by the view. It’s not a domain model but a ViewModel because, a specific view uses it. Also, it doesn’t represent a real world object.
DataModel: In order to solve a problem, objects interact with each other. Some objects share a relationship among them and consequently, form a data model that represents the objects and the relationship between them.
In an application managing customer orders, for instance, if we have a customer and order object then these objects share a many to many relationship between them. The data model is eventually dependent on the way our objects interact with each other. In a database, we see the data model as a network of tables referring to some other tables.
To know more about object relationships visit my blog post: Basics of Object Relationships
For more details visit my blog post: Entity vs Model vs ViewModel vs DataModel
I hope I've not missed your point here king.net...
Anyway, presuming you're talking about entity modelling or entity-relationship modelling (ERDs):
an entity represents any real world entity - e.g. student, course,
an entity will have attributes - e.g. student has first name, surname, date-of-birth
an entity will have relationships - e.g. student "is enrolled on" course (where student and course are entities with attributes and "is enrolled on" is the relationship.
the relationship may be "one-to-one", "one-to-many" or "many-to-many" - e.g. one student "is enrolled on" many courses and similarly one course "has" many students.
relationships also have cardinality
Adding relationships between entities creates a "data model". You've modeled some real world system and the internal entities/ objects in that system. Next step is to normalise it to ensure it meets "normal form".
In ERD terms, you may have "logical" and "physical" models. The logical describes the data-model in simple high-level terms that witholds the technical detail required to implement it. It represents the system solution overview. The physical model includes technical details required to actually implement the system (such as "many-to-many join tables" needed to implement "many-to-many" relationships).
Here are some tutorials on-line (though I'm sure there must be thousands):
http://www.maakal.com/maakalDB/Database101ERDpart1.htm
http://www.itteam-direct.com/gwentrel.htm
http://www.bkent.net/Doc/simple5.htm
I'm not quite sure what you mean by "model" and "view model" in a related context. Not sure if you may be confusing this with Model-View-Controller paradigm (MVC). Here, a model is some data component and the view represents an observer of that data (such as a table or graph UI component). There's lots on-line explaining "model view controller" or "MVC".
Hope this helps, Wayne
Entity:
An entity is the representation of a real-world element within Object Relational Mapping (ORM) as the Entity Framework. This representation will be mapped to a table in a database and its attributes will be transformed into columns. An entity is written using a POCO class that is a simple class, as you can see in the following example in C#:
using System;
using System.Collections.Generic;
using System.Text;
namespace MyAplication.Entity
{
public class Person
{
public long PersonId { get; set; }
public string Name { get; set; }
public short Age { get; set; }
}
}
Working with UI creation is a complex task. To keep things organized, programmers separate their applications into layers.
Each layer is responsible for a task and this prevents the code from being messed up. It is in this scenario that the architectural patterns like the MVC and the MVVM appear.
Model:
Within the MVC we have a layer responsible for representing the data previously stored, a given could be an instance of a person modeled in the previous example. This layer is the Model. This template will be used to construct the view.
ViewModel:
A ViewModel in the MVVM architecture is much like a Model in the MVC architecture. However a ViewModel is a simplified representation of the data with only the information that is required for the construction of a view.
using System;
using System.Collections.Generic;
using System.Text;
using MyAplication.Web.ViewModel.BaseViewModel;
namespace MyAplication.Web.ViewModel.Person
{
public class PersonNameViewModel : BaseViewModel<string>
{
//I just neet the name
public string Name { get; set; }
}
}
DataModel:
It is simply an abstract model (this model is different from the MVC layer model) which establishes the relationships that exist between the elements that represent real-world entities. It is a very comprehensive subject.
First of all,to know about Entity you must know about Class.
All of them represent same fields but the terminology changes based on declaration.
Let us consider a table from any database[SQL,ORACLE,Informix,Cassandra..] as example.
CLASS:
Generally a table is a considered as a class until it is added to edmx or dbmx.
//Student class
public class Student()
{
//Properties
public int StudentNumber;
public string StudentName;
}
ENTITY:
After drag drop/adding the table into dbmx/edmx it is referred to as
Entity.
Each Entity is generated from its corresponding class and we can add
attributes to entity which are used for performing operations using
linq or entity.
DATAMODEL:
Contains all the fields in table.
DATAMODEL is a direct class reference to your cshtml or controller
where you can access the attributes to perform CRUD operations.
VIEWMODEL:
Some situations occur where we need to perform CRUD operations more
than one model(table).
So we combine all our required models in a class and define them in
its constructor.
Example:
Lets assume
//Student class
public class Student()
{
//Properties
public int StudentNumber;
public string StudentName;
}
//Marks Class
Public class Marks()
{
public int Maths;
public int Physics;
public int Chemistry;
//Now sometimes situations occur where we have to use one datamodel inside //other datamodel.
public Student StudentModel;
}
Simple talk:
DTO stands for Data Transfer Object. DTOs are mainly used for transferring data between services (web services, APIs, etc.) which can encompass variety of properties of different entities (with or without their ID). Take this row as an example of a DTO: Consider that a shopping website is going to send its shipping requests to a shipping company by a web-service. Its DTO would be something like this: CustomerFullName, ShippingFee, ShippingAddress. In this example CustomerFullName is combination of properties FirstName + LastName for the Customer entity, and ShippingFee is the result of several processes of destination, tax, etc over some other entities.
On the contrary, Entities are bunch of properties gathered to represent a single entity with a specific ID (e.g., Teacher, Student, Employee, etc.). In other words, DTOs are a bunch of meaningless properties gathered to be sent to the client and a DTO doesn't necessarily have relationship to the other DTOs, while an Entity includes properties of a specific object with meaningful relation to the other entities. In a relational database paradigm, we can look at DTOs as views' row while Entities are tables' row with the primary key.
However, Model is a combination of these two. A model can contain several related entities plus extra data to handle real-world application/UI problems. Consider a Model named CustomerOrdersModel that contains Customer Entity, List<Order> Entities, and an extra Boolean flag PayWithCredit specifying whether user is going to pay with debit-card or credit-card.

Core data relation with predicates

I have an entity Car and an entity Race. Both entities has an attribute called carClass. My problem is that I want to create a relation between them on joining by carClass.
Basically I want to create a relation in core data which will represents the possible competitors of a race, depending on carClass, in other words if I create a race with a specific carClass and I call competitors on that race I want to get a list of cars with the same carClass.
My question is, is this possible to create using Core Data relations, I'm thinking about just adding an NSPredicate to a relation.
Thanks in advance.
Update
I think i didn't explained clearly what I want to achieve.
Car{
carClass:string
}
Race{
carClass:string
competitors<-->>cars.carClass == carClass
}
And the competitors should be fetched automatically, so I don't have to add manually the competitors, he should retrieve all the matching cars from the cars model.
The simplest model would look something like this:
CarClass{
name:string
race<-->Race.carClass
cars<-->>Car.carClass
}
Car{
name:string
carClass<<-->CarClass.cars
}
Race{
name:string
carClass<-->>CarClass.race
}
Don't think about this is SQL terms, think of it in object terms. Each entity should represent a real-world object, event or condition that you want to simulate. The relationships between objects should be mimic the real-world relationships. If both the Race and Car real-world things have a common relationship with a CarClass condition then you simply put that in the model.
Unlike SQL, object models can be arbitrarily complex and they can contain behaviors as well. Your not just stuck with tables, columns and rows.
Update:
(See update in OP for reference)
When you find yourself putting the same attribute value in two or more different entities, that usually means you need to create a new entity to model that value.
In this case, even though in terms of data the carClass value is just a string logically it is separate thing altogether that is related to both Race and Car objects. So, to accurately model it, you need to provide a separate entity for it. In addition, you need to model competitors so you need a model that looks something like:
CarClass{
class:string
race<-->Race.carClass
cars<-->>Car.carClass
}
Car{
name:string
carClass<<-->CarClass.cars
competitor<-->>Competitor.car
}
Race{
name:string
carClass<-->CarClass.race
}
Competitor{
name:string
car<-->Car.competitor
}
(Note that this model assumes that all races are defined by their carClass and not the individual vehicles or drivers.)
So, to find all competitors of a particular race, you would walk Race.carClass.cars.competitor.
This probably isn't exactly what you want but you can get the idea or how you use entities and relationships to model real-world objects, events or conditions and the linkages between them.
Sure it's possible. But why not just check each cars carClass when you're setting up the race and add the car to the race if the class matches?