Autofilling from one array to another - objective-c

Is there a similar pattern in objective-c to create an observer pattern as there is in Java?
That way the attributes of a super class will pass inheritance to subclasses.

Related

Is there any relation between class cluster and generics?

Is there any relation between class cluster and generics ?
So i was reading about class cluster pattern and it sounds like there is some similarity between class cluster pattern and generics.
They aren't very similar. Class cluster is a pattern for providing multiple implementations of the same abstract data structure, without exposing this detail to the library user.
For example there are many different ways to implement NSDictionary. Should it use a hash table? Linked list chaining? A tree? Just a linear searched array? There are several subclasses of NSDictionary that implement a few of these options. When you create an NSDictionary, it uses the parameters given to pick one of these subclasses to create. It is returned to you as an NSDictionary so you can use it without worrying about the underlying implementation.
Generics on the other hand provide a way to reuse the same implementation for several types. The basic example being Array<int>. The same array implementation can be given different types instead of int and they will work similarly.
Unlike class clusters, this actually doesn't change the underlying behavior of the class, other than substituting in a type.
I believe this is not the case because
class clusters is a design pattern while generics is a language feature.
Some more info at hand:
Class cluster
A class cluster is an architecture that groups a number of private,
concrete subclasses under a public, abstract superclass.
Generics
...
Generics are one of the most powerful features of Swift, and much
of the Swift standard library is built with generic code.

Why is factory method a class pattern, while an abstract factory an object pattern?

From GOF book:
Class patterns deal with relationships between classes and their subclasses. These relationships are established through inheritance,
so they are static-fixed at compile-time. Object patterns deal
with object relationships, which can be changed at run-time and
are more dynamic. Almost all patterns use inheritance to some extent.
So the only patterns labeled "class patterns" are those that focus on
class relationships.
Why is factory method a class pattern, and abstract factory an object pattern, given that they seem to be very similar patterns?
Thanks.
Factory Method and Abstract Factory are similar in intent, but wildly different in implementation (you might even say opposite). In other words, these patterns represent different ways of solving the same problem (instantiation).
The GoF says,
We classify design patterns by two criteria. The first criterion,
called purpose, reflects what a pattern does.
Because their intent is similar (i.e. they have the same purpose) these two patterns are both classified as creational.
The GoF goes on to say,
The second criterion, called scope, specifies whether the pattern applies
primarily to classes or to objects.
This leads into the quote from the OP, where class and object scope are each defined. Because Factory Method's implementation focuses on inheritance (a class relationship) while Abstract Factory's implementation focuses on composition (an object relationship) these two patterns are classified under opposing scopes.
The definitions and implementations of these two patterns can be found in numerous other SO threads, so I will not repeat them here. I have also touched on the composition vs. inheritance question in these two patterns elsewhere.
The GOF book says
Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate.
What does this mean? Let's take a look at the example that the book shows.
In the example a framework defines the Application interface to let others implement it. This means that I can implement e.g. a MyApplication or MyOtherApplication like this:
public class MyApplication extends Application {
protected Document createDocument() {
return new MyDocument();
}
}
public class MyOtherApplication extends Application {
protected Document createDocument() {
return new MyOtherDocument();
}
}
When the framework starts it might choose one of these implementations depending on what it finds on the classpath.
But it means that after the framework has instantiated either MyApplication or MyOtherApplication the way a document is created is fix. The way a document is created can not be changed anymore at runtime for the Application instance. There is no setter or anything else that you can use to change the way the Document is created. Thus it is also called a virtual constructor and thus it is a class pattern.
Abstract Factory
In contrast to the factory method an abstract factory can be changed at runtime and thus the way the objects it creates. That's why they say it is an object pattern.
A abstract factory is also responsible for creating
... families of related or dependent objects ...
This is also a difference to the factory method aka. virtual constructor.
Factory patterns are probably better to place in its own category. But logic behind object/class division maybe quite simple. Factory method in its minimal form is static (not configurable), just like classes are. But abstract factory result (object they produce) class depends on some input data, and since it is dynamic effect it should be put into object pattern category.

How to inherit multiple class in objective C?

I have classA and ClassA inherit ClassX and access methods of classX. How can I also access the methiod of ClassY with inheritance because multiple inheritance is not possible. I dont want to create another other composition class for this.I want to use same classA for multiple inheritance.
There is no multiple inheritance. The only way to achieve this is by merging the two class heirarchies. Either by having ClassX inherit ClassY or ClassY inherit ClassX (then ClassA inherits the child class X or Y).
If the two classes do not by design fit into the same hierarchy, you might want to reconsider your design and the reasons why you do not want to use composition.
Like Objective-C, Swift does not have multiple inheritance. Swift uses protocols and categories to give you the same sort of ability. You can define a protocol that defines a set of methods, and then you can add support for that protocol to multiple classes. You can often build support for a protocol into a category and then that category to your classes as needed.
As said before, multiple inheritance is not supported by the language (neither ObjC nor Swift). If you need to inherit methods/properties from multiple classes, you will need to use composition. Alternatively, what the language does allow you to do is to have a class conform to multiple protocols, which may or may not be a solution to the problem you are trying to solve.
I have come across very few cases where I thought that I really needed to have multiple inheritance, and even for those cases, they were typically resolved by employing an appropriate code design pattern (thinking about something like the Gang of Four design patterns). In essence, you want to abstract your code in such a way so that multiple inheritance is not a requirement anymore.

abstract factory and singleton pattern together

Is it possible combine the Abstract factory pattern and singleton pattern together?if yes then,how?give me some example with diagram .
The abstract factory pattern is all about creating objects. The singleton pattern is all about not creating more than one object of a particular type. How and why do you envisage them working together?

Dynamic Binding best approach in OOP

I have some classes in c++. with some hierarchy of base class and some derived classes.
There are some methods in the some derived classes that does functionality for that particular derived class and the other classes do not need any implementation of those methods. So i left them as empty implementation. (All of these functions are virtual)
Now my question is that what are the best practices in OOP while calling such a method with base class pointer in your program. I mean first check the type of reference stored in the base class pointer and then call the method or call the method in any way as the implementation of that particular method in other classes in empty.
In C++, run-time polymorphism is achieved through virtual functions. More info