Wednesday, December 18, 2013

Factory method design pattern - Creational design pattern

Factory method

Its a Creational design pattern.
Definition: “The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.”
Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.

Easy way
easily we can say that it’s a method that provides the object of a class at run time. Normally it happens by passing argument to that method and on the basis of that argument method decide which object to create.

Example :-
You have  ice-cream factory and having two flaour and want to add one more .  task of this method (Factory) will be to fulfill the desired demand that a customer makes. So basically Factory Method Pattern is just a method which actually fulfills our demand of object creation at run-time.

Same like for shape also See below UML diagram source http://www.tutorialspoint.com



Code in JAVA

Here are classes 
interface IShape
{
    string draw();
}
class Circle: IShape
{
    public string draw()
    {
        return "Circle";
    }
}
class Rectangle : IShape
{
    public string Functionality()
    {
        return "Rectangle";
    }
}
class Square : IShape
{
    public string Functionality()
    {
        return "Square";
    }

Factory method
static class Factory
{
   /// This is the Factory method
  
   public static IShape Get(int id)
   {
       switch (id)
       {
           case 0:
                return new Circle();
           case 1:
                return new Rectangle();
           case 2:
                return new Square();
           default:
                return null;
        }
   }

Factory method is just like regular method but when we are talking about patterns it just returns the instance of a class at run-time depending upon the input method gets.




Tuesday, December 17, 2013

Design pattern tutorial / Design pattern with example / Design pattern fundamentals / Creational Design Patterns / Structural design Patterns / Behavioral design pattern

Design pattern 
Design pattern is a general repeatable solution to a commonly occurring problem in object-oriented  software design. its like  library of solution to desired problem.  These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.  Design pattern is a description or template for how to solve a problem that can be used in many different situations.
All design patterns are  tested, proven development paradigms. Design Patterns improves code readability for coders and architects familiar with the patterns.

History
In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson und John Vlissides published a book titled Design Patterns - Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development. These authors are collectively known as Gang of Four (GOF).

Types of Design Pattern

1.Creational design Patterns
These design pattern provides way to create objects . It hides the logic of object creation. Objects are not created directly by calling constructor. It adds the flexibility in deciding which objects needs to be created for a given use case. like passing parameter will decide which object to create .
·      Abstarct factory
·      Builder
·      factory Method
·      Object pool
·      Prototype
·      Singleton

2.Structural design  Patterns
This design pattern concerns the class and object composition. It use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
·      Adapter
·      Bridge
·      Composite
·      decorator
·      facade
·      Flyweight
·      Private class data
·      Proxy

3. Behavioral design pattern
Its about the communication between objects.
·      Chain of responsibility
·      Command
·      Interpreter
·      Iterator
·      Mediator
·      Memento
·      Null Object
·      Observer
·      State
·      Strategy
·      Template method

·      Visitor

Monday, December 16, 2013

EXC_BAD_ACCESS iOS ( iPhone ) OR iOS EXC_BAD_ACCESS causes crash



  1. Set "Enable Zombie Objects" means NSZombieEnabled. which helps to find cause sometime.  To set the same go to  Menubar > Product > Scheme > Edit Scheme.   This optionwill provide a warning in logs  when you try to access an object that has been deallocated. That is the cause for crash.
                             2.Find memory leaks in your app. Use Leak instrument for to find  memory leaks  
    1. Open the Leaks instrument Menubar > Xcode > Open Developer tool > Instrument 
    2. Choose your app from the Choose Target pop-up menu
    3. Click the Record button.
    4. Exercise your app to execute code, and click the Stop button when leaks are displayed.
    5. Click any leaked object that is identified in the Detail pane.
    6. Within the Extended Detail pane, double-click an instruction from your code.
    7. Click the Xcode icon in the Detail pane to open that code in Xcode.
3. Analyse code . Find possible chances part of code .s et a breakpoint in that part of code and step through until you find crashing

4.Another way is comment part by part code and check which part of code is causing it

Wednesday, December 11, 2013

"dynamic" or @dynamic keyword in Objective C iOS (iPhone)

tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass  will be provided at runtime). Used in CoreData
Uses for @dynamic are e.g. with subclasses of NSManagedObject (CoreData) or when you want to create an outlet for a property defined by a superclass that was not defined as an outlet.
Super class:
@property (nonatomic, retain) NSButton *someButton;
...
@synthesize someButton;
Subclass:
@property (nonatomic, retain) IBOutlet NSButton *someButton;
...

@dynamic someButton;

Monday, December 9, 2013

Retain Cycles in iOS (iPhone)

Retain Cycles

There are two Objects P and Q. P creates Q and retains it. Q has an instance variable that points to P, retaining it. So both retain each other.


Example:-

NSMutableArray *P = [NSMutableArray array];
NSMutableArray *Q = [NSMutableArray array];
[P addObject:Q];
[Q addObject:P];
Here both P and Q has strong refernce to each other. 
Every time a reference is deleted, the count goes down, when the count gets to zero, there are no references and so the object can be deleted.
Neither will get deallocated unless you manually break the cycle by e.g. removing one from the other.

Often avoided because they can make it tricky to ensure you haven't got memory leaks.



 If you have a cycle, You may have a group of objects and you don't want them any more, so you drop the only reference you have to these objects, but because there is a cycle the objects reference each other. This means their reference counts never go to zero, and they don't get deleted. This is a memory leak.

Garbage Collection handles this retain cycle without memory leaks

Garbage Collection in iOS (iPhone)

Garbage Collection
When you use the Cocoa garbage collection technology, it manages your application's memory for you.
There is no need to explicitly manage objects' retain counts to ensure
that they remain "live" or that the memory they take up is reclaimed when they are no longer used.

How the Garbage Collector Works (here collector or collection means GC)
When a collection is initiated, the collector initializes the set with all well-known root objects. The collector then recursively follows strong references from these objects to other objects, and adds these to the set. At the end of the process, all objects that are not reachable through a chain of strong references to objects in the root set are designated as "garbage." At the end of the collection sequence, the unreachable objects are finalized and immediately afterwards the memory they occupy is recovered.

The initial rootset of objectsis comprised of global variables,stack variables, and objects with external references. These objects are never considered as garbage. The root set is comprised of all objects reachable from root objects and all possible references found by examining the call stacks of every Cocoa thread.

As implied earlier, there are two types of reference between objects—strong and weak. A strong reference is visible to the collector, a weak reference is not.
 An important corollary is that simply because you have a strong reference to an object does not mean that that object will survive garbage collection, 
You can create a weak reference using the keyword __weak, or by adding objects to a collection configured to use weak references (such as NSHashTable and NSMapTable).

Enabling Garbage Collection
Garbage collection is an optional feature; you need to set an appropriate flag for the compiler to mark code as being GC capable. The compiler will then use garbage collector write-barrier assignment primitives within the Objective-C runtime. An application marked GC capable will be started by the runtime with garbage collection enabled.

There are three possible compiler settings:
No flag. This means that GC is not supported.
-fobjc-gc-only This means that only GC logic is present.Code compiled as GC Required is presumed to not use traditional Cocoa retain/release methods and may not be loaded into an application that is not running with garbage collection enabled.
-fobjc-gc This means that both GC and retain/release logic is present.Code compiled as GC Supported is presumed to also contain traditional retain/release method logic and can be loaded into any application.
You can choose an option most easily by selecting the appropriate build setting in Xcode, as illustrated in





Foundation Tools (like command line code)
In a Cocoa desktop application, the garbage collector is automatically started and run for you. If you are writing a Foundation tool, you need to start the collector thread manually using the function objc_startCollectorThread:
#import <objc/objc-auto.h>
int main (int argc, const char * argv[]) {
objc_startCollectorThread();
// your code
return 0;
}

You may want to occasionally clear the stack using objc_clear_stack() to ensure that nothing is falsely rooted on the stack. You should typically do this when the stack is as shallow as possible—for example, at the top of a processing loop.
You can also use objc_collect(OBJC_COLLECT_IF_NEEDED) to provide a hint to the collector that collection might be appropriate—for example, after you finish using a large number of temporary objects.


Finalizing objects
In a garbage-collected application, you should ideally ensure that any external resources held by an object (such as open file descriptors) are closed prior to an object’s destruction. If you do need to perform some you must ensure that there are strong 
Nib files
references to all top-level objects in a nib file (including for example, stand-alone controllers)—otherwise they will be collected operations just before an object is reclaimed, you should do so in a finalize method.
You can create a strong reference simply by adding an outlet to the File's Owner and connecting it to a top-level object

Triggering garbage collection
Cocoa automatically hints at a suitable point in the event cycle that collection may be appropriate.
The collector then initiates collection if memory load exceeds a threshold. Typically this should be sufficient to provide good performance. Sometimes, however, you may provide a hint to the collector that collection may be warranted—for example after a loop in which you create a large number of temporary objects. You can do this using the NSGarbageCollector method collectIfNeeded.

// Create temporary objects
NSGarbageCollector *collector = [NSGarbageCollector defaultCollector];
[collector collectIfNeeded];

Threading
Garbage collection is performed on its own thread—a thread is explicitly registered with the collector if it calls NSThread's currentThread method (or if it uses an autorelease pool). There is no other explicit API for registering a pthread with the collector.




Prune caches
The collector scans memory to find reachable objects, so by definition keeps the working set hot. You should therefore make sure you get rid of objects you don't need.

Avoid allocating large numbers of short-lived objects
Object allocation is no less expensive an operation in a garbage collected environment than in a reference-counted environment.

Compile GC-Only
In general, you should not try to design your application to be dual-mode (that is, to support both garbage collection and reference-counted environments). The exception is if you are developing frameworks and you expect clients to operate in either mode.

C++
In general, C++ code should remain unchanged: you can assume memory allocated from standard malloc zone. If you need to ensure the longevity of Objective-C objects, you should use CFRetain instead of retain.



Garbage collection offers some significant advantages over a manually reference-counted environment 
-simplifies the task of managing memory
-reduces the amount of code you have to write and maintain
-makes it easier to write multi-threaded code: you do not have to use locks to ensure the atomicity of accessor methods and you do not have to deal with per-thread autorelease pools
(Note that although garbage collection simplifies some aspects of multi-threaded programming, it does not automaticallymake your application thread-safe. For more about thread-safe application development, see Threading Programming Guide .)

Garbage collection does though have some disadvantages:
-application’s working set may be larger
-Performance may not be as good as if you hand-optimize memory management
-A common design pattern whereby resources are tied to the lifetime of objects does not work effectively under GC.
-You must ensure that for any object you want to be long-lived you maintain a chain of strong references to it from a root object, or resort to reference counting for that object.
-Not all frameworks and technologies support garbage collection

Performance
The performance characteristics of an application that uses garbage collection are different from those of an application that uses reference-counting
Garbage-collected application may have betterperformance, for example:
Multi-threaded applications may perform better with garbage collection because of better thread support;
Accessor methods are much more efficient (you can implement them using simple assignment with no locks);
Your application is unlikely to have leaks or stale references.

In other areas, however, performance may be worse:
-Allocation may be a significant consideration if your application allocates large numbers of (possibly short-lived) objects.
The working set may be larger—in particular, the overall heap can grow larger due to allocation outpacing collection.
The collector scans heap memory to find reachable objects, so by definition keeps the working set hot.This may be a significant consideration, particularly if your application uses a large cache.
The collector runs in a secondary thread. As such, a GC-enabled application will in almost all cases consume more CPU cycles than a reference-counted application.

source:-https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/GarbageCollection/GarbageCollection.pdf

Wednesday, November 13, 2013

Version Mismatch - Neither CFBundleVersion nor CFBundleShortVersionString in the Info.plist match the version of the app set in iTunes Connect .

Thus is just warning message . It will not stop application  from submission.
If you want to correct the version you can reject current app from itunes.
But be careful when you change this version  review process will start over from the begining when you resubmit your binary.

About

Powered by Blogger.